-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStatistics.cs
More file actions
237 lines (178 loc) · 7.48 KB
/
Statistics.cs
File metadata and controls
237 lines (178 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System.Threading;
using System.Diagnostics;
namespace net.vieapps.Services
{
public sealed class Statistics
{
long _requestsTotal;
int _requestsInFlight;
long _requestsHttpTotal;
int _requestsHttpInFlight;
long _cacheL1Hit304;
long _cacheL1Hit200;
long _cacheL1Miss;
long _cacheL1Bypass;
long _cacheL2Hit304;
long _cacheL2Hit200;
long _cacheL2Miss;
long _cacheL2Bypass;
long _rpcEntered;
int _rpcInFlight;
long _rpcRejected;
long _rpcCompleted;
long _rpcLatencyTotal;
long _rpcMaxLatency;
long _lastRequestsTotal;
long _lastRequestsHttpTotal;
long _lastRpcEntered;
long _lastRpcCompleted;
double GetRate(ref long last, long current, double elapsed)
{
if (elapsed <= 0)
return 0;
var delta = current - Interlocked.Exchange(ref last, current);
return delta > 0 ? delta / elapsed : 0;
}
public void IncreaseRequest(bool updateHttp = true)
{
Interlocked.Increment(ref this._requestsTotal);
Interlocked.Increment(ref this._requestsInFlight);
if (updateHttp)
{
Interlocked.Increment(ref this._requestsHttpTotal);
Interlocked.Increment(ref this._requestsHttpInFlight);
}
}
public void DecreaseRequest(bool updateHttp = true)
{
if (Interlocked.Decrement(ref this._requestsInFlight) < 0)
Interlocked.Exchange(ref this._requestsInFlight, 0);
if (updateHttp)
{
if (Interlocked.Decrement(ref this._requestsHttpInFlight) < 0)
Interlocked.Exchange(ref this._requestsHttpInFlight, 0);
}
}
public long RequestsTotal => Volatile.Read(ref this._requestsTotal);
public int RequestsInFlight => Volatile.Read(ref this._requestsInFlight);
public long RequestsHttpTotal => Volatile.Read(ref this._requestsHttpTotal);
public int RequestsHttpInFlight => Volatile.Read(ref this._requestsHttpInFlight);
public double GetRequestsRate(double elapsedSeconds, bool useHttp = false)
=> useHttp ? this.GetRequestsHttpRate(elapsedSeconds) : this.GetRate(ref this._lastRequestsTotal, this.RequestsTotal, elapsedSeconds);
public double GetRequestsHttpRate(double elapsedSeconds)
=> this.GetRate(ref this._lastRequestsHttpTotal, this.RequestsHttpTotal, elapsedSeconds);
public long L1Hit304()
=> Interlocked.Increment(ref this._cacheL1Hit304);
public long L1Hit200()
=> Interlocked.Increment(ref this._cacheL1Hit200);
public long L1Hit(bool is304)
=> is304 ? this.L1Hit304() : this.L1Hit200();
public long CacheL1Hit304Count => Volatile.Read(ref this._cacheL1Hit304);
public long CacheL1Hit200Count => Volatile.Read(ref this._cacheL1Hit200);
public long CacheL1HitCount => this.CacheL1Hit304Count + this.CacheL1Hit200Count;
public long L1Miss()
=> Interlocked.Increment(ref this._cacheL1Miss);
public long CacheL1MissCount => Volatile.Read(ref this._cacheL1Miss);
public long L1Bypass()
=> Interlocked.Increment(ref this._cacheL1Bypass);
public long CacheL1BypassCount => Volatile.Read(ref this._cacheL1Bypass);
public long L2Hit304()
=> Interlocked.Increment(ref this._cacheL2Hit304);
public long L2Hit200()
=> Interlocked.Increment(ref this._cacheL2Hit200);
public long L2Hit(bool is304)
=> is304 ? this.L2Hit304() : this.L2Hit200();
public long CacheL2Hit304Count => Volatile.Read(ref this._cacheL2Hit304);
public long CacheL2Hit200Count => Volatile.Read(ref this._cacheL2Hit200);
public long CacheL2HitCount => this.CacheL2Hit304Count + this.CacheL2Hit200Count;
public long L2Miss()
=> Interlocked.Increment(ref this._cacheL2Miss);
public long CacheL2MissCount => Volatile.Read(ref this._cacheL2Miss);
public long L2Bypass()
=> Interlocked.Increment(ref this._cacheL2Bypass);
public long CacheL2BypassCount => Volatile.Read(ref this._cacheL2Bypass);
double GetRatio(long count, bool useHttp = false)
{
var total = useHttp ? this.RequestsHttpTotal : this.RequestsTotal;
return total > 0 ? count * 100.0 / total : 0;
}
public double GetCacheL1HitRatio(bool useHttp = true)
=> this.GetRatio(this.CacheL1HitCount, useHttp);
public double GetCacheL1MissRatio(bool useHttp = true)
=> this.GetRatio(this.CacheL1MissCount, useHttp);
public double GetCacheL1BypassRatio(bool useHttp = true)
=> this.GetRatio(this.CacheL1BypassCount, useHttp);
public double GetCacheL2HitRatio(bool useHttp = true)
=> this.GetRatio(this.CacheL2HitCount, useHttp);
public double GetCacheL2MissRatio(bool useHttp = true)
=> this.GetRatio(this.CacheL2MissCount, useHttp);
public double GetCacheL2BypassRatio(bool useHttp = true)
=> this.GetRatio(this.CacheL2BypassCount, useHttp);
public void RpcEntered()
{
Interlocked.Increment(ref this._rpcEntered);
Interlocked.Increment(ref this._rpcInFlight);
}
public long RpcEnteredCount => Volatile.Read(ref this._rpcEntered);
public int RpcInFlightCount => Volatile.Read(ref this._rpcInFlight);
public long RpcRejectedCount => Volatile.Read(ref this._rpcRejected);
public void RpcRejected()
=> Interlocked.Increment(ref this._rpcRejected);
public long RpcCompletedCount => Volatile.Read(ref this._rpcCompleted);
public void RpcCompleted(long elapsedMilliseconds)
{
if (Interlocked.Decrement(ref this._rpcInFlight) < 0)
Interlocked.Exchange(ref this._rpcInFlight, 0);
Interlocked.Add(ref this._rpcLatencyTotal, elapsedMilliseconds);
Interlocked.Increment(ref this._rpcCompleted);
long currentMax;
do
{
currentMax = Volatile.Read(ref this._rpcMaxLatency);
if (elapsedMilliseconds <= currentMax)
return;
}
while (Interlocked.CompareExchange(ref this._rpcMaxLatency, elapsedMilliseconds, currentMax) != currentMax);
}
public void RpcCompleted(Stopwatch stopwatch)
=> this.RpcCompleted(stopwatch.ElapsedMilliseconds);
public long RpcMaxLatency => Volatile.Read(ref this._rpcMaxLatency);
public double RpcAverageLatency
{
get
{
var count = Volatile.Read(ref this._rpcCompleted);
return count <= 0 ? 0 : (double)Volatile.Read(ref this._rpcLatencyTotal) / count;
}
}
public double GetRpcEnteredRate(double elapsedSeconds)
=> this.GetRate(ref this._lastRpcEntered, this.RpcEnteredCount, elapsedSeconds);
public double GetRpcCompletedRate(double elapsedSeconds)
=> this.GetRate(ref this._lastRpcCompleted, this._rpcCompleted, elapsedSeconds);
public void Reset(long value = 0)
{
Interlocked.Exchange(ref this._requestsTotal, value);
Interlocked.Exchange(ref this._requestsInFlight, 0);
Interlocked.Exchange(ref this._requestsHttpTotal, value);
Interlocked.Exchange(ref this._requestsHttpInFlight, 0);
Interlocked.Exchange(ref this._cacheL1Hit304, value);
Interlocked.Exchange(ref this._cacheL1Hit200, value);
Interlocked.Exchange(ref this._cacheL1Miss, value);
Interlocked.Exchange(ref this._cacheL1Bypass, value);
Interlocked.Exchange(ref this._cacheL2Hit304, value);
Interlocked.Exchange(ref this._cacheL2Hit200, value);
Interlocked.Exchange(ref this._cacheL2Miss, value);
Interlocked.Exchange(ref this._cacheL2Bypass, value);
Interlocked.Exchange(ref this._rpcEntered, value);
Interlocked.Exchange(ref this._rpcRejected, value);
Interlocked.Exchange(ref this._rpcInFlight, 0);
Interlocked.Exchange(ref this._rpcCompleted, value);
Interlocked.Exchange(ref this._rpcLatencyTotal, value);
Interlocked.Exchange(ref this._rpcMaxLatency, value);
Interlocked.Exchange(ref this._lastRequestsTotal, value);
Interlocked.Exchange(ref this._lastRequestsHttpTotal, value);
Interlocked.Exchange(ref this._lastRpcEntered, value);
Interlocked.Exchange(ref this._lastRpcCompleted, value);
}
}
}