blob: feac8d1690825942dcee7d654e0f2451dab37e5b [file] [log] [blame]
Jan Tattermusche58842f2017-05-19 15:52:05 +02001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Jan Tattermusche58842f2017-05-19 15:52:05 +02004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// 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 Tattermusche58842f2017-05-19 15:52:05 +02008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermusche58842f2017-05-19 15:52:05 +020010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// 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 Tattermusche58842f2017-05-19 15:52:05 +020016
17#endregion
18
19using System;
20using System.Threading;
21using Grpc.Core;
22using Grpc.Core.Internal;
23using System.Collections.Generic;
24using System.Diagnostics;
25
26namespace 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}