Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(988)

Side by Side Diff: third_party/grpc/src/csharp/Grpc.IntegrationTesting/Histogram.cs

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #region Copyright notice and license
2
3 // Copyright 2015-2016, Google Inc.
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
15 // distribution.
16 // * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 #endregion
33
34 using System;
35 using System.Collections.Generic;
36 using System.Diagnostics;
37 using System.IO;
38 using System.Linq;
39 using System.Text.RegularExpressions;
40 using System.Threading;
41 using System.Threading.Tasks;
42 using Google.Protobuf;
43 using Grpc.Core;
44 using Grpc.Core.Utils;
45 using NUnit.Framework;
46 using Grpc.Testing;
47
48 namespace Grpc.IntegrationTesting
49 {
50 /// <summary>
51 /// Basic implementation of histogram based on grpc/support/histogram.h.
52 /// </summary>
53 public class Histogram
54 {
55 readonly object myLock = new object();
56 readonly double multiplier;
57 readonly double oneOnLogMultiplier;
58 readonly double maxPossible;
59 readonly uint[] buckets;
60
61 int count;
62 double sum;
63 double sumOfSquares;
64 double min;
65 double max;
66
67 public Histogram(double resolution, double maxPossible)
68 {
69 GrpcPreconditions.CheckArgument(resolution > 0);
70 GrpcPreconditions.CheckArgument(maxPossible > 0);
71 this.maxPossible = maxPossible;
72 this.multiplier = 1.0 + resolution;
73 this.oneOnLogMultiplier = 1.0 / Math.Log(1.0 + resolution);
74 this.buckets = new uint[FindBucket(maxPossible) + 1];
75
76 ResetUnsafe();
77 }
78
79 public void AddObservation(double value)
80 {
81 lock (myLock)
82 {
83 AddObservationUnsafe(value);
84 }
85 }
86
87
88 /// <summary>
89 /// Gets snapshot of stats and reset
90 /// </summary>
91 public HistogramData GetSnapshot(bool reset = false)
92 {
93 lock (myLock)
94 {
95 return GetSnapshotUnsafe(reset);
96 }
97 }
98
99 /// <summary>
100 /// Finds bucket index to which given observation should go.
101 /// </summary>
102 private int FindBucket(double value)
103 {
104 value = Math.Max(value, 1.0);
105 value = Math.Min(value, this.maxPossible);
106 return (int)(Math.Log(value) * oneOnLogMultiplier);
107 }
108
109 private void AddObservationUnsafe(double value)
110 {
111 this.count++;
112 this.sum += value;
113 this.sumOfSquares += value * value;
114 this.min = Math.Min(this.min, value);
115 this.max = Math.Max(this.max, value);
116
117 this.buckets[FindBucket(value)]++;
118 }
119
120 private HistogramData GetSnapshotUnsafe(bool reset)
121 {
122 var data = new HistogramData
123 {
124 Count = count,
125 Sum = sum,
126 SumOfSquares = sumOfSquares,
127 MinSeen = min,
128 MaxSeen = max,
129 Bucket = { buckets }
130 };
131
132 if (reset)
133 {
134 ResetUnsafe();
135 }
136
137 return data;
138 }
139
140 private void ResetUnsafe()
141 {
142 this.count = 0;
143 this.sum = 0;
144 this.sumOfSquares = 0;
145 this.min = double.PositiveInfinity;
146 this.max = double.NegativeInfinity;
147 for (int i = 0; i < this.buckets.Length; i++)
148 {
149 this.buckets[i] = 0;
150 }
151 }
152 }
153 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698