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 Grpc.Core;
21 using Grpc.Core.Internal;
22 using Grpc.Core.Logging;
23 using CommandLine;
24 using CommandLine.Text;
25 
26 namespace Grpc.Microbenchmarks
27 {
28     class Program
29     {
30         public enum MicrobenchmarkType
31         {
32             CompletionRegistry,
33             PInvokeByteArray,
34             SendMessage
35         }
36 
37         private class BenchmarkOptions
38         {
39             [Option("benchmark", Required = true, HelpText = "Benchmark to run")]
40             public MicrobenchmarkType Benchmark { get; set; }
41         }
42 
Main(string[] args)43         public static void Main(string[] args)
44         {
45             GrpcEnvironment.SetLogger(new ConsoleLogger());
46             var parserResult = Parser.Default.ParseArguments<BenchmarkOptions>(args)
47                 .WithNotParsed(errors => {
48                     Console.WriteLine("Supported benchmarks:");
49                     foreach (var enumValue in Enum.GetValues(typeof(MicrobenchmarkType)))
50                     {
51                         Console.WriteLine("  " + enumValue);
52                     }
53                     Environment.Exit(1);
54                 })
55                 .WithParsed(options =>
56                 {
57                     switch (options.Benchmark)
58                     {
59                         case MicrobenchmarkType.CompletionRegistry:
60                           RunCompletionRegistryBenchmark();
61                           break;
62                         case MicrobenchmarkType.PInvokeByteArray:
63                           RunPInvokeByteArrayBenchmark();
64                           break;
65                         case MicrobenchmarkType.SendMessage:
66                           RunSendMessageBenchmark();
67                           break;
68                         default:
69                           throw new ArgumentException("Unsupported benchmark.");
70                     }
71                 });
72         }
73 
RunCompletionRegistryBenchmark()74         static void RunCompletionRegistryBenchmark()
75         {
76             var benchmark = new CompletionRegistryBenchmark();
77             benchmark.Init();
78             foreach (int threadCount in new int[] {1, 1, 2, 4, 8, 12})
79             {
80                 foreach (bool useSharedRegistry in new bool[] {false, true})
81                 {
82                     benchmark.Run(threadCount, 4 * 1000 * 1000, useSharedRegistry);
83                 }
84             }
85             benchmark.Cleanup();
86         }
87 
RunPInvokeByteArrayBenchmark()88         static void RunPInvokeByteArrayBenchmark()
89         {
90             var benchmark = new PInvokeByteArrayBenchmark();
91             benchmark.Init();
92             foreach (int threadCount in new int[] {1, 1, 2, 4, 8, 12})
93             {
94                 benchmark.Run(threadCount, 4 * 1000 * 1000, 0);
95             }
96             benchmark.Cleanup();
97         }
98 
RunSendMessageBenchmark()99         static void RunSendMessageBenchmark()
100         {
101             var benchmark = new SendMessageBenchmark();
102             benchmark.Init();
103             foreach (int threadCount in new int[] {1, 1, 2, 4, 8, 12})
104             {
105                 benchmark.Run(threadCount, 4 * 1000 * 1000, 0);
106             }
107             benchmark.Cleanup();
108         }
109     }
110 }
111