CompletionRegistryBenchmark.cs 2.0 KB

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