SparseTimeStat extends Bucketed<SparseTimeStat.Distribution> { public <T> T time(Callable<T> callable) throws Exception { try (BlockTimer ignored = time()) { return callable.call(); } } SparseTimeStat();  SparseTimeStat(Ticker ticker); void add(long value, TimeUnit timeUnit); void add(double value, TimeUnit timeUnit); void add(Duration duration); T time(Callable<T> callable); BlockTimer time();  }
@Test public void testTimeCallable() throws Exception { SparseTimeStat stat = new SparseTimeStat(ticker); stat.setBucketIdProvider(bucketIdProvider); stat.time(() -> { ticker.elapseTime(10, TimeUnit.MILLISECONDS); return null; }); ++bucketIdProvider.id; assertPreviousDistribution(stat, 1, 0.010, 0.010, 0.010); }
@Test public void testTimeCallableException() throws Exception { SparseTimeStat stat = new SparseTimeStat(ticker); stat.setBucketIdProvider(bucketIdProvider); try { stat.time(() -> { ticker.elapseTime(10, TimeUnit.MILLISECONDS); throw new RuntimeException("test exception"); }); } catch (RuntimeException ignored) { } ++bucketIdProvider.id; assertPreviousDistribution(stat, 1, 0.010, 0.010, 0.010); }
@Test public void testTimeTry() throws Exception { SparseTimeStat stat = new SparseTimeStat(ticker); stat.setBucketIdProvider(bucketIdProvider); try (BlockTimer ignored = stat.time()) { ticker.elapseTime(10, TimeUnit.MILLISECONDS); } ++bucketIdProvider.id; assertPreviousDistribution(stat, 1, 0.010, 0.010, 0.010); }
@Test public void testTimeTryException() throws Exception { SparseTimeStat stat = new SparseTimeStat(ticker); stat.setBucketIdProvider(bucketIdProvider); try (BlockTimer ignored = stat.time()) { ticker.elapseTime(10, TimeUnit.MILLISECONDS); throw new Exception("test exception"); } catch (Exception ignored) { } ++bucketIdProvider.id; assertPreviousDistribution(stat, 1, 0.010, 0.010, 0.010); }
@Test public void testTimeTrySetTimer() throws Exception { SparseTimeStat oldStat = new SparseTimeStat(ticker); SparseTimeStat stat = new SparseTimeStat(ticker); stat.setBucketIdProvider(bucketIdProvider); try (BlockTimer timer = oldStat.time()) { ticker.elapseTime(10, TimeUnit.MILLISECONDS); timer.timeTo(stat); } ++bucketIdProvider.id; assertPreviousDistribution(stat, 1, 0.010, 0.010, 0.010); assertPreviousDistributionEmpty(oldStat); }