/** * Adds an old value with a fixed timestamp to the sample. * * @param value the value to be added * @param timestamp the epoch timestamp of {@code value} in seconds */ public void update(long value, long timestamp) { lockForRegularUsage(); try { final double priority = weight(timestamp - startTime) / random(); final long newCount = count.incrementAndGet(); if (newCount <= reservoirSize) { values.put(priority, value); } else { Double first = values.firstKey(); if (first < priority) { if (values.putIfAbsent(priority, value) == null) { // ensure we always remove an item while (values.remove(first) == null) { first = values.firstKey(); } } } } } finally { unlockForRegularUsage(); } final long now = System.nanoTime(); final long next = nextScaleTime.get(); if (now >= next) { rescale(now, next); } }
/** Stops execution time measurement. */ public void stop() { add(System.nanoTime() - startClock); }
/** Starts execution time measurement. */ public void start() { startClock = System.nanoTime(); }
public void clear() { values.clear(); count.set(0); this.startTime = tick(); nextScaleTime.set(System.nanoTime() + RESCALE_THRESHOLD); }
private static long tick() { return TimeUnit.NANOSECONDS.toSeconds(System.nanoTime()); }