ElapsedTimeRecorder { public static long elapsedAtPercentile(ElapsedTimeRecorder that, double pct) { if (pct>1) { throw new UnsupportedOperationException("pct should be entered as a value between 0 and 1 where 1 represents 100% and .5 represents 50%"); } long targetCount = (long)Math.rint(pct * that.totalCount); if (targetCount==that.totalCount) { return that.maxValue; } if (0 != targetCount) { int i = 0; while (i<that.buckets.length-1) { long sum = that.sums[i]; int b = that.buckets[i++]; if (targetCount<=b) { long floor = i<=1 ? 0 : (1L<<(i-2)); long avg = sum/(long)b; long dif; int half = b>>1; if (half>1) { long center = avg-floor; if (targetCount<half) { dif = (center * targetCount)/half ; } else { dif = center+(((floor-center) * (targetCount-half))/half) ; } } else { dif = ((2*floor) * targetCount)/(long)b ; } return Math.min(that.maxValue, floor + dif); } else { targetCount -= b; } } return that.maxValue; } else { return 0; } } ElapsedTimeRecorder(); String toString(); static long totalCount(ElapsedTimeRecorder that); A report(A target); static void record(ElapsedTimeRecorder that, long valueNS); static long elapsedAtPercentile(ElapsedTimeRecorder that, double pct); void add(ElapsedTimeRecorder source); static void clear(ElapsedTimeRecorder that);  }
@Test public void simpleEmpty() { ElapsedTimeRecorder etr = new ElapsedTimeRecorder(); assertEquals(0, etr.elapsedAtPercentile(etr, .5f)); }