• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2015 gRPC authors.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
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 
ThreadedBenchmark(IEnumerable<ThreadStart> runners)32         public ThreadedBenchmark(IEnumerable<ThreadStart> runners)
33         {
34             this.runners = new List<ThreadStart>(runners);
35         }
36 
ThreadedBenchmark(int threadCount, Action threadBody)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 
Run()46         public void Run()
47         {
48             Console.WriteLine("Running threads.");
49             var gcStats = new GCStats();
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             }
62             Console.WriteLine("All threads finished (GC Stats Delta: " + gcStats.GetSnapshot() + ")");
63         }
64     }
65 }
66