SendMessageBenchmark.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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;
  19. using Grpc.Core.Internal;
  20. namespace Grpc.Microbenchmarks
  21. {
  22. public class SendMessageBenchmark : CommonThreadedBase
  23. {
  24. static readonly NativeMethods Native = NativeMethods.Get();
  25. public override void Setup()
  26. {
  27. Native.grpcsharp_test_override_method("grpcsharp_call_start_batch", "nop");
  28. base.Setup();
  29. }
  30. [Params(0)]
  31. public int PayloadSize { get; set; }
  32. const int Iterations = 5 * 1000 * 1000; // High number to make the overhead of RunConcurrent negligible.
  33. [Benchmark(OperationsPerInvoke = Iterations)]
  34. public void SendMessage()
  35. {
  36. RunConcurrent(RunBody);
  37. }
  38. private void RunBody()
  39. {
  40. var completionRegistry = new CompletionRegistry(Environment, () => Environment.BatchContextPool.Lease(), () => throw new NotImplementedException());
  41. var cq = CompletionQueueSafeHandle.CreateAsync(completionRegistry);
  42. var call = CreateFakeCall(cq);
  43. var sendCompletionCallback = new NopSendCompletionCallback();
  44. var sliceBuffer = SliceBufferSafeHandle.Create();
  45. var writeFlags = default(WriteFlags);
  46. for (int i = 0; i < Iterations; i++)
  47. {
  48. // SendMessage steals the slices from the slice buffer, so we need to repopulate in each iteration.
  49. sliceBuffer.Reset();
  50. sliceBuffer.GetSpan(PayloadSize);
  51. sliceBuffer.Advance(PayloadSize);
  52. call.StartSendMessage(sendCompletionCallback, sliceBuffer, writeFlags, false);
  53. var callback = completionRegistry.Extract(completionRegistry.LastRegisteredKey);
  54. callback.OnComplete(true);
  55. }
  56. sliceBuffer.Dispose();
  57. cq.Dispose();
  58. }
  59. private static CallSafeHandle CreateFakeCall(CompletionQueueSafeHandle cq)
  60. {
  61. var call = CallSafeHandle.CreateFake(new IntPtr(0xdead), cq);
  62. bool success = false;
  63. while (!success)
  64. {
  65. // avoid calling destroy on a nonexistent grpc_call pointer
  66. call.DangerousAddRef(ref success);
  67. }
  68. return call;
  69. }
  70. private class NopSendCompletionCallback : ISendCompletionCallback
  71. {
  72. public void OnSendCompletion(bool success)
  73. {
  74. // NOP
  75. }
  76. }
  77. }
  78. }