| Jan Tattermusch | e58842f | 2017-05-19 15:52:05 +0200 | [diff] [blame] | 1 | #region Copyright notice and license | 
 | 2 |  | 
| Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2015 gRPC authors. | 
| Jan Tattermusch | e58842f | 2017-05-19 15:52:05 +0200 | [diff] [blame] | 4 | // | 
| Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 6 | // you may not use this file except in compliance with the License. | 
 | 7 | // You may obtain a copy of the License at | 
| Jan Tattermusch | e58842f | 2017-05-19 15:52:05 +0200 | [diff] [blame] | 8 | // | 
| Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
| Jan Tattermusch | e58842f | 2017-05-19 15:52:05 +0200 | [diff] [blame] | 10 | // | 
| Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | // Unless required by applicable law or agreed to in writing, software | 
 | 12 | // distributed under the License is distributed on an "AS IS" BASIS, | 
 | 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 14 | // See the License for the specific language governing permissions and | 
 | 15 | // limitations under the License. | 
| Jan Tattermusch | e58842f | 2017-05-19 15:52:05 +0200 | [diff] [blame] | 16 |  | 
 | 17 | #endregion | 
 | 18 |  | 
 | 19 | using System; | 
 | 20 | using System.Threading; | 
 | 21 | using Grpc.Core; | 
 | 22 | using Grpc.Core.Internal; | 
 | 23 | using System.Collections.Generic; | 
 | 24 | using System.Diagnostics; | 
 | 25 |  | 
 | 26 | namespace Grpc.Microbenchmarks | 
 | 27 | { | 
 | 28 |     public class ThreadedBenchmark | 
 | 29 |     { | 
 | 30 |         List<ThreadStart> runners; | 
 | 31 |  | 
 | 32 |         public ThreadedBenchmark(IEnumerable<ThreadStart> runners) | 
 | 33 |         { | 
 | 34 |             this.runners = new List<ThreadStart>(runners); | 
 | 35 |         } | 
 | 36 |  | 
 | 37 |         public ThreadedBenchmark(int threadCount, Action threadBody) | 
 | 38 |         { | 
 | 39 |             this.runners = new List<ThreadStart>(); | 
 | 40 |             for (int i = 0; i < threadCount; i++) | 
 | 41 |             { | 
 | 42 |                 this.runners.Add(new ThreadStart(() => threadBody())); | 
 | 43 |             } | 
 | 44 |         } | 
 | 45 |          | 
 | 46 |         public void Run() | 
 | 47 |         { | 
 | 48 |             Console.WriteLine("Running threads."); | 
| Jan Tattermusch | 1de0d69 | 2017-11-17 15:55:47 +0100 | [diff] [blame] | 49 |             var gcStats = new GCStats(); | 
| Jan Tattermusch | e58842f | 2017-05-19 15:52:05 +0200 | [diff] [blame] | 50 |             var threads = new List<Thread>(); | 
 | 51 |             for (int i = 0; i < runners.Count; i++) | 
 | 52 |             { | 
 | 53 |                 var thread = new Thread(runners[i]); | 
 | 54 |                 thread.Start(); | 
 | 55 |                 threads.Add(thread); | 
 | 56 |             } | 
 | 57 |  | 
 | 58 |             foreach (var thread in threads) | 
 | 59 |             { | 
 | 60 |                 thread.Join(); | 
 | 61 |             } | 
| Jan Tattermusch | 1de0d69 | 2017-11-17 15:55:47 +0100 | [diff] [blame] | 62 |             Console.WriteLine("All threads finished (GC Stats Delta: " + gcStats.GetSnapshot() + ")"); | 
| Jan Tattermusch | e58842f | 2017-05-19 15:52:05 +0200 | [diff] [blame] | 63 |         } | 
 | 64 |     } | 
 | 65 | } |