Search in sources :

Example 1 with StatsTimer

use of com.netflix.servo.monitor.StatsTimer in project tutorials by eugenp.

the class MetricTypeTest method givenStatsTimer_whenExecuteTask_thenStatsCalculated.

@Test
public // ==
void givenStatsTimer_whenExecuteTask_thenStatsCalculated() throws Exception {
    System.setProperty("netflix.servo", "1000");
    StatsTimer timer = new StatsTimer(MonitorConfig.builder("test").build(), new StatsConfig.Builder().withComputeFrequencyMillis(2000).withPercentiles(new double[] { 99.0, 95.0, 90.0 }).withPublishMax(true).withPublishMin(true).withPublishCount(true).withPublishMean(true).withPublishStdDev(true).withPublishVariance(true).build(), MILLISECONDS);
    Stopwatch stopwatch = timer.start();
    MILLISECONDS.sleep(1);
    timer.record(3, MILLISECONDS);
    stopwatch.stop();
    stopwatch = timer.start();
    timer.record(6, MILLISECONDS);
    MILLISECONDS.sleep(2);
    stopwatch.stop();
    assertEquals("timer should count 12 milliseconds in total", 12, timer.getTotalTime());
    assertEquals("timer should count 12 milliseconds in total", 12, timer.getTotalMeasurement());
    assertEquals("timer should record 4 updates", 4, timer.getCount());
    assertEquals("stats timer value time-cost/update should be 2", 3, timer.getValue().intValue());
    final Map<String, Number> metricMap = timer.getMonitors().stream().collect(toMap(monitor -> getMonitorTagValue(monitor, "statistic"), monitor -> (Number) monitor.getValue()));
    assertThat(metricMap.keySet(), containsInAnyOrder("count", "totalTime", "max", "min", "variance", "stdDev", "avg", "percentile_99", "percentile_95", "percentile_90"));
}
Also used : Counter(com.netflix.servo.monitor.Counter) BucketTimer(com.netflix.servo.monitor.BucketTimer) StepCounter(com.netflix.servo.monitor.StepCounter) Monitor(com.netflix.servo.monitor.Monitor) Assert.assertThat(org.junit.Assert.assertThat) Collectors.toMap(java.util.stream.Collectors.toMap) StatsConfig(com.netflix.servo.stats.StatsConfig) BasicTimer(com.netflix.servo.monitor.BasicTimer) StatsTimer(com.netflix.servo.monitor.StatsTimer) Map(java.util.Map) BasicGauge(com.netflix.servo.monitor.BasicGauge) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) Gauge(com.netflix.servo.monitor.Gauge) MaxGauge(com.netflix.servo.monitor.MaxGauge) Matchers.allOf(org.hamcrest.Matchers.allOf) BasicCounter(com.netflix.servo.monitor.BasicCounter) Test(org.junit.Test) PeakRateCounter(com.netflix.servo.monitor.PeakRateCounter) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Ignore(org.junit.Ignore) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) MonitorConfig(com.netflix.servo.monitor.MonitorConfig) Stopwatch(com.netflix.servo.monitor.Stopwatch) Monitors(com.netflix.servo.monitor.Monitors) BasicInformational(com.netflix.servo.monitor.BasicInformational) BucketConfig(com.netflix.servo.monitor.BucketConfig) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Assert.assertEquals(org.junit.Assert.assertEquals) StatsTimer(com.netflix.servo.monitor.StatsTimer) Stopwatch(com.netflix.servo.monitor.Stopwatch) Test(org.junit.Test)

Aggregations

BasicCounter (com.netflix.servo.monitor.BasicCounter)1 BasicGauge (com.netflix.servo.monitor.BasicGauge)1 BasicInformational (com.netflix.servo.monitor.BasicInformational)1 BasicTimer (com.netflix.servo.monitor.BasicTimer)1 BucketConfig (com.netflix.servo.monitor.BucketConfig)1 BucketTimer (com.netflix.servo.monitor.BucketTimer)1 Counter (com.netflix.servo.monitor.Counter)1 Gauge (com.netflix.servo.monitor.Gauge)1 MaxGauge (com.netflix.servo.monitor.MaxGauge)1 Monitor (com.netflix.servo.monitor.Monitor)1 MonitorConfig (com.netflix.servo.monitor.MonitorConfig)1 Monitors (com.netflix.servo.monitor.Monitors)1 PeakRateCounter (com.netflix.servo.monitor.PeakRateCounter)1 StatsTimer (com.netflix.servo.monitor.StatsTimer)1 StepCounter (com.netflix.servo.monitor.StepCounter)1 Stopwatch (com.netflix.servo.monitor.Stopwatch)1 StatsConfig (com.netflix.servo.stats.StatsConfig)1 Map (java.util.Map)1 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)1 SECONDS (java.util.concurrent.TimeUnit.SECONDS)1