Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 1 | #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 | |
| 34 | using System; |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 35 | using System.Collections.Concurrent; |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 36 | using System.Collections.Generic; |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 37 | using System.Diagnostics; |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 38 | using System.Threading.Tasks; |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 39 | |
| 40 | namespace 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 Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 49 | var logger = GrpcEnvironment.Logger; |
| 50 | |
| 51 | logger.Info("Warmup iterations: {0}", warmupIterations); |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 52 | for (int i = 0; i < warmupIterations; i++) |
| 53 | { |
| 54 | action(); |
| 55 | } |
| 56 | |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 57 | logger.Info("Benchmark iterations: {0}", benchmarkIterations); |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 58 | var stopwatch = new Stopwatch(); |
| 59 | stopwatch.Start(); |
| 60 | for (int i = 0; i < benchmarkIterations; i++) |
| 61 | { |
| 62 | action(); |
| 63 | } |
| 64 | stopwatch.Stop(); |
Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 65 | logger.Info("Elapsed time: {0}ms", stopwatch.ElapsedMilliseconds); |
| 66 | logger.Info("Ops per second: {0}", (int)((double)benchmarkIterations * 1000 / stopwatch.ElapsedMilliseconds)); |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } |