blob: 95b9aaaf3f95b6e06d2e2e574d2b558c0d5963ea [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.");
Jan Tattermusch1de0d692017-11-17 15:55:47 +010049 var gcStats = new GCStats();
Jan Tattermusche58842f2017-05-19 15:52:05 +020050 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 Tattermusch1de0d692017-11-17 15:55:47 +010062 Console.WriteLine("All threads finished (GC Stats Delta: " + gcStats.GetSnapshot() + ")");
Jan Tattermusche58842f2017-05-19 15:52:05 +020063 }
64 }
65}