1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.grinder.statistics;
24
25 import static java.util.Arrays.asList;
26 import static net.grinder.testutility.AssertUtilities.assertNotEquals;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertTrue;
30
31 import java.io.ByteArrayInputStream;
32 import java.io.ByteArrayOutputStream;
33 import java.io.ObjectInputStream;
34 import java.io.ObjectOutputStream;
35 import java.util.Random;
36
37 import net.grinder.statistics.StatisticsIndexMap.DoubleIndex;
38 import net.grinder.statistics.StatisticsIndexMap.LongIndex;
39 import net.grinder.util.Serialiser;
40
41 import org.junit.Before;
42 import org.junit.Test;
43
44
45
46
47
48
49
50
51 public class TestStatisticsSetImplementation {
52
53 private StatisticsIndexMap m_indexMap;
54
55 private LongIndex m_longIndex0;
56 private LongIndex m_longIndex1;
57 private LongIndex m_longIndex2;
58 private LongIndex m_transientLongIndex;
59
60 private DoubleIndex m_doubleIndex0;
61 private DoubleIndex m_doubleIndex1;
62 private DoubleIndex m_doubleIndex2;
63
64 @Before public void setUp() throws Exception {
65 m_indexMap = StatisticsServicesImplementation.getInstance()
66 .getStatisticsIndexMap();
67
68 m_longIndex0 = m_indexMap.getLongIndex("userLong0");
69 m_longIndex1 = m_indexMap.getLongIndex("userLong1");
70 m_longIndex2 = m_indexMap.getLongIndex("userLong2");
71 m_transientLongIndex = m_indexMap.getLongIndex("period");
72 m_doubleIndex0 = m_indexMap.getDoubleIndex("userDouble0");
73 m_doubleIndex1 = m_indexMap.getDoubleIndex("userDouble1");
74 m_doubleIndex2 = m_indexMap.getDoubleIndex("userDouble2");
75 }
76
77 @Test public void testCreation() {
78 final StatisticsSetImplementation statistics =
79 new StatisticsSetImplementation(m_indexMap);
80
81 assertEquals(0, statistics.getValue(m_longIndex1));
82 assertEquals(0, statistics.getValue(m_transientLongIndex));
83 assertDoublesEqual(0d, statistics.getValue(m_doubleIndex2));
84 assertFalse(statistics.isComposite());
85 }
86
87 @Test public void testReset() throws Exception {
88 final StatisticsSetImplementation statistics0 =
89 new StatisticsSetImplementation(m_indexMap);
90
91 assertTrue(statistics0.isZero());
92
93 statistics0.reset();
94 assertTrue(statistics0.isZero());
95
96 statistics0.setValue(m_longIndex2, 700);
97 statistics0.setValue(m_transientLongIndex, 123);
98 statistics0.setValue(m_doubleIndex2, -0.9999);
99 statistics0.setIsComposite();
100 assertEquals(700, statistics0.getValue(m_longIndex2));
101 assertEquals(123, statistics0.getValue(m_transientLongIndex));
102 assertDoublesEqual(-0.9999d, statistics0.getValue(m_doubleIndex2));
103 assertFalse(statistics0.isZero());
104 assertTrue(statistics0.isComposite());
105
106 statistics0.reset();
107 assertEquals(0, statistics0.getValue(m_longIndex2));
108 assertEquals(0, statistics0.getValue(m_transientLongIndex));
109 assertDoublesEqual(0d, statistics0.getValue(m_doubleIndex2));
110 assertTrue(statistics0.isZero());
111 assertFalse(statistics0.isComposite());
112 }
113
114 @Test public void testGetValueSetValueAndEquals() throws Exception {
115 final StatisticsSetImplementation statistics0 =
116 new StatisticsSetImplementation(m_indexMap);
117 final StatisticsSetImplementation statistics1 =
118 new StatisticsSetImplementation(m_indexMap);
119
120 assertEquals(statistics0, statistics0);
121 assertEquals(statistics0, statistics1);
122 assertEquals(statistics1, statistics0);
123
124 statistics0.setValue(m_transientLongIndex, 123);
125 assertNotEquals(statistics0, statistics1);
126 statistics0.setValue(m_transientLongIndex, 0);
127 assertEquals(statistics0, statistics1);
128
129 statistics0.setValue(m_longIndex1, 700);
130 assertEquals(700, statistics0.getValue(m_longIndex1));
131 statistics0.setValue(m_longIndex1, -300);
132 assertEquals(-300, statistics0.getValue(m_longIndex1));
133 assertNotEquals(statistics0, statistics1);
134 assertNotEquals(statistics1, statistics0);
135
136 statistics1.setValue(m_longIndex1, 500);
137 assertNotEquals(statistics0, statistics1);
138 statistics1.setValue(m_longIndex1, -300);
139 assertEquals(statistics0, statistics1);
140
141 statistics0.setValue(m_longIndex0, 1);
142 assertNotEquals(statistics0, statistics1);
143 statistics1.setValue(m_longIndex0, 1);
144 assertEquals(statistics0, statistics1);
145
146 assertEquals(statistics0, statistics0);
147 assertEquals(statistics1, statistics1);
148
149 statistics0.setValue(m_longIndex2, 0);
150 assertEquals(statistics0, statistics1);
151
152 statistics0.setValue(m_doubleIndex2, 7.00d);
153 assertDoublesEqual(7.00d, statistics0.getValue(m_doubleIndex2));
154 statistics0.setValue(m_doubleIndex2, 3.00d);
155 assertDoublesEqual(3.00d, statistics0.getValue(m_doubleIndex2));
156 assertNotEquals(statistics0, statistics1);
157
158 statistics1.setValue(m_doubleIndex2, 5.00d);
159 assertNotEquals(statistics0, statistics1);
160 statistics1.setValue(m_doubleIndex2, 3.00d);
161 assertEquals(statistics0, statistics1);
162
163 statistics0.setValue(m_doubleIndex0, -1.0d);
164 assertNotEquals(statistics0, statistics1);
165 statistics1.setValue(m_doubleIndex0, -1.0d);
166 assertEquals(statistics0, statistics1);
167
168 assertEquals(statistics0, statistics0);
169 assertEquals(statistics1, statistics1);
170
171 statistics0.setValue(m_doubleIndex1, 0);
172 assertEquals(statistics0, statistics1);
173
174 assertFalse(statistics0.isComposite());
175 statistics0.setIsComposite();
176 assertTrue(statistics0.isComposite());
177 assertNotEquals(statistics0, statistics1);
178 statistics1.setIsComposite();
179 assertEquals(statistics0, statistics1);
180 }
181
182 @Test public void testAddValue() throws Exception {
183 final StatisticsSetImplementation statistics0 =
184 new StatisticsSetImplementation(m_indexMap);
185 final StatisticsSetImplementation statistics1 =
186 new StatisticsSetImplementation(m_indexMap);
187
188 statistics0.addValue(m_longIndex1, 700);
189 statistics0.addValue(m_longIndex1, 300);
190 assertTrue(!statistics0.equals(statistics1));
191 statistics1.addValue(m_longIndex1, 500);
192 assertTrue(!statistics0.equals(statistics1));
193 statistics1.addValue(m_longIndex1, 500);
194 assertEquals(statistics0, statistics1);
195
196 statistics0.addValue(m_doubleIndex1, 7.00d);
197 statistics0.addValue(m_doubleIndex1, 3.00d);
198 assertTrue(!statistics0.equals(statistics1));
199 statistics1.addValue(m_doubleIndex1, 5.00d);
200 assertTrue(!statistics0.equals(statistics1));
201 statistics1.addValue(m_doubleIndex1, 5.00d);
202 assertEquals(statistics0, statistics1);
203 }
204
205 @Test public void testAddTransientValue() throws Exception {
206 final StatisticsSetImplementation statistics0 =
207 new StatisticsSetImplementation(m_indexMap);
208 final StatisticsSetImplementation statistics1 =
209 new StatisticsSetImplementation(m_indexMap);
210
211 statistics0.addValue(m_transientLongIndex, 700);
212 statistics1.addValue(m_transientLongIndex, 300);
213 assertEquals(statistics0, statistics1);
214 assertEquals(0, statistics1.getValue(m_transientLongIndex));
215 }
216
217 @Test public void testAdd() throws Exception {
218 final StatisticsSetImplementation statistics0 =
219 new StatisticsSetImplementation(m_indexMap);
220 final StatisticsSetImplementation statistics1 =
221 new StatisticsSetImplementation(m_indexMap);
222
223 assertTrue(statistics1.isZero());
224 assertFalse(statistics1.isComposite());
225
226
227 statistics0.add(statistics1);
228 assertEquals(statistics0, statistics1);
229
230 assertFalse(statistics0.isZero());
231 assertTrue(statistics1.isZero());
232 assertFalse(statistics1.isComposite());
233
234
235 statistics0.addValue(m_longIndex0, 100);
236 statistics0.addValue(m_doubleIndex2, -5.5);
237 statistics0.setIsComposite();
238 statistics1.add(statistics0);
239 assertEquals(statistics0, statistics1);
240 assertTrue(statistics1.isComposite());
241
242
243 statistics1.add(statistics0);
244 assertTrue(!statistics0.equals(statistics1));
245
246
247 statistics0.add(statistics0);
248 assertEquals(statistics0, statistics1);
249
250 assertEquals(200, statistics0.getValue(m_longIndex0));
251 assertDoublesEqual(-11d, statistics0.getValue(m_doubleIndex2));
252 }
253
254 @Test public void testSnapshot() throws Exception {
255 final StatisticsSetImplementation original =
256 new StatisticsSetImplementation(m_indexMap);
257 original.addValue(m_longIndex0, 10);
258 original.setValue(m_doubleIndex2, 3);
259 original.setIsComposite();
260
261 final StatisticsSet snapshot = original.snapshot();
262
263 assertDoublesEqual(3d, snapshot.getValue(m_doubleIndex2));
264 assertEquals(0, snapshot.getValue(m_longIndex1));
265 assertEquals(10, snapshot.getValue(m_longIndex0));
266 assertTrue(original.isComposite());
267
268 assertDoublesEqual(3d, original.getValue(m_doubleIndex2));
269 assertEquals(0, original.getValue(m_longIndex1));
270 assertEquals(10, original.getValue(m_longIndex0));
271
272 original.addValue(m_longIndex0, 5);
273 original.addValue(m_doubleIndex0, 20);
274 snapshot.addValue(m_longIndex0, 1);
275
276 assertDoublesEqual(3d, snapshot.getValue(m_doubleIndex2));
277 assertEquals(0, snapshot.getValue(m_longIndex1));
278 assertEquals(11, snapshot.getValue(m_longIndex0));
279
280 assertDoublesEqual(3d, original.getValue(m_doubleIndex2));
281 assertDoublesEqual(20d, original.getValue(m_doubleIndex0));
282 assertEquals(0, original.getValue(m_longIndex1));
283 assertEquals(15, original.getValue(m_longIndex0));
284
285 original.reset();
286 final StatisticsSet snapshot2 = original.snapshot();
287 assertTrue(snapshot2.isZero());
288 }
289
290 @Test public void testLongSampleReadAndWrite() throws Exception {
291 final StatisticsSet rawStatistics0 =
292 new StatisticsSetImplementation(m_indexMap);
293
294 final StatisticsIndexMap.LongSampleIndex longSampleIndex =
295 m_indexMap.getLongSampleIndex("timedTests");
296
297 assertEquals(0, rawStatistics0.getSum(longSampleIndex));
298 assertEquals(0, rawStatistics0.getCount(longSampleIndex));
299 assertDoublesEqual(0d, rawStatistics0.getVariance(longSampleIndex));
300
301 rawStatistics0.addSample(longSampleIndex, 0);
302 assertEquals(0, rawStatistics0.getSum(longSampleIndex));
303 assertEquals(1, rawStatistics0.getCount(longSampleIndex));
304 assertDoublesEqual(0d, rawStatistics0.getVariance(longSampleIndex));
305
306 rawStatistics0.addSample(longSampleIndex, 5);
307 assertEquals(5, rawStatistics0.getSum(longSampleIndex));
308 assertEquals(2, rawStatistics0.getCount(longSampleIndex));
309 assertDoublesEqual(6.25, rawStatistics0.getVariance(longSampleIndex));
310
311 rawStatistics0.addSample(longSampleIndex, 1);
312 assertEquals(6, rawStatistics0.getSum(longSampleIndex));
313 assertEquals(3, rawStatistics0.getCount(longSampleIndex));
314 assertDoublesEqual(14 / 3d, rawStatistics0.getVariance(longSampleIndex));
315
316 rawStatistics0.reset(longSampleIndex);
317 assertEquals(0, rawStatistics0.getSum(longSampleIndex));
318 assertEquals(0, rawStatistics0.getCount(longSampleIndex));
319 assertDoublesEqual(0d, rawStatistics0.getVariance(longSampleIndex));
320
321 final StatisticsSet rawStatistics1 =
322 new StatisticsSetImplementation(m_indexMap);
323
324 rawStatistics1.addSample(longSampleIndex, -1);
325 assertEquals(-1, rawStatistics1.getSum(longSampleIndex));
326 assertEquals(1, rawStatistics1.getCount(longSampleIndex));
327 assertDoublesEqual(0, rawStatistics1.getVariance(longSampleIndex));
328
329 rawStatistics0.add(rawStatistics1);
330 assertEquals(-1, rawStatistics0.getSum(longSampleIndex));
331 assertEquals(1, rawStatistics0.getCount(longSampleIndex));
332 assertDoublesEqual(0, rawStatistics0.getVariance(longSampleIndex));
333 assertEquals(-1, rawStatistics1.getSum(longSampleIndex));
334 assertEquals(1, rawStatistics1.getCount(longSampleIndex));
335 assertDoublesEqual(0, rawStatistics1.getVariance(longSampleIndex));
336 }
337
338 @Test public void testDoubleSampleReadAndWrite() throws Exception {
339 try {
340 final StatisticsIndexMap.DoubleIndex sumIndex = m_indexMap
341 .getDoubleIndex("userDouble0");
342 final StatisticsIndexMap.LongIndex countIndex = m_indexMap
343 .getLongIndex("userLong0");
344 final StatisticsIndexMap.DoubleIndex varianceIndex = m_indexMap
345 .getDoubleIndex("userDouble1");
346
347 final StatisticsIndexMap.DoubleSampleIndex doubleSampleIndex = m_indexMap
348 .createDoubleSampleIndex("testDoubleSampleStatistic", sumIndex,
349 countIndex, varianceIndex);
350
351 final StatisticsSet rawStatistics0 =
352 new StatisticsSetImplementation(m_indexMap);
353
354 assertDoublesEqual(0, rawStatistics0.getSum(doubleSampleIndex));
355 assertEquals(0, rawStatistics0.getCount(doubleSampleIndex));
356 assertDoublesEqual(0d, rawStatistics0.getVariance(doubleSampleIndex));
357
358 rawStatistics0.addSample(doubleSampleIndex, 0);
359 assertDoublesEqual(0, rawStatistics0.getSum(doubleSampleIndex));
360 assertEquals(1, rawStatistics0.getCount(doubleSampleIndex));
361 assertDoublesEqual(0d, rawStatistics0.getVariance(doubleSampleIndex));
362
363 rawStatistics0.addSample(doubleSampleIndex, 5);
364 assertDoublesEqual(5, rawStatistics0.getSum(doubleSampleIndex));
365 assertEquals(2, rawStatistics0.getCount(doubleSampleIndex));
366 assertDoublesEqual(6.25, rawStatistics0.getVariance(doubleSampleIndex));
367
368 rawStatistics0.addSample(doubleSampleIndex, 1);
369 assertDoublesEqual(6, rawStatistics0.getSum(doubleSampleIndex));
370 assertEquals(3, rawStatistics0.getCount(doubleSampleIndex));
371 assertDoublesEqual(14 / 3d, rawStatistics0.getVariance(doubleSampleIndex));
372
373 final StatisticsSet rawStatistics1 =
374 new StatisticsSetImplementation(m_indexMap);
375
376 rawStatistics0.add(rawStatistics1);
377 assertDoublesEqual(6, rawStatistics0.getSum(doubleSampleIndex));
378 assertEquals(3, rawStatistics0.getCount(doubleSampleIndex));
379 assertDoublesEqual(14 / 3d, rawStatistics0.getVariance(doubleSampleIndex));
380 assertDoublesEqual(0, rawStatistics1.getSum(doubleSampleIndex));
381 assertEquals(0, rawStatistics1.getCount(doubleSampleIndex));
382 assertDoublesEqual(0d, rawStatistics1.getVariance(doubleSampleIndex));
383
384 rawStatistics1.addSample(doubleSampleIndex, 5);
385 rawStatistics1.addSample(doubleSampleIndex, -5);
386 assertDoublesEqual(0, rawStatistics1.getSum(doubleSampleIndex));
387 assertEquals(2, rawStatistics1.getCount(doubleSampleIndex));
388 assertDoublesEqual(25, rawStatistics1.getVariance(doubleSampleIndex));
389
390 rawStatistics0.add(rawStatistics1);
391 assertDoublesEqual(6, rawStatistics0.getSum(doubleSampleIndex));
392 assertEquals(5, rawStatistics0.getCount(doubleSampleIndex));
393 assertDoublesEqual(13.76, rawStatistics0.getVariance(doubleSampleIndex));
394 assertDoublesEqual(0, rawStatistics1.getSum(doubleSampleIndex));
395 assertEquals(2, rawStatistics1.getCount(doubleSampleIndex));
396 assertDoublesEqual(25, rawStatistics1.getVariance(doubleSampleIndex));
397
398 rawStatistics0.reset(doubleSampleIndex);
399 assertDoublesEqual(0, rawStatistics0.getSum(doubleSampleIndex));
400 assertEquals(0, rawStatistics0.getCount(doubleSampleIndex));
401 assertDoublesEqual(0d, rawStatistics0.getVariance(doubleSampleIndex));
402 }
403 finally {
404 m_indexMap.removeDoubleSampleIndex("testDoubleSampleStatistic");
405 }
406 }
407
408 @Test public void testSerialisation() throws Exception {
409 final Random random = new Random();
410
411 final StatisticsSetImplementation original0 =
412 new StatisticsSetImplementation(m_indexMap);
413 original0.addValue(m_longIndex0, Math.abs(random.nextLong()));
414 original0.setValue(m_transientLongIndex, Math.abs(random.nextLong()));
415 original0.addValue(m_longIndex2, Math.abs(random.nextLong()));
416
417 final StatisticsSetImplementation original1 =
418 new StatisticsSetImplementation(m_indexMap);
419 original1.setIsComposite();
420
421 final ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
422
423 final ObjectOutputStream objectOutputStream =
424 new ObjectOutputStream(byteOutputStream);
425
426 final Serialiser serialiser = new Serialiser();
427
428 original0.writeExternal(objectOutputStream, serialiser);
429 original1.writeExternal(objectOutputStream, serialiser);
430
431 objectOutputStream.close();
432
433 final ObjectInputStream objectInputStream = new ObjectInputStream(
434 new ByteArrayInputStream(byteOutputStream.toByteArray()));
435
436 final StatisticsSetImplementation received0 =
437 new StatisticsSetImplementation(m_indexMap,
438 objectInputStream,
439 serialiser);
440
441 final StatisticsSetImplementation received1 =
442 new StatisticsSetImplementation(m_indexMap,
443 objectInputStream,
444 serialiser);
445
446
447 assertEquals(0, received0.getValue(m_transientLongIndex));
448 original0.setValue(m_transientLongIndex, 0);
449
450 assertEquals(original0, received0);
451 assertEquals(original1, received1);
452 }
453
454 @Test public void testEqualsMiscellanea() throws Exception {
455 final StatisticsSet rawStatistics0 =
456 new StatisticsSetImplementation(m_indexMap);
457 final StatisticsSet rawStatistics1 =
458 new StatisticsSetImplementation(m_indexMap);
459
460 assertFalse(rawStatistics0.equals(this));
461
462 final int hashCode0 = rawStatistics0.hashCode();
463 assertEquals(hashCode0, rawStatistics1.hashCode());
464 assertEquals(hashCode0, rawStatistics0.hashCode());
465 rawStatistics0.setValue(m_longIndex0, 99);
466 assertFalse(rawStatistics0.hashCode() == hashCode0);
467 }
468
469 private static String commaSeparate(int n, String value) {
470 final StringBuilder result = new StringBuilder();
471
472 result.append("{");
473
474 for (int i = 0; i < n; ++i) {
475 if (i != 0) {
476 result.append(", ");
477 }
478
479 result.append(value);
480 }
481
482 result.append("}");
483
484 return result.toString();
485 }
486
487 @Test public void testToString() throws Exception {
488
489 final StatisticsSet rawStatistics =
490 new StatisticsSetImplementation(m_indexMap);
491 final String s0 = rawStatistics.toString();
492
493 assertTrue(s0.indexOf(commaSeparate(m_indexMap.getNumberOfLongs(), "0"))
494 >= 0);
495
496 assertTrue(s0.indexOf(
497 commaSeparate(m_indexMap.getNumberOfTransientLongs(), "0"))
498 >= 0);
499
500 assertTrue(s0.indexOf(commaSeparate(m_indexMap.getNumberOfDoubles(), "0.0"))
501 >= 0);
502
503 assertTrue(s0.indexOf("composite = false") >= 0);
504 rawStatistics.setIsComposite();
505 assertTrue(rawStatistics.toString().indexOf("composite = true") >= 0);
506 }
507
508 @Test public void testToString2() throws Exception {
509
510 final StatisticsIndexMap statisticsIndexMap2 =
511 new StatisticsIndexMap(asList("a"),
512 asList("b"),
513 asList("c", "d"),
514 asList("timedTests"));
515
516 final StatisticsSet rawStatistics =
517 new StatisticsSetImplementation(statisticsIndexMap2);
518
519 assertEquals(
520 "StatisticsSet = {{0, 0, 0}, {0.0, 0.0}, {0, 0}, composite = false}",
521 rawStatistics.toString().toString());
522 }
523
524 private void assertDoublesEqual(double a, double b) {
525 assertEquals(a, b, 0.000000001d);
526 }
527 }