This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
StatsdReporterTest.java
326 lines (292 loc) · 13.1 KB
/
StatsdReporterTest.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
* Copyright (C) 2012-2013 Sean Laurent
*
* Licensed 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 com.bealetech.metrics.reporting;
import com.yammer.metrics.core.*;
import com.yammer.metrics.reporting.AbstractPollingReporter;
import com.yammer.metrics.stats.Snapshot;
import org.junit.Before;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.mockito.stubbing.Stubber;
import java.io.ByteArrayOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
public class StatsdReporterTest {
protected final Clock clock = mock(Clock.class);
protected AbstractPollingReporter reporter;
protected TestMetricsRegistry registry;
protected DatagramPacket packet;
protected static class TestMetricsRegistry extends MetricsRegistry {
public <T extends Metric> T add(MetricName name, T metric) {
return getOrAdd(name, metric);
}
}
@Before
public void init() throws Exception {
when(clock.tick()).thenReturn(1234L);
when(clock.time()).thenReturn(5678L);
registry = new TestMetricsRegistry();
byte[] data = new byte[65536];
packet = new DatagramPacket(data, data.length);
reporter = createReporter(registry, clock);
}
protected AbstractPollingReporter createReporter(MetricsRegistry registry, Clock clock) throws Exception {
final DatagramSocket socket = mock(DatagramSocket.class);
final StatsdReporter.UDPSocketProvider provider = mock(StatsdReporter.UDPSocketProvider.class);
when(provider.get()).thenReturn(socket);
when(provider.newPacket(any(ByteArrayOutputStream.class))).thenReturn(packet);
final StatsdReporter reporter = new StatsdReporter(registry,
"prefix",
MetricPredicate.ALL,
provider,
clock);
reporter.setPrintVMMetrics(false);
return reporter;
}
protected <T extends Metric> void assertReporterOutput(Callable<T> action, String... expected) throws Exception {
// Invoke the callable to trigger (ie, mark()/inc()/etc) and return the metric
final T metric = action.call();
try {
// Add the metric to the registry, run the reporter and flush the result
registry.add(new MetricName(Object.class, "metric"), metric);
reporter.run();
String packetData = new String(packet.getData());
final String[] lines = packetData.split("\r?\n|\r");
// Assertions: first check that the line count matches then compare line by line ignoring leading and trailing whitespace
assertEquals("Line count mismatch, was:\n" + Arrays.toString(lines) + "\nexpected:\n" + Arrays
.toString(expected) + "\n", expected.length,
lines.length);
for (int i = 0; i < lines.length; i++) {
if (!expected[i].trim().equals(lines[i].trim())) {
System.err.println("Failure comparing line " + (1 + i));
System.err.println("Was: '" + lines[i] + "'");
System.err.println("Expected: '" + expected[i] + "'\n");
}
assertEquals(expected[i].trim(), lines[i].trim());
}
} finally {
reporter.shutdown();
}
}
public String[] expectedGaugeResult(String value) {
return new String[]{String.format("prefix.java.lang.Object.metric.count:%s|g", value)};
}
public String[] expectedTimerResult() {
return new String[]{
"prefix.java.lang.Object.metric.count:1|g",
"prefix.java.lang.Object.metric.meanRate:2.00|ms",
"prefix.java.lang.Object.metric.1MinuteRate:1.00|ms",
"prefix.java.lang.Object.metric.5MinuteRate:5.00|ms",
"prefix.java.lang.Object.metric.15MinuteRate:15.00|ms",
"prefix.java.lang.Object.metric.min:1.00|ms",
"prefix.java.lang.Object.metric.max:3.00|ms",
"prefix.java.lang.Object.metric.mean:2.00|ms",
"prefix.java.lang.Object.metric.stddev:1.50|ms",
"prefix.java.lang.Object.metric.median:0.50|ms",
"prefix.java.lang.Object.metric.75percentile:0.75|ms",
"prefix.java.lang.Object.metric.95percentile:0.95|ms",
"prefix.java.lang.Object.metric.98percentile:0.98|ms",
"prefix.java.lang.Object.metric.99percentile:0.99|ms",
"prefix.java.lang.Object.metric.999percentile:1.00|ms"
};
}
public String[] expectedMeterResult() {
return new String[]{
"prefix.java.lang.Object.metric.count:1|g",
"prefix.java.lang.Object.metric.meanRate:2.00|ms",
"prefix.java.lang.Object.metric.1MinuteRate:1.00|ms",
"prefix.java.lang.Object.metric.5MinuteRate:5.00|ms",
"prefix.java.lang.Object.metric.15MinuteRate:15.00|ms",
};
}
public String[] expectedHistogramResult() {
return new String[]{
"prefix.java.lang.Object.metric.min:1.00|ms",
"prefix.java.lang.Object.metric.max:3.00|ms",
"prefix.java.lang.Object.metric.mean:2.00|ms",
"prefix.java.lang.Object.metric.stddev:1.50|ms",
"prefix.java.lang.Object.metric.median:0.50|ms",
"prefix.java.lang.Object.metric.75percentile:0.75|ms",
"prefix.java.lang.Object.metric.95percentile:0.95|ms",
"prefix.java.lang.Object.metric.98percentile:0.98|ms",
"prefix.java.lang.Object.metric.99percentile:0.99|ms",
"prefix.java.lang.Object.metric.999percentile:1.00|ms"
};
}
public String[] expectedCounterResult(long count) {
return new String[]{
String.format("prefix.java.lang.Object.metric.count:%d|g", count)
};
}
@Test
public final void counter() throws Exception {
final long count = new Random().nextInt(Integer.MAX_VALUE);
assertReporterOutput(
new Callable<Counter>() {
@Override
public Counter call() throws Exception {
return createCounter(count);
}
},
expectedCounterResult(count));
}
@Test
public final void histogram() throws Exception {
assertReporterOutput(
new Callable<Histogram>() {
@Override
public Histogram call() throws Exception {
return createHistogram();
}
},
expectedHistogramResult());
}
@Test
public final void meter() throws Exception {
assertReporterOutput(
new Callable<Meter>() {
@Override
public Meter call() throws Exception {
return createMeter();
}
},
expectedMeterResult());
}
@Test
public final void timer() throws Exception {
assertReporterOutput(
new Callable<Timer>() {
@Override
public Timer call() throws Exception {
return createTimer();
}
},
expectedTimerResult());
}
@Test
public final void gauge() throws Exception {
final String value = "gaugeValue";
assertReporterOutput(
new Callable<Gauge<String>>() {
@Override
public Gauge<String> call() throws Exception {
return createGauge();
}
},
expectedGaugeResult(value));
}
static Counter createCounter(long count) throws Exception {
final Counter mock = mock(Counter.class);
when(mock.count()).thenReturn(count);
return configureMatcher(mock, doAnswer(new MetricsProcessorAction() {
@Override
void delegateToProcessor(MetricProcessor<Object> processor, MetricName name, Object context) throws Exception {
processor.processCounter(name, mock, context);
}
}));
}
static Histogram createHistogram() throws Exception {
final Histogram mock = mock(Histogram.class);
setupSummarizableMock(mock);
setupSamplingMock(mock);
return configureMatcher(mock, doAnswer(new MetricsProcessorAction() {
@Override
void delegateToProcessor(MetricProcessor<Object> processor, MetricName name, Object context) throws Exception {
processor.processHistogram(name, mock, context);
}
}));
}
static Gauge<String> createGauge() throws Exception {
@SuppressWarnings("unchecked")
final Gauge<String> mock = mock(Gauge.class);
when(mock.value()).thenReturn("gaugeValue");
return configureMatcher(mock, doAnswer(new MetricsProcessorAction() {
@Override
void delegateToProcessor(MetricProcessor<Object> processor, MetricName name, Object context) throws Exception {
processor.processGauge(name, mock, context);
}
}));
}
static Timer createTimer() throws Exception {
final Timer mock = mock(Timer.class);
when(mock.durationUnit()).thenReturn(TimeUnit.MILLISECONDS);
setupSummarizableMock(mock);
setupMeteredMock(mock);
setupSamplingMock(mock);
return configureMatcher(mock, doAnswer(new MetricsProcessorAction() {
@Override
void delegateToProcessor(MetricProcessor<Object> processor, MetricName name, Object context) throws Exception {
processor.processTimer(name, mock, context);
}
}));
}
static Meter createMeter() throws Exception {
final Meter mock = mock(Meter.class);
setupMeteredMock(mock);
return configureMatcher(mock, doAnswer(new MetricsProcessorAction() {
@Override
void delegateToProcessor(MetricProcessor<Object> processor, MetricName name, Object context) throws Exception {
processor.processMeter(name, mock, context);
}
}));
}
@SuppressWarnings("unchecked")
static <T extends Metric> T configureMatcher(T mock, Stubber stub) throws Exception {
stub.when(mock).processWith(any(MetricProcessor.class), any(MetricName.class), any());
return mock;
}
static abstract class MetricsProcessorAction implements Answer<Object> {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
@SuppressWarnings("unchecked")
final MetricProcessor<Object> processor = (MetricProcessor<Object>) invocation.getArguments()[0];
final MetricName name = (MetricName) invocation.getArguments()[1];
final Object context = invocation.getArguments()[2];
delegateToProcessor(processor, name, context);
return null;
}
abstract void delegateToProcessor(MetricProcessor<Object> processor, MetricName name, Object context) throws Exception;
}
static void setupSummarizableMock(Summarizable summarizable) {
when(summarizable.min()).thenReturn(1d);
when(summarizable.max()).thenReturn(3d);
when(summarizable.mean()).thenReturn(2d);
when(summarizable.stdDev()).thenReturn(1.5d);
}
static void setupMeteredMock(Metered metered) {
when(metered.count()).thenReturn(1L);
when(metered.oneMinuteRate()).thenReturn(1d);
when(metered.fiveMinuteRate()).thenReturn(5d);
when(metered.fifteenMinuteRate()).thenReturn(15d);
when(metered.meanRate()).thenReturn(2d);
when(metered.eventType()).thenReturn("eventType");
when(metered.rateUnit()).thenReturn(TimeUnit.SECONDS);
}
static void setupSamplingMock(Sampling sampling) {
final double[] values = new double[1000];
for (int i = 0; i < values.length; i++) {
values[i] = i / 1000.0;
}
when(sampling.getSnapshot()).thenReturn(new Snapshot(values));
}}