BucketedTimeDistribution extends Bucketed<BucketedTimeDistribution.Distribution> { public void add(final long value) { applyToCurrentBucket((Function<Distribution, Void>) input -> { synchronized (input) { input.digest.add(value); input.total += value; } return null; }); }  void add(final long value);  }
@Test public void testInitialState() throws Exception { BucketedTimeDistribution distribution = new BucketedTimeDistribution(); distribution.add(1); assertPreviousDistributionEmpty(distribution); }
@Test public void testWithBucketIdProvider() throws Exception { BucketedTimeDistribution distribution = new BucketedTimeDistribution(); distribution.setBucketIdProvider(bucketIdProvider); distribution.add(1_000_000); assertPreviousDistributionEmpty(distribution); ++bucketIdProvider.id; distribution.add(2_000_000); distribution.add(3_000_000); assertPreviousDistribution(distribution, 1, .001, .001, .001); ++bucketIdProvider.id; assertPreviousDistribution(distribution, 2, .002, .003, .005); }
@Test public void testDiscardsBuckets() throws Exception { BucketedTimeDistribution distribution = new BucketedTimeDistribution(); distribution.add(100_000_000); distribution.setBucketIdProvider(bucketIdProvider); distribution.add(1_000_000); assertPreviousDistributionEmpty(distribution); ++bucketIdProvider.id; distribution.add(2_000_000); distribution.add(3_000_000); assertPreviousDistribution(distribution, 1, .001, .001, .001); ++bucketIdProvider.id; ++bucketIdProvider.id; distribution.add(100_000_000); assertPreviousDistributionEmpty(distribution); ++bucketIdProvider.id; assertPreviousDistribution(distribution, 1, .1, .1, .1); distribution.add(200_000_000); ++bucketIdProvider.id; ++bucketIdProvider.id; assertPreviousDistributionEmpty(distribution); }