Index: third_party/grpc/src/csharp/Grpc.Core/Utils/BenchmarkUtil.cs |
diff --git a/third_party/protobuf/csharp/src/Google.Protobuf/Reflection/DescriptorUtil.cs b/third_party/grpc/src/csharp/Grpc.Core/Utils/BenchmarkUtil.cs |
similarity index 60% |
copy from third_party/protobuf/csharp/src/Google.Protobuf/Reflection/DescriptorUtil.cs |
copy to third_party/grpc/src/csharp/Grpc.Core/Utils/BenchmarkUtil.cs |
index f5570fc40a24f9e91017280b4f6389607be6e1a7..eb3a5b16e3c2544a92a74d5cd578838e071a3b6f 100644 |
--- a/third_party/protobuf/csharp/src/Google.Protobuf/Reflection/DescriptorUtil.cs |
+++ b/third_party/grpc/src/csharp/Grpc.Core/Utils/BenchmarkUtil.cs |
@@ -1,7 +1,7 @@ |
#region Copyright notice and license |
-// Protocol Buffers - Google's data interchange format |
-// Copyright 2008 Google Inc. All rights reserved. |
-// https://developers.google.com/protocol-buffers/ |
+ |
+// Copyright 2015, Google Inc. |
+// All rights reserved. |
// |
// Redistribution and use in source and binary forms, with or without |
// modification, are permitted provided that the following conditions are |
@@ -28,37 +28,45 @@ |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ |
#endregion |
+using System; |
+using System.Collections.Concurrent; |
using System.Collections.Generic; |
-using System.Collections.ObjectModel; |
+using System.Diagnostics; |
+using System.Threading.Tasks; |
-namespace Google.Protobuf.Reflection |
+namespace Grpc.Core.Utils |
{ |
/// <summary> |
- /// Internal class containing utility methods when working with descriptors. |
+ /// Utility methods to run microbenchmarks. |
/// </summary> |
- internal static class DescriptorUtil |
+ public static class BenchmarkUtil |
{ |
/// <summary> |
- /// Equivalent to Func[TInput, int, TOutput] but usable in .NET 2.0. Only used to convert |
- /// arrays. |
- /// </summary> |
- internal delegate TOutput IndexedConverter<TInput, TOutput>(TInput element, int index); |
- |
- /// <summary> |
- /// Converts the given array into a read-only list, applying the specified conversion to |
- /// each input element. |
+ /// Runs a simple benchmark preceded by warmup phase. |
/// </summary> |
- internal static IList<TOutput> ConvertAndMakeReadOnly<TInput, TOutput> |
- (IList<TInput> input, IndexedConverter<TInput, TOutput> converter) |
+ public static void RunBenchmark(int warmupIterations, int benchmarkIterations, Action action) |
{ |
- TOutput[] array = new TOutput[input.Count]; |
- for (int i = 0; i < array.Length; i++) |
+ var logger = GrpcEnvironment.Logger; |
+ |
+ logger.Info("Warmup iterations: {0}", warmupIterations); |
+ for (int i = 0; i < warmupIterations; i++) |
+ { |
+ action(); |
+ } |
+ |
+ logger.Info("Benchmark iterations: {0}", benchmarkIterations); |
+ var stopwatch = new Stopwatch(); |
+ stopwatch.Start(); |
+ for (int i = 0; i < benchmarkIterations; i++) |
{ |
- array[i] = converter(input[i], i); |
+ action(); |
} |
- return new ReadOnlyCollection<TOutput>(array); |
+ stopwatch.Stop(); |
+ logger.Info("Elapsed time: {0}ms", stopwatch.ElapsedMilliseconds); |
+ logger.Info("Ops per second: {0}", (int)((double)benchmarkIterations * 1000 / stopwatch.ElapsedMilliseconds)); |
} |
} |
-} |
+} |