• 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 Grpc.Core;
21 using Grpc.Core.Internal;
22 
23 namespace Grpc.Microbenchmarks
24 {
25     internal class GCStats
26     {
27         readonly object myLock = new object();
28         GCStatsSnapshot lastSnapshot;
29 
GCStats()30         public GCStats()
31         {
32             lastSnapshot = new GCStatsSnapshot(GC.CollectionCount(0), GC.CollectionCount(1), GC.CollectionCount(2));
33         }
34 
GetSnapshot(bool reset = false)35         public GCStatsSnapshot GetSnapshot(bool reset = false)
36         {
37             lock (myLock)
38             {
39                 var newSnapshot = new GCStatsSnapshot(GC.CollectionCount(0) - lastSnapshot.Gen0,
40                     GC.CollectionCount(1) - lastSnapshot.Gen1,
41                     GC.CollectionCount(2) - lastSnapshot.Gen2);
42                 if (reset)
43                 {
44                     lastSnapshot = newSnapshot;
45                 }
46                 return newSnapshot;
47             }
48         }
49     }
50 
51     public class GCStatsSnapshot
52     {
GCStatsSnapshot(int gen0, int gen1, int gen2)53         public GCStatsSnapshot(int gen0, int gen1, int gen2)
54         {
55             this.Gen0 = gen0;
56             this.Gen1 = gen1;
57             this.Gen2 = gen2;
58         }
59 
60         public int Gen0 { get; }
61         public int Gen1 { get; }
62         public int Gen2 { get; }
63 
ToString()64         public override string ToString()
65         {
66             return string.Format("[GCCollectionCount: gen0 {0}, gen1 {1}, gen2 {2}]", Gen0, Gen1, Gen2);
67         }
68     }
69 }
70