SendMessageBenchmark.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #region Copyright notice and license
  2. // Copyright 2015, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #endregion
  31. using System;
  32. using System.Threading;
  33. using Grpc.Core;
  34. using Grpc.Core.Internal;
  35. using System.Collections.Generic;
  36. using System.Diagnostics;
  37. namespace Grpc.Microbenchmarks
  38. {
  39. public class SendMessageBenchmark
  40. {
  41. static readonly NativeMethods Native = NativeMethods.Get();
  42. GrpcEnvironment environment;
  43. public void Init()
  44. {
  45. Native.grpcsharp_test_override_method("grpcsharp_call_start_batch", "nop");
  46. environment = GrpcEnvironment.AddRef();
  47. }
  48. public void Cleanup()
  49. {
  50. GrpcEnvironment.ReleaseAsync().Wait();
  51. // TODO(jtattermusch): track GC stats
  52. }
  53. public void Run(int threadCount, int iterations, int payloadSize)
  54. {
  55. Console.WriteLine(string.Format("SendMessageBenchmark: threads={0}, iterations={1}, payloadSize={2}", threadCount, iterations, payloadSize));
  56. var threadedBenchmark = new ThreadedBenchmark(threadCount, () => ThreadBody(iterations, payloadSize));
  57. threadedBenchmark.Run();
  58. }
  59. private void ThreadBody(int iterations, int payloadSize)
  60. {
  61. // TODO(jtattermusch): parametrize by number of pending completions.
  62. // TODO(jtattermusch): parametrize by cached/non-cached BatchContextSafeHandle
  63. var completionRegistry = new CompletionRegistry(environment);
  64. var cq = CompletionQueueSafeHandle.CreateAsync(completionRegistry);
  65. var call = CreateFakeCall(cq);
  66. var sendCompletionHandler = new SendCompletionHandler((success) => { });
  67. var payload = new byte[payloadSize];
  68. var writeFlags = default(WriteFlags);
  69. var stopwatch = Stopwatch.StartNew();
  70. for (int i = 0; i < iterations; i++)
  71. {
  72. call.StartSendMessage(sendCompletionHandler, payload, writeFlags, false);
  73. var callback = completionRegistry.Extract(completionRegistry.LastRegisteredKey);
  74. callback(true);
  75. }
  76. stopwatch.Stop();
  77. Console.WriteLine("Elapsed millis: " + stopwatch.ElapsedMilliseconds);
  78. cq.Dispose();
  79. }
  80. private static CallSafeHandle CreateFakeCall(CompletionQueueSafeHandle cq)
  81. {
  82. var call = CallSafeHandle.CreateFake(new IntPtr(0xdead), cq);
  83. bool success = false;
  84. while (!success)
  85. {
  86. // avoid calling destroy on a nonexistent grpc_call pointer
  87. call.DangerousAddRef(ref success);
  88. }
  89. return call;
  90. }
  91. }
  92. }