SpreadStat implements Stat { @Override public void update(long value) { rollWindows(); long minValue = currentMin.get(); while (value < minValue && !currentMin.compareAndSet(minValue, value)) { minValue = currentMin.get(); } long maxValue = currentMax.get(); while (value > maxValue && !currentMax.compareAndSet(maxValue, value)) { maxValue = currentMax.get(); } total.add(value); count.increment(); } SpreadStat(String key);  SpreadStat(String key, Clock clock); @Override void update(long value); @Override String toString(); Snapshot getRate(); Snapshot getSum(); Snapshot getSamples(); Snapshot getAverage(); Snapshot getMin(); Snapshot getMax();  }
@Test public void testBasic() { SpreadTest test = new SpreadTest(); { test.assertSum(0, 0, 0, 0); test.assertRate(0, 0, 0, 0); test.assertMin(MAX_VALUE, MAX_VALUE, MAX_VALUE, MAX_VALUE); test.assertMax(MIN_VALUE, MIN_VALUE, MIN_VALUE, MIN_VALUE); test.assertAverage(0, 0, 0, 0); test.assertSamples(0, 0, 0, 0); test.advanceMillis(100); test.update(100); test.assertSum(100, 100, 100, 100); test.assertRate(100, 100, 100, 100); test.assertMin(100, 100, 100, 100); test.assertMax(100, 100, 100, 100); test.assertAverage(100, 100, 100, 100); test.assertSamples(1, 1, 1, 1); test.advanceMillis(456); test.update(100); test.assertSum(200, 200, 200, 200); test.assertRate(200, 200, 200, 200); test.assertMin(100, 100, 100, 100); test.assertMax(100, 100, 100, 100); test.assertAverage(100, 100, 100, 100); test.assertSamples(2, 2, 2, 2); test.advanceMillis(500); } { test.assertSum(200, 200, 200, 200); test.assertRate(200, 200, 200, 200); test.assertMin(100, 100, 100, 100); test.assertMax(100, 100, 100, 100); test.assertAverage(100, 100, 100, 100); test.assertSamples(2, 2, 2, 2); test.advanceMillis(100); test.assertSum(200, 200, 200, 200); test.assertRate(200, 200, 200, 200); test.assertMax(100, 100, 100, 100); test.assertAverage(100, 100, 100, 100); test.assertSamples(2, 2, 2, 2); test.advanceMillis(1_200); } { test.assertSum(200, 200, 200, 200); test.assertRate(100, 100, 100, 100); test.assertMax(100, 100, 100, 100); test.assertAverage(100, 100, 100, 100); test.assertSamples(2, 2, 2, 2); test.advanceMillis(202); test.update(123); test.advanceMillis(3); test.assertStats(); test.update(1); test.advanceMillis(5); test.assertStats(); test.update(2); test.advanceMillis(12); test.assertStats(); test.update(1); test.advanceMillis(1_300); test.assertStats(); } { test.update(111); test.advanceMillis(57_200); test.assertStats(); } { test.assertSum(238, 438, 438, 438); test.assertRate(3, 7, 7, 7); test.assertMax(123, 123, 123, 123); test.assertAverage(47, 62, 62, 62); test.assertSamples(5, 7, 7, 7); test.update(53); test.advanceMillis(1_802); } { test.assertSum(164, 491, 491, 491); test.assertRate(2, 7, 7, 7); test.assertMax(111, 123, 123, 123); test.assertAverage(82, 61, 61, 61); test.assertSamples(2, 8, 8, 8); } }
@Test public void testLongRunningSmallSteps() { SpreadTest test = new SpreadTest(); Random random = new Random(0); for (int i = 1; i < 5_000; ++i) { int value = random.nextBoolean() ? 0 : random.nextInt(100); if (value > 0 || random.nextBoolean()) { test.update(value); } test.assertStats(); test.advanceMillis(random.nextInt(100)); } }
@Test public void testLongRunningBigSteps() { SpreadTest test = new SpreadTest(); Random random = new Random(0); for (int i = 1; i < 5_000; ++i) { int value = random.nextBoolean() ? 0 : random.nextInt(100); if (value > 0 || random.nextBoolean()) { test.update(value); } test.assertStats(); test.advanceMillis(random.nextInt(5_000)); } }
@Test public void testTrivialLongRunning() { SpreadTest test = new SpreadTest(); for (int i = 1; i < 10_000; ++i) { test.advanceMillis(1_000); test.update(10); test.assertSum(10 * Math.min(i, 60), 10 * Math.min(i, 600), 10 * Math.min(i, 3600), 10 * i); test.assertRate(10, 10, 10, 10); test.assertMax(10, 10, 10, 10); test.assertAverage(10, 10, 10, 10); test.assertSamples(Math.min(i, 60), Math.min(i, 600), Math.min(i, 3600), i); } }
@Test public void testDelayAfterSustained() { SpreadTest test = new SpreadTest(); for (int i = 0; i < 10_000; ++i) { test.advanceMillis(1_000); test.update(10); } test.assertSum(600, 6000, 36_000, 100_000); test.advanceMillis(10_000); test.assertSum(500, 5900, 35_900, 100_000); }