PInvokeTest.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 System.Diagnostics;
  18. using System.Runtime.InteropServices;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. using Grpc.Core;
  22. using Grpc.Core.Internal;
  23. using Grpc.Core.Utils;
  24. using NUnit.Framework;
  25. namespace Grpc.Core.Tests
  26. {
  27. public class PInvokeTest
  28. {
  29. static readonly NativeMethods Native = NativeMethods.Get();
  30. int counter;
  31. /// <summary>
  32. /// (~1.26us .NET Windows)
  33. /// </summary>
  34. [Test]
  35. public void CompletionQueueCreateSyncDestroyBenchmark()
  36. {
  37. GrpcEnvironment.AddRef(); // completion queue requires gRPC environment being initialized.
  38. BenchmarkUtil.RunBenchmark(
  39. 10, 10,
  40. () =>
  41. {
  42. CompletionQueueSafeHandle cq = CompletionQueueSafeHandle.CreateSync();
  43. cq.Dispose();
  44. });
  45. GrpcEnvironment.ReleaseAsync().Wait();
  46. }
  47. /// <summary>
  48. /// Approximate results:
  49. /// (~80ns Mono Linux)
  50. /// (~110ns .NET Windows)
  51. /// </summary>
  52. [Test]
  53. [Category("Performance")]
  54. [Ignore("Prevent running on Jenkins")]
  55. public void NativeCallbackBenchmark()
  56. {
  57. OpCompletionDelegate handler = Handler;
  58. counter = 0;
  59. BenchmarkUtil.RunBenchmark(
  60. 1000000, 10000000,
  61. () =>
  62. {
  63. Native.grpcsharp_test_callback(handler);
  64. });
  65. Assert.AreNotEqual(0, counter);
  66. }
  67. /// <summary>
  68. /// Creating a new native-to-managed callback has significant overhead
  69. /// compared to using an existing one. We need to be aware of this.
  70. /// (~50us on Mono Linux!!!)
  71. /// (~1.1us on .NET Windows)
  72. /// </summary>
  73. [Test]
  74. [Category("Performance")]
  75. [Ignore("Prevent running on Jenkins")]
  76. public void NewNativeCallbackBenchmark()
  77. {
  78. counter = 0;
  79. BenchmarkUtil.RunBenchmark(
  80. 10000, 10000,
  81. () =>
  82. {
  83. Native.grpcsharp_test_callback(new OpCompletionDelegate(Handler));
  84. });
  85. Assert.AreNotEqual(0, counter);
  86. }
  87. /// <summary>
  88. /// Tests overhead of a simple PInvoke call.
  89. /// (~46ns .NET Windows)
  90. /// </summary>
  91. [Test]
  92. [Category("Performance")]
  93. [Ignore("Prevent running on Jenkins")]
  94. public void NopPInvokeBenchmark()
  95. {
  96. BenchmarkUtil.RunBenchmark(
  97. 1000000, 100000000,
  98. () =>
  99. {
  100. Native.grpcsharp_test_nop(IntPtr.Zero);
  101. });
  102. }
  103. private void Handler(bool success)
  104. {
  105. counter++;
  106. }
  107. }
  108. }