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."); |
| 49 | var threads = new List<Thread>(); |
| 50 | for (int i = 0; i < runners.Count; i++) |
| 51 | { |
| 52 | var thread = new Thread(runners[i]); |
| 53 | thread.Start(); |
| 54 | threads.Add(thread); |
| 55 | } |
| 56 | |
| 57 | foreach (var thread in threads) |
| 58 | { |
| 59 | thread.Join(); |
| 60 | } |
| 61 | Console.WriteLine("All threads finished."); |
| 62 | } |
| 63 | } |
| 64 | } |