OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015-2016, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using System.Diagnostics; |
| 36 using System.Runtime.InteropServices; |
| 37 using System.Threading; |
| 38 using System.Threading.Tasks; |
| 39 using Grpc.Core; |
| 40 using Grpc.Core.Internal; |
| 41 using Grpc.Core.Utils; |
| 42 using NUnit.Framework; |
| 43 |
| 44 namespace Grpc.Core.Tests |
| 45 { |
| 46 public class PInvokeTest |
| 47 { |
| 48 static readonly NativeMethods Native = NativeMethods.Get(); |
| 49 |
| 50 int counter; |
| 51 |
| 52 /// <summary> |
| 53 /// (~1.26us .NET Windows) |
| 54 /// </summary> |
| 55 [Test] |
| 56 public void CompletionQueueCreateDestroyBenchmark() |
| 57 { |
| 58 GrpcEnvironment.AddRef(); // completion queue requires gRPC environ
ment being initialized. |
| 59 |
| 60 BenchmarkUtil.RunBenchmark( |
| 61 10, 10, |
| 62 () => |
| 63 { |
| 64 CompletionQueueSafeHandle cq = CompletionQueueSafeHandle.Cre
ate(); |
| 65 cq.Dispose(); |
| 66 }); |
| 67 |
| 68 GrpcEnvironment.Release(); |
| 69 } |
| 70 |
| 71 /// <summary> |
| 72 /// Approximate results: |
| 73 /// (~80ns Mono Linux) |
| 74 /// (~110ns .NET Windows) |
| 75 /// </summary> |
| 76 [Test] |
| 77 public void NativeCallbackBenchmark() |
| 78 { |
| 79 OpCompletionDelegate handler = Handler; |
| 80 |
| 81 counter = 0; |
| 82 BenchmarkUtil.RunBenchmark( |
| 83 1000000, 10000000, |
| 84 () => |
| 85 { |
| 86 Native.grpcsharp_test_callback(handler); |
| 87 }); |
| 88 Assert.AreNotEqual(0, counter); |
| 89 } |
| 90 |
| 91 /// <summary> |
| 92 /// Creating a new native-to-managed callback has significant overhead |
| 93 /// compared to using an existing one. We need to be aware of this. |
| 94 /// (~50us on Mono Linux!!!) |
| 95 /// (~1.1us on .NET Windows) |
| 96 /// </summary> |
| 97 [Test] |
| 98 public void NewNativeCallbackBenchmark() |
| 99 { |
| 100 counter = 0; |
| 101 BenchmarkUtil.RunBenchmark( |
| 102 10000, 10000, |
| 103 () => |
| 104 { |
| 105 Native.grpcsharp_test_callback(new OpCompletionDelegate(Hand
ler)); |
| 106 }); |
| 107 Assert.AreNotEqual(0, counter); |
| 108 } |
| 109 |
| 110 /// <summary> |
| 111 /// Tests overhead of a simple PInvoke call. |
| 112 /// (~46ns .NET Windows) |
| 113 /// </summary> |
| 114 [Test] |
| 115 public void NopPInvokeBenchmark() |
| 116 { |
| 117 BenchmarkUtil.RunBenchmark( |
| 118 1000000, 100000000, |
| 119 () => |
| 120 { |
| 121 Native.grpcsharp_test_nop(IntPtr.Zero); |
| 122 }); |
| 123 } |
| 124 |
| 125 private void Handler(bool success) |
| 126 { |
| 127 counter++; |
| 128 } |
| 129 } |
| 130 } |
OLD | NEW |