PercentileTracker { public synchronized void update(double sample) { long now = System.currentTimeMillis(); removeOutOfTimeWindowEntries(); cslm.put(now, sample); } PercentileTracker(long windowMillis); synchronized void update(double sample); double getPercentile(int requestedPercentile);  }
@Test public void testUpdate() { PercentileTracker pt = new PercentileTracker(5000); for (int i = 0; i < 11; i++) { pt.update((double)i); try { sleep(10); } catch (InterruptedException e) { fail(); } } double value = pt.getPercentile(95); Assert.assertEquals(9.5d, value, 0); }
@Test public void testPTWholeListOutsideWindow() { PercentileTracker pt = new PercentileTracker(200); for (int i = 0; i < 11; i++) { pt.update((double)i); try { sleep(10); } catch (InterruptedException e) { fail(); } } Assert.assertEquals(pt.cslm.entrySet().size(), 11); try { sleep(300); } catch (InterruptedException e) { fail(); } pt.update(1d); }