PInvokeTest.cs 4.4 KB

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