/** * Construct a {@link Timer} with given window size * * @param name name of this timer * @param windowMs the window size. unit is millisecond * @param clock the clock for the reservoir */ public Timer(String name, long windowMs, Clock clock) { this(name, new SlidingTimeWindowReservoir(windowMs, clock)); }
/** * Default constructor. It uses {@link SlidingTimeWindowReservoir} as the * default reservoir. * * @param name name of this timer */ public Timer(String name) { this(name, new SlidingTimeWindowReservoir()); }
/** * Construct a {@link Timer} with given window size and collision buffer * * @param name name of this timer * @param windowMs the window size. unit is millisecond * @param collisionBuffer amount of collisions allowed in one millisecond. * @param clock the clock for the reservoir */ public Timer(String name, long windowMs, int collisionBuffer, Clock clock) { this(name, new SlidingTimeWindowReservoir(windowMs, collisionBuffer, clock)); }
/** * Construct a {@link Timer} with given window size * * @param name name of this timer * @param windowMs the window size. unit is millisecond * @param clock the clock for the reservoir */ public Timer(String name, long windowMs, Clock clock) { this(name, new SlidingTimeWindowReservoir(windowMs, clock)); }
/** * Default constructor. It uses {@link SlidingTimeWindowReservoir} as the * default reservoir. * * @param name name of this timer */ public Timer(String name) { this(name, new SlidingTimeWindowReservoir()); }
/** * Construct a {@link Timer} with given window size and collision buffer * * @param name name of this timer * @param windowMs the window size. unit is millisecond * @param collisionBuffer amount of collisions allowed in one millisecond. * @param clock the clock for the reservoir */ public Timer(String name, long windowMs, int collisionBuffer, Clock clock) { this(name, new SlidingTimeWindowReservoir(windowMs, collisionBuffer, clock)); }
prefetchCount = config.getPrefetchCount(systemName); recentRetryAttempts = new SlidingTimeWindowReservoir(config.getRetryWindowMs(systemName), clock);
@Test public void testRemoveExpiredValues() { SlidingTimeWindowReservoir slidingTimeWindowReservoir = new SlidingTimeWindowReservoir(300, 8, clock); when(clock.currentTimeMillis()).thenReturn(0L); slidingTimeWindowReservoir.update(1L); when(clock.currentTimeMillis()).thenReturn(100L); slidingTimeWindowReservoir.update(2L); when(clock.currentTimeMillis()).thenReturn(301L); slidingTimeWindowReservoir.update(3L); when(clock.currentTimeMillis()).thenReturn(500L); slidingTimeWindowReservoir.update(4L); Snapshot snapshot = slidingTimeWindowReservoir.getSnapshot(); assertTrue(snapshot.getValues().containsAll(Arrays.asList(3L, 4L))); assertEquals(2, snapshot.getSize()); }
@Test public void testUpdateSizeSnapshot() { SlidingTimeWindowReservoir slidingTimeWindowReservoir = new SlidingTimeWindowReservoir(300, 8, clock); when(clock.currentTimeMillis()).thenReturn(0L); slidingTimeWindowReservoir.update(1L); when(clock.currentTimeMillis()).thenReturn(1L); slidingTimeWindowReservoir.update(2L); when(clock.currentTimeMillis()).thenReturn(2L); slidingTimeWindowReservoir.update(3L); assertEquals(3, slidingTimeWindowReservoir.size()); Snapshot snapshot = slidingTimeWindowReservoir.getSnapshot(); assertTrue(snapshot.getValues().containsAll(Arrays.asList(1L, 2L, 3L))); assertEquals(3, snapshot.getSize()); }
@Test public void testDuplicateTime() { SlidingTimeWindowReservoir slidingTimeWindowReservoir = new SlidingTimeWindowReservoir(300, 2, clock); when(clock.currentTimeMillis()).thenReturn(1L); slidingTimeWindowReservoir.update(1L); slidingTimeWindowReservoir.update(2L); Snapshot snapshot = slidingTimeWindowReservoir.getSnapshot(); assertTrue(snapshot.getValues().containsAll(Arrays.asList(1L, 2L))); assertEquals(2, snapshot.getSize()); // update causes collision, will override the last update slidingTimeWindowReservoir.update(3L); snapshot = slidingTimeWindowReservoir.getSnapshot(); assertTrue(snapshot.getValues().containsAll(Arrays.asList(1L, 3L))); assertEquals(2, snapshot.getSize()); }