forked from apache/bookkeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestStatsProvider.java
296 lines (247 loc) · 8.41 KB
/
TestStatsProvider.java
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.bookkeeper.test;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiConsumer;
import org.apache.bookkeeper.stats.Counter;
import org.apache.bookkeeper.stats.Gauge;
import org.apache.bookkeeper.stats.OpStatsData;
import org.apache.bookkeeper.stats.OpStatsLogger;
import org.apache.bookkeeper.stats.StatsLogger;
import org.apache.bookkeeper.stats.StatsProvider;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.StringUtils;
/**
* Simple in-memory stat provider for use in unit tests.
*/
public class TestStatsProvider implements StatsProvider {
/**
* In-memory counter.
*/
public class TestCounter implements Counter {
private AtomicLong val = new AtomicLong(0);
private AtomicLong max = new AtomicLong(0);
@Override
public void clear() {
val.set(0);
}
@Override
public void inc() {
updateMax(val.incrementAndGet());
}
@Override
public void dec() {
val.decrementAndGet();
}
@Override
public void addCount(long delta) {
updateMax(val.addAndGet(delta));
}
@Override
public void addLatency(long eventLatency, TimeUnit unit) {
long valueMillis = unit.toMicros(eventLatency) / 1000;
updateMax(val.addAndGet(valueMillis));
}
@Override
public Long get() {
return val.get();
}
private void updateMax(long newVal) {
while (true) {
long curMax = max.get();
if (curMax > newVal) {
break;
}
if (max.compareAndSet(curMax, newVal)) {
break;
}
}
}
public Long getMax() {
return max.get();
}
}
/**
* In-memory StatsLogger.
*/
public class TestOpStatsLogger implements OpStatsLogger {
private long successCount;
private long successValue;
private long failureCount;
private long failureValue;
TestOpStatsLogger() {
clear();
}
@Override
public void registerFailedEvent(long eventLatency, TimeUnit unit) {
registerFailedValue(TimeUnit.NANOSECONDS.convert(eventLatency, unit));
}
@Override
public void registerSuccessfulEvent(long eventLatency, TimeUnit unit) {
registerSuccessfulValue(TimeUnit.NANOSECONDS.convert(eventLatency, unit));
}
@Override
public synchronized void registerSuccessfulValue(long value) {
successCount++;
successValue += value;
}
@Override
public synchronized void registerFailedValue(long value) {
failureCount++;
failureValue += value;
}
@Override
public OpStatsData toOpStatsData() {
// Not supported at this time
return null;
}
@Override
public synchronized void clear() {
successCount = 0;
successValue = 0;
failureCount = 0;
failureValue = 0;
}
public synchronized double getSuccessAverage() {
if (successCount == 0) {
return 0;
}
return successValue / (double) successCount;
}
public synchronized long getSuccessCount() {
return successCount;
}
public synchronized long getFailureCount() {
return failureCount;
}
}
/**
* In-memory Logger.
*/
public class TestStatsLogger implements StatsLogger {
private String path;
TestStatsLogger(String path) {
this.path = path;
}
private String getSubPath(String name) {
if (path.isEmpty()) {
return name;
} else {
return path + "." + name;
}
}
@Override
public OpStatsLogger getOpStatsLogger(String name) {
return TestStatsProvider.this.getOrCreateOpStatsLogger(getSubPath(name));
}
@Override
public Counter getCounter(String name) {
return TestStatsProvider.this.getOrCreateCounter(getSubPath(name));
}
public Gauge<? extends Number> getGauge(String name) {
return gaugeMap.get(getSubPath(name));
}
@Override
public <T extends Number> void registerGauge(String name, Gauge<T> gauge) {
TestStatsProvider.this.registerGauge(getSubPath(name), gauge);
}
@Override
public <T extends Number> void unregisterGauge(String name, Gauge<T> gauge) {
TestStatsProvider.this.unregisterGauge(getSubPath(name), gauge);
}
@Override
public StatsLogger scope(String name) {
return new TestStatsLogger(getSubPath(name));
}
@Override
public void removeScope(String name, StatsLogger statsLogger) {}
@Override
public OpStatsLogger getThreadScopedOpStatsLogger(String name) {
return getOpStatsLogger(name);
}
@Override
public Counter getThreadScopedCounter(String name) {
return getCounter(name);
}
}
@Override
public void start(Configuration conf) {
}
@Override
public void stop() {
}
private Map<String, TestOpStatsLogger> opStatLoggerMap = new ConcurrentHashMap<>();
private Map<String, TestCounter> counterMap = new ConcurrentHashMap<>();
private Map<String, Gauge<? extends Number>> gaugeMap = new ConcurrentHashMap<>();
@Override
public TestStatsLogger getStatsLogger(String scope) {
return new TestStatsLogger(scope);
}
public TestOpStatsLogger getOpStatsLogger(String path) {
return opStatLoggerMap.get(path);
}
public TestCounter getCounter(String path) {
return counterMap.get(path);
}
public Gauge<? extends Number> getGauge(String path) {
return gaugeMap.get(path);
}
public void forEachOpStatLogger(BiConsumer<String, TestOpStatsLogger> f) {
for (Map.Entry<String, TestOpStatsLogger> entry : opStatLoggerMap.entrySet()) {
f.accept(entry.getKey(), entry.getValue());
}
}
public void clear() {
for (TestOpStatsLogger logger : opStatLoggerMap.values()) {
logger.clear();
}
for (TestCounter counter : counterMap.values()) {
counter.clear();
}
}
private TestOpStatsLogger getOrCreateOpStatsLogger(String path) {
return opStatLoggerMap.computeIfAbsent(
path,
(String s) -> new TestOpStatsLogger());
}
private TestCounter getOrCreateCounter(String path) {
return counterMap.computeIfAbsent(
path,
(String s) -> new TestCounter());
}
private <T extends Number> void registerGauge(String name, Gauge<T> gauge) {
gaugeMap.put(name, gauge);
}
private <T extends Number> void unregisterGauge(String name, Gauge<T> gauge) {
gaugeMap.remove(name, gauge);
}
@Override
public String getStatsName(String... statsComponents) {
if (statsComponents.length == 0) {
return "";
} else if (statsComponents[0].isEmpty()) {
return StringUtils.join(statsComponents, '.', 1, statsComponents.length);
} else {
return StringUtils.join(statsComponents, '.');
}
}
}