public void add(final int val) { recentDesiredResizes.add(val); }
public List<TimestampedValue<T>> getValues() { return getValues(System.currentTimeMillis()); }
public synchronized List<TimestampedValue<T>> getValues(long now) { pruneValues(now); return ImmutableList.copyOf(values); }
@Test public void testGetsSubSetOfRecentVals() { TimeWindowedList list = new TimeWindowedList<Object>(Duration.ONE_SECOND); list.add("a", 0L); list.add("b", 100L); assertEquals(list.getValuesInWindow(100L, HUNDRED_MILLISECONDS), timestampedValues("a", 0L, "b", 100L)); assertEquals(list.getValuesInWindow(101L, ONE_MILLISECOND), timestampedValues("b", 100L)); }
@Test public void testKeepsOnlyRecentVals() { TimeWindowedList list = new TimeWindowedList<Object>(MutableMap.of("timePeriod", 1000L)); list.add("a", 0L); list.add("b", 100L); assertEquals(list.getValues(1000L), timestampedValues("a", 0L, "b", 100L)); assertEquals(list.getValues(1100L), timestampedValues("b", 100L)); assertEquals(list.getValues(1101L), Lists.newArrayList()); }
@Test public void testGetsWindowWithMinExpiredWhenEmpty() { TimeWindowedList list = new TimeWindowedList<Object>(MutableMap.of("timePeriod", 1L, "minExpiredVals", 1)); assertEquals(list.getValuesInWindow(1000L, TEN_MILLISECONDS), timestampedValues()); }
public SizeHistory(long windowSize) { recentDesiredResizes = new TimeWindowedList<Number>(MutableMap.of("timePeriod", windowSize, "minExpiredVals", 1)); }
/** * Summarises the history of values in this time window, with a few special things: * <ul> * <li>If entire time-window is not covered by the given values, then min is Integer.MIN_VALUE and max is Integer.MAX_VALUE * <li>If no values, then latest is -1 * <li>If no recent values, then keeps last-seen value (no matter how old), to use that * <li>"stable for growth" means that since that max value, there have not been any higher values * <li>"stable for shrinking" means that since that low value, there have not been any lower values * </ul> */ public WindowSummary summarizeWindow(Duration windowSize) { long now = System.currentTimeMillis(); List<TimestampedValue<Number>> windowVals = recentDesiredResizes.getValuesInWindow(now, windowSize); Number latestObj = latestInWindow(windowVals); long latest = (latestObj == null) ? -1: latestObj.longValue(); long max = maxInWindow(windowVals, windowSize).longValue(); long min = minInWindow(windowVals, windowSize).longValue(); // TODO Could do more sophisticated "stable" check; this is the easiest code - correct but not most efficient // in terms of the caller having to schedule additional stability checks. boolean stable = (min == max); return new WindowSummary(latest, min, max, stable, stable); }
public void setWindowSize(Duration newWindowSize) { recentDesiredResizes.setTimePeriod(newWindowSize); }
@Test public void testGetsWindowWithMinValsWhenExpired() { TimeWindowedList list = new TimeWindowedList<Object>(MutableMap.of("timePeriod", 1L, "minVals", 1)); list.add("a", 0L); list.add("b", 1L); assertEquals(list.getValuesInWindow(1000L, TEN_MILLISECONDS), timestampedValues("b", 1L)); }
@Test public void testKeepsMinVals() { TimeWindowedList list = new TimeWindowedList<Object>(MutableMap.of("timePeriod", 1L, "minVals", 2)); assertEquals(list.getValues(2L), timestampedValues()); list.add("a", 0L); assertEquals(list.getValues(2L), timestampedValues("a", 0L)); list.add("b", 100L); assertEquals(list.getValues(102L), timestampedValues("a", 0L, "b", 100L)); list.add("c", 200L); assertEquals(list.getValues(202L), timestampedValues("b", 100L, "c", 200L)); }
@Test public void testGetsWindowWithMinWhenEmpty() { TimeWindowedList list = new TimeWindowedList<Object>(MutableMap.of("timePeriod", 1L, "minVals", 1)); assertEquals(list.getValuesInWindow(1000L, TEN_MILLISECONDS), timestampedValues()); }
@Test public void testGetsSubSetOfValsIncludingOneMinExpiredVal() { TimeWindowedList list = new TimeWindowedList<Object>(MutableMap.of("timePeriod", 1000L, "minExpiredVals", 1)); list.add("a", 0L); list.add("b", 100L); assertEquals(list.getValuesInWindow(100L, HUNDRED_MILLISECONDS), timestampedValues("a", 0L, "b", 100L)); assertEquals(list.getValuesInWindow(101L, TWO_MILLISECONDS), timestampedValues("a", 0L, "b", 100L)); assertEquals(list.getValuesInWindow(102L, ONE_MILLISECOND), timestampedValues("b", 100L)); assertEquals(list.getValuesInWindow(1001L, ONE_MILLISECOND), timestampedValues("b", 100L)); }
@Test public void testKeepsMinExpiredVals() { TimeWindowedList list = new TimeWindowedList<Object>(MutableMap.of("timePeriod", 1000L, "minExpiredVals", 1)); list.add("a", 0L); list.add("b", 100L); assertEquals(list.getValues(1001L), timestampedValues("a", 0L, "b", 100L)); assertEquals(list.getValues(1101L), timestampedValues("b", 100L)); }
public void add(T val) { add(val, System.currentTimeMillis()); }
public synchronized void add(T val, long timestamp) { values.add(values.size(), new TimestampedValue<T>(val, timestamp)); pruneValues(timestamp); }
@Test public void testZeroSizeWindowWithOneExpiredContainsOnlyMostRecentValue() { TimeWindowedList list = new TimeWindowedList<Object>(MutableMap.of("timePeriod", 0L, "minExpiredVals", 1)); list.add("a", 0L); assertEquals(list.getValuesInWindow(0L, HUNDRED_MILLISECONDS), timestampedValues("a", 0L)); assertEquals(list.getValuesInWindow(2L, ONE_MILLISECOND), timestampedValues("a", 0L)); list.add("b", 100L); assertEquals(list.getValuesInWindow(100L, ONE_MILLISECOND), timestampedValues("b", 100L)); assertEquals(list.getValuesInWindow(102L, ONE_MILLISECOND), timestampedValues("b", 100L)); }