blob: 82653c3a1f587d12547104180cbd39072ffd3a89 [file] [log] [blame]
Jan Tattermusch50faa8f2015-02-21 17:51:52 -08001#region Copyright notice and license
2
3// Copyright 2015, 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
34using System;
Jan Tattermusch50faa8f2015-02-21 17:51:52 -080035using System.Collections.Concurrent;
Jan Tattermusch075dde42015-03-11 18:21:00 -070036using System.Collections.Generic;
Jan Tattermusch50faa8f2015-02-21 17:51:52 -080037using System.Diagnostics;
Jan Tattermusch075dde42015-03-11 18:21:00 -070038using System.Threading.Tasks;
Jan Tattermusch50faa8f2015-02-21 17:51:52 -080039
40namespace Grpc.Core.Utils
41{
42 public static class BenchmarkUtil
43 {
44 /// <summary>
45 /// Runs a simple benchmark preceded by warmup phase.
46 /// </summary>
47 public static void RunBenchmark(int warmupIterations, int benchmarkIterations, Action action)
48 {
Jan Tattermusch05261612015-07-24 00:28:16 -070049 var logger = GrpcEnvironment.Logger;
50
51 logger.Info("Warmup iterations: {0}", warmupIterations);
Jan Tattermusch50faa8f2015-02-21 17:51:52 -080052 for (int i = 0; i < warmupIterations; i++)
53 {
54 action();
55 }
56
Jan Tattermusch05261612015-07-24 00:28:16 -070057 logger.Info("Benchmark iterations: {0}", benchmarkIterations);
Jan Tattermusch50faa8f2015-02-21 17:51:52 -080058 var stopwatch = new Stopwatch();
59 stopwatch.Start();
60 for (int i = 0; i < benchmarkIterations; i++)
61 {
62 action();
63 }
64 stopwatch.Stop();
Jan Tattermusch05261612015-07-24 00:28:16 -070065 logger.Info("Elapsed time: {0}ms", stopwatch.ElapsedMilliseconds);
66 logger.Info("Ops per second: {0}", (int)((double)benchmarkIterations * 1000 / stopwatch.ElapsedMilliseconds));
Jan Tattermusch50faa8f2015-02-21 17:51:52 -080067 }
68 }
69}