ConsoleReporter extends ScheduledReporter { @Override @SuppressWarnings("rawtypes") public void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters, SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters, SortedMap<String, Timer> timers) { final String dateTime = dateFormat.format(new Date(clock.getTime())); printWithBanner(dateTime, '='); output.println(); if (!gauges.isEmpty()) { printWithBanner("-- Gauges", '-'); for (Map.Entry<String, Gauge> entry : gauges.entrySet()) { output.println(entry.getKey()); printGauge(entry.getValue()); } output.println(); } if (!counters.isEmpty()) { printWithBanner("-- Counters", '-'); for (Map.Entry<String, Counter> entry : counters.entrySet()) { output.println(entry.getKey()); printCounter(entry); } output.println(); } if (!histograms.isEmpty()) { printWithBanner("-- Histograms", '-'); for (Map.Entry<String, Histogram> entry : histograms.entrySet()) { output.println(entry.getKey()); printHistogram(entry.getValue()); } output.println(); } if (!meters.isEmpty()) { printWithBanner("-- Meters", '-'); for (Map.Entry<String, Meter> entry : meters.entrySet()) { output.println(entry.getKey()); printMeter(entry.getValue()); } output.println(); } if (!timers.isEmpty()) { printWithBanner("-- Timers", '-'); for (Map.Entry<String, Timer> entry : timers.entrySet()) { output.println(entry.getKey()); printTimer(entry.getValue()); } output.println(); } output.println(); output.flush(); } private  ConsoleReporter(MetricRegistry registry,
                            PrintStream output,
                            Locale locale,
                            Clock clock,
                            TimeZone timeZone,
                            TimeUnit rateUnit,
                            TimeUnit durationUnit,
                            MetricFilter filter,
                            ScheduledExecutorService executor,
                            boolean shutdownExecutorOnStop,
                            Set<MetricAttribute> disabledMetricAttributes); static Builder forRegistry(MetricRegistry registry); @Override @SuppressWarnings("rawtypes") void report(SortedMap<String, Gauge> gauges,
                       SortedMap<String, Counter> counters,
                       SortedMap<String, Histogram> histograms,
                       SortedMap<String, Meter> meters,
                       SortedMap<String, Timer> timers);  }
@Test public void reportsGaugeValues() throws Exception { final Gauge<Integer> gauge = () -> 1; reporter.report(map("gauge", gauge), map(), map(), map(), map()); assertThat(consoleOutput()) .isEqualTo(lines( dateHeader, "", "-- Gauges ----------------------------------------------------------------------", "gauge", " value = 1", "", "" )); }
@Test public void reportsCounterValues() throws Exception { final Counter counter = mock(Counter.class); when(counter.getCount()).thenReturn(100L); reporter.report(map(), map("test.counter", counter), map(), map(), map()); assertThat(consoleOutput()) .isEqualTo(lines( dateHeader, "", "-- Counters --------------------------------------------------------------------", "test.counter", " count = 100", "", "" )); }
@Test public void reportsHistogramValues() throws Exception { final Histogram histogram = mock(Histogram.class); when(histogram.getCount()).thenReturn(1L); final Snapshot snapshot = mock(Snapshot.class); when(snapshot.getMax()).thenReturn(2L); when(snapshot.getMean()).thenReturn(3.0); when(snapshot.getMin()).thenReturn(4L); when(snapshot.getStdDev()).thenReturn(5.0); when(snapshot.getMedian()).thenReturn(6.0); when(snapshot.get75thPercentile()).thenReturn(7.0); when(snapshot.get95thPercentile()).thenReturn(8.0); when(snapshot.get98thPercentile()).thenReturn(9.0); when(snapshot.get99thPercentile()).thenReturn(10.0); when(snapshot.get999thPercentile()).thenReturn(11.0); when(histogram.getSnapshot()).thenReturn(snapshot); reporter.report(map(), map(), map("test.histogram", histogram), map(), map()); assertThat(consoleOutput()) .isEqualTo(lines( dateHeader, "", "-- Histograms ------------------------------------------------------------------", "test.histogram", " count = 1", " min = 4", " max = 2", " mean = 3.00", " stddev = 5.00", " median = 6.00", " 75% <= 7.00", " 95% <= 8.00", " 98% <= 9.00", " 99% <= 10.00", " 99.9% <= 11.00", "", "" )); }
@Test public void reportsMeterValues() throws Exception { final Meter meter = mock(Meter.class); when(meter.getCount()).thenReturn(1L); when(meter.getMeanRate()).thenReturn(2.0); when(meter.getOneMinuteRate()).thenReturn(3.0); when(meter.getFiveMinuteRate()).thenReturn(4.0); when(meter.getFifteenMinuteRate()).thenReturn(5.0); reporter.report(map(), map(), map(), map("test.meter", meter), map()); assertThat(consoleOutput()) .isEqualTo(lines( dateHeader, "", "-- Meters ----------------------------------------------------------------------", "test.meter", " count = 1", " mean rate = 2.00 events/second", " 1-minute rate = 3.00 events/second", " 5-minute rate = 4.00 events/second", " 15-minute rate = 5.00 events/second", "", "" )); }
@Test public void reportsTimerValues() throws Exception { final Timer timer = mock(Timer.class); when(timer.getCount()).thenReturn(1L); when(timer.getMeanRate()).thenReturn(2.0); when(timer.getOneMinuteRate()).thenReturn(3.0); when(timer.getFiveMinuteRate()).thenReturn(4.0); when(timer.getFifteenMinuteRate()).thenReturn(5.0); final Snapshot snapshot = mock(Snapshot.class); when(snapshot.getMax()).thenReturn(TimeUnit.MILLISECONDS.toNanos(100)); when(snapshot.getMean()).thenReturn((double) TimeUnit.MILLISECONDS.toNanos(200)); when(snapshot.getMin()).thenReturn(TimeUnit.MILLISECONDS.toNanos(300)); when(snapshot.getStdDev()).thenReturn((double) TimeUnit.MILLISECONDS.toNanos(400)); when(snapshot.getMedian()).thenReturn((double) TimeUnit.MILLISECONDS.toNanos(500)); when(snapshot.get75thPercentile()).thenReturn((double) TimeUnit.MILLISECONDS.toNanos(600)); when(snapshot.get95thPercentile()).thenReturn((double) TimeUnit.MILLISECONDS.toNanos(700)); when(snapshot.get98thPercentile()).thenReturn((double) TimeUnit.MILLISECONDS.toNanos(800)); when(snapshot.get99thPercentile()).thenReturn((double) TimeUnit.MILLISECONDS.toNanos(900)); when(snapshot.get999thPercentile()).thenReturn((double) TimeUnit.MILLISECONDS .toNanos(1000)); when(timer.getSnapshot()).thenReturn(snapshot); reporter.report(map(), map(), map(), map(), map("test.another.timer", timer)); assertThat(consoleOutput()) .isEqualTo(lines( dateHeader, "", "-- Timers ----------------------------------------------------------------------", "test.another.timer", " count = 1", " mean rate = 2.00 calls/second", " 1-minute rate = 3.00 calls/second", " 5-minute rate = 4.00 calls/second", " 15-minute rate = 5.00 calls/second", " min = 300.00 milliseconds", " max = 100.00 milliseconds", " mean = 200.00 milliseconds", " stddev = 400.00 milliseconds", " median = 500.00 milliseconds", " 75% <= 600.00 milliseconds", " 95% <= 700.00 milliseconds", " 98% <= 800.00 milliseconds", " 99% <= 900.00 milliseconds", " 99.9% <= 1000.00 milliseconds", "", "" )); }