BucketTimer extends AbstractMonitor<Long> implements Timer, CompositeMonitor<Long>, SpectatorMonitor { @Override public void record(long duration) { totalTime.increment(duration); min.update(duration); max.update(duration); final long[] buckets = bucketConfig.getBuckets(); final long bucketDuration = bucketConfig.getTimeUnit().convert(duration, timeUnit); for (int i = 0; i < buckets.length; i++) { if (bucketDuration <= buckets[i]) { bucketCount[i].increment(); return; } } overflowCount.increment(); } BucketTimer(MonitorConfig config, BucketConfig bucketConfig);  BucketTimer(MonitorConfig config, BucketConfig bucketConfig, TimeUnit unit);   BucketTimer(MonitorConfig config, BucketConfig bucketConfig, TimeUnit unit, Clock clock); @Override void initializeSpectator(TagList tags); @Override List<Monitor<?>> getMonitors(); @Override Stopwatch start(); @Override TimeUnit getTimeUnit(); @Override void record(long duration); @Override void record(long duration, TimeUnit unit); @Override Long getValue(int pollerIndex); Long getTotalTime(); Long getCount(int pollerIndex); Long getMin(int pollerIndex); Long getMax(int pollerIndex); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString();  }
@Test public void testRecord() throws Exception { BucketTimer c = newInstance("foo"); Map<String, Number> expectedValues; expectedValues = new HashMap<>(); expectedValues.put("totalTime", 0L); expectedValues.put("min", 0L); expectedValues.put("max", 0L); expectedValues.put("bucket=10ms", 0L); expectedValues.put("bucket=20ms", 0L); expectedValues.put("bucket=overflow", 0L); assertMonitors(c.getMonitors(), expectedValues); assertEquals(c.getCount(0).longValue(), 0L); c.record(40); expectedValues = new HashMap<>(); expectedValues.put("totalTime", 40L); expectedValues.put("min", 40L); expectedValues.put("max", 40L); expectedValues.put("bucket=10ms", 0L); expectedValues.put("bucket=20ms", 0L); expectedValues.put("bucket=overflow", 1L); assertMonitors(c.getMonitors(), expectedValues); assertEquals(c.getCount(0).longValue(), 1L); c.record(10); expectedValues = new HashMap<>(); expectedValues.put("totalTime", 50L); expectedValues.put("min", 10L); expectedValues.put("max", 40L); expectedValues.put("bucket=10ms", 1L); expectedValues.put("bucket=20ms", 0L); expectedValues.put("bucket=overflow", 1L); assertMonitors(c.getMonitors(), expectedValues); assertEquals(c.getCount(0).longValue(), 2L); c.record(5); expectedValues = new HashMap<>(); expectedValues.put("totalTime", 55L); expectedValues.put("min", 5L); expectedValues.put("max", 40L); expectedValues.put("bucket=10ms", 2L); expectedValues.put("bucket=20ms", 0L); expectedValues.put("bucket=overflow", 1L); assertMonitors(c.getMonitors(), expectedValues); assertEquals(c.getCount(0).longValue(), 3L); c.record(20); expectedValues = new HashMap<>(); expectedValues.put("totalTime", 75L); expectedValues.put("min", 5L); expectedValues.put("max", 40L); expectedValues.put("bucket=10ms", 2L); expectedValues.put("bucket=20ms", 1L); expectedValues.put("bucket=overflow", 1L); assertMonitors(c.getMonitors(), expectedValues); assertEquals(c.getCount(0).longValue(), 4L); c.record(125); expectedValues = new HashMap<>(); expectedValues.put("totalTime", 200L); expectedValues.put("min", 5L); expectedValues.put("max", 125L); expectedValues.put("bucket=10ms", 2L); expectedValues.put("bucket=20ms", 1L); expectedValues.put("bucket=overflow", 2L); assertMonitors(c.getMonitors(), expectedValues); assertEquals(c.getCount(0).longValue(), 5L); }
@Test public void testEqualsCount() throws Exception { BucketTimer c1 = newInstance("foo"); BucketTimer c2 = newInstance("foo"); assertEquals(c1, c2); c1.record(42); assertNotEquals(c1, c2); c2.record(42); assertEquals(c1, c2); c1.record(11); assertNotEquals(c1, c2); c2.record(11); assertEquals(c1, c2); }