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 AtomicCounterBenchmark : 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 = 20 * 1000 * 1000;  // High number to make the overhead of RunConcurrent negligible.
34         [Benchmark(OperationsPerInvoke = Iterations)]
35 
SharedAtomicCounterIncrement()36         public void SharedAtomicCounterIncrement()
37         {
38             RunConcurrent(() => { RunBody(); });
39         }
40 
41         readonly AtomicCounter sharedCounter = new AtomicCounter();
42 
RunBody()43         private int RunBody()
44         {
45             for (int i = 0; i < Iterations; i++)
46             {
47                 sharedCounter.Increment();
48             }
49             return (int) sharedCounter.Count;
50         }
51     }
52 }
53