@Test @SuppressWarnings("unchecked") public void testGetTimeSinceLastMeasurement() throws Exception { long sleepTime = 20L; // fill history with the same value. long now = System.nanoTime() - 2 * sleepTime * 1000000; for (int i = 0; i < TestUtils.getPropertyValue(history, "retention", Integer.class); i++) { history.success(now); } final Deque<Long> times = TestUtils.getPropertyValue(history, "times", Deque.class); assertEquals(Long.valueOf(now), times.peekFirst()); assertEquals(Long.valueOf(now), times.peekLast()); // increment just so we'll have a different value between first and last history.success(System.nanoTime() - sleepTime * 1000000); assertNotEquals(times.peekFirst(), times.peekLast()); /* * We've called Thread.sleep twice with the same value in quick * succession. If timeSinceLastSend is pulling off the correct end of * the queue, then we should be closer to the sleep time than we are to * 2 x sleepTime, but we should definitely be greater than the sleep * time. */ double timeSinceLastMeasurement = history.getTimeSinceLastMeasurement(); assertThat(timeSinceLastMeasurement, Matchers.greaterThan((double) (sleepTime / 100))); assertThat(timeSinceLastMeasurement, Matchers.lessThanOrEqualTo(1.5 * sleepTime / 100)); }
@Test @Ignore public void testPerf() { ExponentialMovingAverageRatio ratio = new ExponentialMovingAverageRatio(60, 10); for (int i = 0; i < 100000; i++) { if (i % 10 == 0) { ratio.failure(); } else { ratio.success(); } } }
@Test public void testRatio() { ExponentialMovingAverageRatio ratio = new ExponentialMovingAverageRatio(60, 10, true); for (int i = 0; i < 100; i++) { if (i % 10 == 1) { ratio.failure(); } else { ratio.success(); } } assertEquals(0.9, ratio.getMax(), 0.02); assertEquals(0.9, ratio.getMean(), 0.03); }
@Test public void testGetMeanFailuresLowRate() throws Exception { assertEquals(1, history.getMean(), 0.01); history .failure(); // need an extra now that we can't determine the time between the first and // previous history.failure(); assertEquals(average(0), history.getMean(), 0.01); history.failure(); assertEquals(average(0, 0), history.getMean(), 0.01); history.success(); assertEquals(average(0, 0, 0.33), history.getMean(), 0.1); }
@Test public void testGetMean() throws Exception { assertEquals(1, history.getMean(), 0.01); history.success(); assertEquals(1, history.getMean(), 0.01); history.success(); assertEquals(1, history.getMean(), 0.01); history.success(); assertEquals(1, history.getMean(), 0.01); }
@Test public void testDecayedMean() throws Exception { history.failure(System.nanoTime() - 200000000); assertEquals(average(0, Math.exp(-0.4)), history.getMean(), 0.01); }
@Test public void testGetEarlyFailure() throws Exception { assertEquals(1, history.getMean(), 0.01); history.failure(); assertEquals(0, history.getMean(), 0.01); }
@Test public void testGetCount() { assertEquals(0, history.getCount()); history.success(); assertEquals(1, history.getCount()); }
@Test public void testReset() throws Exception { assertEquals(0, history.getStandardDeviation(), 0.01); history.success(); history.failure(); assertThat(history.getStandardDeviation(), not(equalTo(0))); history.reset(); assertEquals(0, history.getStandardDeviation(), 0.01); assertEquals(0, history.getCount()); assertEquals(0, history.getTimeSinceLastMeasurement(), 0.01); assertEquals(1, history.getMean(), 0.01); assertEquals(0, history.getMin(), 0.01); assertEquals(0, history.getMax(), 0.01); history.success(); assertEquals(1, history.getMin(), 0.01); }
@Test public void testGetStandardDeviation() throws Exception { assertEquals(0, history.getStandardDeviation(), 0.01); history.success(); assertEquals(0, history.getStandardDeviation(), 1); }