• 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.Runtime.InteropServices;
21 using BenchmarkDotNet.Attributes;
22 using Grpc.Core.Internal;
23 
24 namespace Grpc.Microbenchmarks
25 {
26     public class ScalabilityExampleBenchmark : CommonThreadedBase
27     {
28         protected override bool NeedsEnvironment => false;
29 
30         // An example of testing scalability of a method that scales perfectly.
31         // This method provides a baseline for how well can CommonThreadedBase
32         // measure scalability.
33         const int Iterations = 50 * 1000 * 1000;  // High number to make the overhead of RunConcurrent negligible.
34         [Benchmark(OperationsPerInvoke = Iterations)]
PerfectScalingExample()35         public void PerfectScalingExample()
36         {
37             RunConcurrent(() => { RunBody(); });
38         }
39 
RunBody()40         private int RunBody()
41         {
42             int result = 0;
43             for (int i = 0; i < Iterations; i++)
44             {
45                 // perform some operation that is completely independent from
46                 // other threads and therefore should scale perfectly if given
47                 // a dedicated thread.
48                 for (int j = 0; j < 100; j++)
49                 {
50                    result = result ^ i ^ j ;
51                 }
52             }
53             return result;
54         }
55     }
56 }
57