Java Code Examples for org.eclipse.microprofile.metrics.Timer#update()

The following examples show how to use org.eclipse.microprofile.metrics.Timer#update() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExportersMetricScalingTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
/**
 * Given a Timer with unit=MINUTES,
 * check that the statistics from OpenMetricsExporter will be correctly converted to SECONDS.
 */
@Test
public void timer_openMetrics() {
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    Metadata metadata = Metadata.builder()
            .withName("timer1")
            .withType(MetricType.TIMER)
            .withUnit(MetricUnits.MINUTES)
            .build();
    Timer metric = registry.timer(metadata);
    metric.update(Duration.ofHours(1));
    metric.update(Duration.ofHours(2));
    metric.update(Duration.ofHours(3));

    OpenMetricsExporter exporter = new OpenMetricsExporter();
    String exported = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("timer1")).toString();

    Assert.assertThat(exported, containsString("application_timer1_seconds{quantile=\"0.5\"} 7200.0"));
    Assert.assertThat(exported, containsString("application_timer1_mean_seconds 7200.0"));
    Assert.assertThat(exported, containsString("application_timer1_min_seconds 3600.0"));
    Assert.assertThat(exported, containsString("application_timer1_max_seconds 10800.0"));
}
 
Example 2
Source File: ExportersMetricScalingTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
/**
 * Given a Timer with unit=MINUTES,
 * check that the statistics from JsonExporter will be presented in MINUTES.
 */
@Test
public void timer_json() {
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    Metadata metadata = Metadata.builder()
            .withName("timer1")
            .withType(MetricType.TIMER)
            .withUnit(MetricUnits.MINUTES)
            .build();
    Timer metric = registry.timer(metadata);
    metric.update(Duration.ofHours(1));
    metric.update(Duration.ofHours(2));
    metric.update(Duration.ofHours(3));

    JsonExporter exporter = new JsonExporter();
    String exported = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("timer1")).toString();

    JsonObject json = Json.createReader(new StringReader(exported)).read().asJsonObject().getJsonObject("timer1");
    assertEquals(120.0, json.getJsonNumber("p50").doubleValue(), 0.001);
    assertEquals(120.0, json.getJsonNumber("mean").doubleValue(), 0.001);
    assertEquals(60.0, json.getJsonNumber("min").doubleValue(), 0.001);
    assertEquals(180.0, json.getJsonNumber("max").doubleValue(), 0.001);
    assertEquals(360.0, json.getJsonNumber("elapsedTime").doubleValue(), 0.001);
}
 
Example 3
Source File: ExportersMetricScalingTest.java    From smallrye-metrics with Apache License 2.0 6 votes vote down vote up
/**
 * Given a Histogram with unit=MINUTES,
 * check that the statistics from JsonExporter will be presented in MINUTES.
 */
@Test
public void histogram_json() {
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    Metadata metadata = Metadata.builder()
            .withName("timer1")
            .withType(MetricType.TIMER)
            .withUnit(MetricUnits.MINUTES)
            .build();
    Timer metric = registry.timer(metadata);
    metric.update(Duration.ofHours(1));
    metric.update(Duration.ofHours(2));
    metric.update(Duration.ofHours(3));

    JsonExporter exporter = new JsonExporter();
    String exported = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("timer1")).toString();

    JsonObject json = Json.createReader(new StringReader(exported)).read().asJsonObject().getJsonObject("timer1");
    assertEquals(120.0, json.getJsonNumber("p50").doubleValue(), 0.001);
    assertEquals(120.0, json.getJsonNumber("mean").doubleValue(), 0.001);
    assertEquals(60.0, json.getJsonNumber("min").doubleValue(), 0.001);
    assertEquals(180.0, json.getJsonNumber("max").doubleValue(), 0.001);
}
 
Example 4
Source File: TimerTest.java    From microprofile-metrics with Apache License 2.0 5 votes vote down vote up
@Test
@InSequence(1)
public void testRate() throws Exception {

    int markSeconds = 30;
    int delaySeconds = 15;

    Timer timer = registry.timer("testRate");

    // Call update ~1/sec
    for (int i = 0; i < markSeconds; i++) {
        timer.update(Duration.ofSeconds(1));
        Thread.sleep(1000);
    }

    // All rates should be around the value of ~1/sec
    TestUtils.assertEqualsWithTolerance(1.0, timer.getMeanRate());
    TestUtils.assertEqualsWithTolerance(1.0, timer.getOneMinuteRate());
    TestUtils.assertEqualsWithTolerance(1.0, timer.getFiveMinuteRate());
    TestUtils.assertEqualsWithTolerance(1.0, timer.getFifteenMinuteRate());

    Thread.sleep(delaySeconds * 1000);

    // Approximately calculate what the expected mean should be
    // and let the tolerance account for the delta
    double expectedMean = ((double) markSeconds/(markSeconds + delaySeconds));
    TestUtils.assertEqualsWithTolerance(expectedMean, timer.getMeanRate());

    // After a delay, we expect some decay of values
    Assert.assertThat(timer.getOneMinuteRate(), lessThan(1.0));
    Assert.assertThat(timer.getFiveMinuteRate(), lessThan(1.0));
    Assert.assertThat(timer.getFifteenMinuteRate(), lessThan(1.0));
}
 
Example 5
Source File: OpenMetricsExporterTest.java    From smallrye-metrics with Apache License 2.0 4 votes vote down vote up
@Test
public void exportTimers() {
    OpenMetricsExporter exporter = new OpenMetricsExporter();
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);

    Metadata metadata = Metadata
            .builder()
            .withType(MetricType.TIMER)
            .withName("mytimer")
            .withDescription("awesome")
            .build();
    Tag blueTag = new Tag("color", "blue");
    Timer blueTimer = registry.timer(metadata, blueTag);
    Tag greenTag = new Tag("color", "green");
    Timer greenTimer = registry.timer(metadata, greenTag);

    blueTimer.update(Duration.ofSeconds(3));
    blueTimer.update(Duration.ofSeconds(4));
    greenTimer.update(Duration.ofSeconds(5));
    greenTimer.update(Duration.ofSeconds(6));

    String result = exporter.exportMetricsByName(MetricRegistry.Type.APPLICATION, "mytimer").toString();
    System.out.println(result);

    assertHasTypeLineExactlyOnce(result, "application_mytimer_seconds", "summary");
    assertHasHelpLineExactlyOnce(result, "application_mytimer_seconds", "awesome");

    assertHasValueLineExactlyOnce(result, "application_mytimer_elapsedTime_seconds", "7.0", blueTag);
    assertHasValueLineExactlyOnce(result, "application_mytimer_elapsedTime_seconds", "11.0", greenTag);

    assertHasValueLineExactlyOnce(result, "application_mytimer_seconds_count", "2.0", blueTag);
    assertHasValueLineExactlyOnce(result, "application_mytimer_seconds_count", "2.0", greenTag);

    assertHasTypeLineExactlyOnce(result, "application_mytimer_min_seconds", "gauge");
    assertHasValueLineExactlyOnce(result, "application_mytimer_min_seconds", "3.0", blueTag);
    assertHasValueLineExactlyOnce(result, "application_mytimer_min_seconds", "5.0", greenTag);

    assertHasTypeLineExactlyOnce(result, "application_mytimer_max_seconds", "gauge");
    assertHasValueLineExactlyOnce(result, "application_mytimer_max_seconds", "4.0", blueTag);
    assertHasValueLineExactlyOnce(result, "application_mytimer_max_seconds", "6.0", greenTag);

    assertHasTypeLineExactlyOnce(result, "application_mytimer_mean_seconds", "gauge");
    assertHasValueLineExactlyOnce(result, "application_mytimer_mean_seconds", "3.5", blueTag);
    assertHasValueLineExactlyOnce(result, "application_mytimer_mean_seconds", "5.5", greenTag);

    assertHasTypeLineExactlyOnce(result, "application_mytimer_stddev_seconds", "gauge");
    assertHasValueLineExactlyOnce(result, "application_mytimer_stddev_seconds", "0.5", blueTag);
    assertHasValueLineExactlyOnce(result, "application_mytimer_stddev_seconds", "0.5", greenTag);

    assertHasTypeLineExactlyOnce(result, "application_mytimer_rate_per_second", "gauge");
    assertHasValueLineExactlyOnce(result, "application_mytimer_rate_per_second", "*", blueTag);
    assertHasValueLineExactlyOnce(result, "application_mytimer_rate_per_second", "*", greenTag);

    assertHasTypeLineExactlyOnce(result, "application_mytimer_one_min_rate_per_second", "gauge");
    assertHasValueLineExactlyOnce(result, "application_mytimer_one_min_rate_per_second", "*", blueTag);
    assertHasValueLineExactlyOnce(result, "application_mytimer_one_min_rate_per_second", "*", greenTag);

    assertHasTypeLineExactlyOnce(result, "application_mytimer_five_min_rate_per_second", "gauge");
    assertHasValueLineExactlyOnce(result, "application_mytimer_five_min_rate_per_second", "*", blueTag);
    assertHasValueLineExactlyOnce(result, "application_mytimer_five_min_rate_per_second", "*", greenTag);

    assertHasTypeLineExactlyOnce(result, "application_mytimer_fifteen_min_rate_per_second", "gauge");
    assertHasValueLineExactlyOnce(result, "application_mytimer_fifteen_min_rate_per_second", "*", blueTag);
    assertHasValueLineExactlyOnce(result, "application_mytimer_fifteen_min_rate_per_second", "*", greenTag);

    assertHasValueLineExactlyOnce(result, "application_mytimer_seconds", "4.0", blueTag, QUANTILE_0_5);
    assertHasValueLineExactlyOnce(result, "application_mytimer_seconds", "4.0", blueTag, QUANTILE_0_75);
    assertHasValueLineExactlyOnce(result, "application_mytimer_seconds", "4.0", blueTag, QUANTILE_0_95);
    assertHasValueLineExactlyOnce(result, "application_mytimer_seconds", "4.0", blueTag, QUANTILE_0_98);
    assertHasValueLineExactlyOnce(result, "application_mytimer_seconds", "4.0", blueTag, QUANTILE_0_99);
    assertHasValueLineExactlyOnce(result, "application_mytimer_seconds", "4.0", blueTag, QUANTILE_0_999);

    assertHasValueLineExactlyOnce(result, "application_mytimer_seconds", "6.0", greenTag, QUANTILE_0_5);
    assertHasValueLineExactlyOnce(result, "application_mytimer_seconds", "6.0", greenTag, QUANTILE_0_75);
    assertHasValueLineExactlyOnce(result, "application_mytimer_seconds", "6.0", greenTag, QUANTILE_0_95);
    assertHasValueLineExactlyOnce(result, "application_mytimer_seconds", "6.0", greenTag, QUANTILE_0_98);
    assertHasValueLineExactlyOnce(result, "application_mytimer_seconds", "6.0", greenTag, QUANTILE_0_99);
    assertHasValueLineExactlyOnce(result, "application_mytimer_seconds", "6.0", greenTag, QUANTILE_0_999);
}
 
Example 6
Source File: JsonExporterTest.java    From smallrye-metrics with Apache License 2.0 4 votes vote down vote up
@Test
public void testTimers() {
    JsonExporter exporter = new JsonExporter();
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    Metadata metadata = new MetadataBuilder()
            .withUnit(MetricUnits.SECONDS)
            .withName("mytimer")
            .build();

    Timer timerWithoutTags = registry.timer(metadata);
    Timer timerRed = registry.timer(metadata, new Tag("color", "red"));
    Timer timerBlue = registry.timer(metadata, new Tag("color", "blue"), new Tag("foo", "bar"));

    timerWithoutTags.update(Duration.ofSeconds(1));
    timerRed.update(Duration.ofSeconds(2));
    timerBlue.update(Duration.ofSeconds(3));

    String result = exporter.exportMetricsByName(MetricRegistry.Type.APPLICATION, "mytimer").toString();
    JsonObject json = Json.createReader(new StringReader(result)).read().asJsonObject();

    JsonObject mytimerObject = json.getJsonObject("mytimer");

    assertEquals(1.0, mytimerObject.getJsonNumber("count").doubleValue(), 1e-10);
    assertEquals(1.0, mytimerObject.getJsonNumber("count;color=red").doubleValue(), 1e-10);
    assertEquals(1.0, mytimerObject.getJsonNumber("count;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("elapsedTime").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("elapsedTime;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("elapsedTime;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("p50").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("p50;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("p50;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("p75").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("p75;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("p75;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("p95").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("p95;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("p95;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("p98").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("p98;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("p98;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("p99").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("p99;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("p99;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("p999").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("p999;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("p999;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("min").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("min;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("min;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("mean").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("mean;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("mean;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("max").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("max;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("max;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(0.0, mytimerObject.getJsonNumber("stddev").doubleValue(), 1e-10);
    assertEquals(0.0, mytimerObject.getJsonNumber("stddev;color=red").doubleValue(), 1e-10);
    assertEquals(0.0, mytimerObject.getJsonNumber("stddev;color=blue;foo=bar").doubleValue(), 1e-10);
}