• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #endregion
18 
19 using System;
20 using BenchmarkDotNet.Attributes;
21 using Grpc.Core.Internal;
22 
23 namespace Grpc.Microbenchmarks
24 {
25     public class CompletionRegistryBenchmark : CommonThreadedBase
26     {
27         [Params(false, true)]
28         public bool UseSharedRegistry { get; set; }
29 
30         const int Iterations = 5 * 1000 * 1000;  // High number to make the overhead of RunConcurrent negligible.
31         [Benchmark(OperationsPerInvoke = Iterations)]
RegisterExtract()32         public void RegisterExtract()
33         {
34             CompletionRegistry sharedRegistry = UseSharedRegistry ? new CompletionRegistry(Environment, () => BatchContextSafeHandle.Create(), () => RequestCallContextSafeHandle.Create()) : null;
35             RunConcurrent(() =>
36             {
37                 RunBody(sharedRegistry);
38             });
39         }
40 
RunBody(CompletionRegistry optionalSharedRegistry)41         private void RunBody(CompletionRegistry optionalSharedRegistry)
42         {
43             var completionRegistry = optionalSharedRegistry ?? new CompletionRegistry(Environment, () => throw new NotImplementedException(), () => throw new NotImplementedException());
44             var ctx = BatchContextSafeHandle.Create();
45 
46             for (int i = 0; i < Iterations; i++)
47             {
48                 completionRegistry.Register(ctx.Handle, ctx);
49                 var callback = completionRegistry.Extract(ctx.Handle);
50                 // NOTE: we are not calling the callback to avoid disposing ctx.
51             }
52             ctx.Recycle();
53         }
54     }
55 }
56