org.eclipse.microprofile.metrics.Timer Java Examples
The following examples show how to use
org.eclipse.microprofile.metrics.Timer.
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: MetricsRegistryImpl.java From smallrye-metrics with Apache License 2.0 | 6 votes |
private MetricType metricTypeFromClass(Class<?> in) { if (in.equals(Counter.class)) { return MetricType.COUNTER; } else if (in.equals(Gauge.class)) { return MetricType.GAUGE; } else if (in.equals(ConcurrentGauge.class)) { return MetricType.CONCURRENT_GAUGE; } else if (in.equals(Meter.class)) { return MetricType.METERED; } else if (in.equals(Timer.class)) { return MetricType.TIMER; } else if (in.equals(SimpleTimer.class)) { return MetricType.SIMPLE_TIMER; } else if (in.equals(Histogram.class)) { return MetricType.HISTOGRAM; } return null; }
Example #2
Source File: MetricRegistryTest.java From microprofile-metrics with Apache License 2.0 | 6 votes |
@Test @InSequence(2) public void registerTest() { metrics.register("regCountTemp", countTemp); assertExists(Counter.class, new MetricID("regCountTemp")); metrics.register("regHistoTemp", histoTemp); assertExists(Histogram.class, new MetricID("regHistoTemp")); metrics.register("regTimerTemp", timerTemp); assertExists(Timer.class, new MetricID("regTimerTemp")); metrics.register("regSimpleTimerTemp", simpleTimerTemp); assertExists(SimpleTimer.class, new MetricID("regSimpleTimerTemp")); metrics.register("regConcurrentGaugeTemp", concurrentGaugeTemp); assertExists(ConcurrentGauge.class, new MetricID("regConcurrentGaugeTemp")); metrics.register("regMeterTemp", meterTemp); assertExists(Meter.class, new MetricID("regMeterTemp")); }
Example #3
Source File: TagsTest.java From microprofile-metrics with Apache License 2.0 | 6 votes |
@Test @InSequence(5) public void timerTagsTest() { Tag tagEarth = new Tag("planet", "earth"); Tag tagRed = new Tag("colour", "red"); Tag tagBlue = new Tag("colour", "blue"); String timerName = "org.eclipse.microprofile.metrics.tck.TagTest.timerColour"; Timer timerColour = registry.timer(timerName); Timer timerRed = registry.timer(timerName,tagEarth,tagRed); Timer timerBlue = registry.timer(timerName,tagEarth,tagBlue); MetricID timerColourMID = new MetricID(timerName); MetricID timerRedMID = new MetricID(timerName, tagEarth,tagRed); MetricID timerBlueMID = new MetricID(timerName, tagEarth,tagBlue); //check multi-dimensional metrics are registered assertThat("Timer is not registered correctly", registry.getTimer(timerColourMID), notNullValue()); assertThat("Timer is not registered correctly", registry.getTimer(timerRedMID), notNullValue()); assertThat("Timer is not registered correctly", registry.getTimer(timerBlueMID), notNullValue()); }
Example #4
Source File: MetricProducerMethodBeanTest.java From microprofile-metrics with Apache License 2.0 | 6 votes |
@Test @InSequence(2) public void callCachedMethodMultipleTimes() { assertThat("Metrics are not registered correctly", registry.getMetricIDs(), contains(cacheHitsMID, callsMID, hitsMID)); Timer calls = registry.getTimer(callsMID); Meter hits = registry.getMeter(hitsMID); @SuppressWarnings("unchecked") Gauge<Double> gauge = (Gauge<Double>) registry.getGauge(cacheHitsMID); long count = 10 + Math.round(Math.random() * 10); for (int i = 0; i < count; i++) { bean.cachedMethod((Math.random() < 0.5)); } assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo((double) hits.getCount() / (double) calls.getCount()))); }
Example #5
Source File: TimedMethodBeanTest.java From microprofile-metrics with Apache License 2.0 | 6 votes |
@Test @InSequence(3) public void removeTimerFromRegistry() throws InterruptedException { Timer timer = registry.getTimer(timerMID); assertThat("Timer is not registered correctly", timer, notNullValue()); // Remove the timer from metrics registry registry.remove(timerMID); try { // Call the timed method and assert an exception is thrown bean.timedMethod(); } catch (RuntimeException cause) { assertThat(cause, is(instanceOf(IllegalStateException.class))); // Make sure that the timer hasn't been called assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(TIMER_COUNT.get()))); return; } fail("No exception has been re-thrown!"); }
Example #6
Source File: TimedInterceptor.java From smallrye-metrics with Apache License 2.0 | 6 votes |
private <E extends Member & AnnotatedElement> Object timedCallable(InvocationContext invocationContext, E element) throws Exception { Set<MetricID> ids = ((MetricsRegistryImpl) registry).getMemberToMetricMappings() .getTimers(new CDIMemberInfoAdapter<>().convert(element)); if (ids == null || ids.isEmpty()) { throw SmallRyeMetricsMessages.msg.noMetricMappedForMember(element); } List<Timer.Context> contexts = ids.stream() .map(metricID -> { Timer metric = registry.getTimers().get(metricID); if (metric == null) { throw SmallRyeMetricsMessages.msg.noMetricFoundInRegistry(MetricType.TIMER, metricID); } return metric; }) .map(Timer::time) .collect(Collectors.toList()); try { return invocationContext.proceed(); } finally { for (Timer.Context timeContext : contexts) { timeContext.stop(); } } }
Example #7
Source File: TimedClassBeanTest.java From microprofile-metrics with Apache License 2.0 | 6 votes |
@Test @InSequence(2) public void callTimedMethodsOnce() { assertThat("Timers are not registered correctly", registry.getTimers().keySet(), is(equalTo(timerMIDsIncludingToString))); assertThat("Constructor timer count is incorrect", registry.getTimer(constructorMID).getCount(), is(equalTo(1L))); // Call the timed methods and assert they've been timed bean.timedMethodOne(); bean.timedMethodTwo(); // Let's call the non-public methods as well bean.timedMethodProtected(); bean.timedMethodPackagedPrivate(); // Make sure that the method timers have been timed assertThat("Method timer counts are incorrect", registry.getTimers(METHOD_TIMERS).values(), everyItem(Matchers.<Timer> hasProperty("count", equalTo(METHOD_COUNT.incrementAndGet())))); }
Example #8
Source File: MetricsRegistryImpl.java From smallrye-metrics with Apache License 2.0 | 6 votes |
private boolean isSameType(Metric metricInstance, MetricType type) { switch (type) { case CONCURRENT_GAUGE: return metricInstance instanceof ConcurrentGauge; case GAUGE: return metricInstance instanceof Gauge; case HISTOGRAM: return metricInstance instanceof Histogram; case TIMER: return metricInstance instanceof Timer; case METERED: return metricInstance instanceof Meter; case COUNTER: return metricInstance instanceof Counter; case SIMPLE_TIMER: return metricInstance instanceof SimpleTimer; default: throw new IllegalArgumentException(); } }
Example #9
Source File: OpenMetricsExporter.java From smallrye-metrics with Apache License 2.0 | 6 votes |
private void writeTimerValues(StringBuilder sb, MetricRegistry.Type scope, Timer timer, Metadata md, Map<String, String> tags) { String unit = OpenMetricsUnit.getBaseUnitAsOpenMetricsString(md.unit()); if (unit.equals(NONE)) unit = "seconds"; String theUnit = USCORE + unit; writeMeterRateValues(sb, scope, timer, md, tags); Snapshot snapshot = timer.getSnapshot(); writeSnapshotBasics(sb, scope, md, snapshot, theUnit, true, tags); writeHelpLine(sb, scope, md.getName(), md, theUnit); writeTypeLine(sb, scope, md.getName(), md, theUnit, SUMMARY); writeValueLine(sb, scope, theUnit + "_count", timer.getCount(), md, tags, false); writeTypeAndValue(sb, scope, "_elapsedTime" + theUnit, timer.getElapsedTime().toNanos(), GAUGE, md, true, tags); writeSnapshotQuantiles(sb, scope, md, snapshot, theUnit, true, tags); }
Example #10
Source File: ExportersMetricScalingTest.java From smallrye-metrics with Apache License 2.0 | 6 votes |
/** * 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 #11
Source File: ExportersMetricScalingTest.java From smallrye-metrics with Apache License 2.0 | 6 votes |
/** * 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 #12
Source File: ExportersMetricScalingTest.java From smallrye-metrics with Apache License 2.0 | 6 votes |
/** * 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 #13
Source File: OpenMetricsExporterTest.java From smallrye-metrics with Apache License 2.0 | 6 votes |
@Test public void testExportOfDifferentTimerImplementations() { OpenMetricsExporter exporter = new OpenMetricsExporter(); MetricRegistry applicationRegistry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); // the export should behave identical for any class derived from Timer Timer[] timers = { new TimerImpl(), new SomeTimer() }; int idx = 0; for (Timer t : timers) { String name = "json_timer_" + idx++; applicationRegistry.register(name, t); String out = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID(name)).toString(); String expectedLine = "application_" + name + "_rate_per_second 0.0"; assertThat(out, containsString(expectedLine)); } }
Example #14
Source File: JsonExporterTest.java From smallrye-metrics with Apache License 2.0 | 6 votes |
@Test public void testExportOfDifferentTimerImplementations() { JsonExporter exporter = new JsonExporter(); MetricRegistry applicationRegistry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); // the export should behave identical for any class derived from Timer Timer[] timers = { new TimerImpl(), new SomeTimer() }; int idx = 0; for (Timer t : timers) { String name = "json_timer_" + idx++; applicationRegistry.register(name, t); StringBuilder out = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID(name)); assertNotNull(out); List<String> lines = Arrays.asList(out.toString().split(LINE_SEPARATOR)); assertEquals(1, lines.stream().filter(line -> line.contains("\"" + name + "\"")).count()); assertEquals(1, lines.stream().filter(line -> line.contains("\"count\": 0")).count()); } }
Example #15
Source File: TimerTest.java From microprofile-metrics with Apache License 2.0 | 6 votes |
@Test @InSequence(3) public void testTimerRegistry() throws Exception { String timerLongName = "test.longData.timer"; String timerTimeName = "testTime"; MetricID timerLongNameMetricID = new MetricID(timerLongName); MetricID timerTimeNameMetricID = new MetricID(timerTimeName); Timer timerLong = registry.getTimer(timerLongNameMetricID); Timer timerTime = registry.getTimer(timerTimeNameMetricID); assertNotNull(timerLong); assertNotNull(timerTime); TestUtils.assertEqualsWithTolerance(480, timerLong.getSnapshot().getValue(0.5)); }
Example #16
Source File: InheritedTimedMethodBeanTest.java From microprofile-metrics with Apache License 2.0 | 6 votes |
@Test @InSequence(2) public void callTimedMethodsOnce() { assertThat("Timers are not registered correctly", registry.getTimers().keySet(), is(equalTo(MetricsUtil.createMetricIDs(absoluteMetricNames())))); // Call the timed methods and assert they've all been timed once bean.publicTimedMethod(); bean.protectedTimedMethod(); bean.packagePrivateTimedMethod(); // Call the methods of the parent and assert they've also been timed once bean.timedMethodOne(); bean.timedMethodTwo(); bean.timedMethodProtected(); bean.timedMethodPackagedPrivate(); assertThat("Timer counts are incorrect", registry.getTimers().values(), everyItem(Matchers.<Timer>hasProperty("count", equalTo(1L)))); }
Example #17
Source File: TimedMethodBeanLookupTest.java From microprofile-metrics with Apache License 2.0 | 6 votes |
@Test @InSequence(3) public void removeTimerFromRegistry() throws InterruptedException { // Get a contextual instance of the bean TimedMethodBean1 bean = instance.get(); Timer timer = registry.getTimer(timerMID); assertThat("Timer is not registered correctly", timer, notNullValue()); // Remove the timer from metrics registry registry.remove(timerMID); try { // Call the timed method and assert an exception is thrown bean.timedMethod(); } catch (RuntimeException cause) { assertThat(cause, is(instanceOf(IllegalStateException.class))); // Make sure that the timer hasn't been called assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(TIMER_COUNT.get()))); return; } fail("No exception has been re-thrown!"); }
Example #18
Source File: TimedMethodBeanLookupTest.java From microprofile-metrics with Apache License 2.0 | 6 votes |
@Test @InSequence(2) public void callTimedMethodOnce() throws InterruptedException { // Get a contextual instance of the bean TimedMethodBean1 bean = instance.get(); Timer timer = registry.getTimer(timerMID); assertThat("Timer is not registered correctly", timer, notNullValue()); // Call the timed method and assert it's been timed bean.timedMethod(); // Make sure that the timer has been called assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(TIMER_COUNT.incrementAndGet()))); TestUtils.assertEqualsWithTolerance(2000000000L, timer.getElapsedTime().toNanos()); }
Example #19
Source File: VisibilityTimedMethodBeanTest.java From microprofile-metrics with Apache License 2.0 | 5 votes |
@Test @InSequence(2) public void callTimedMethodsOnce() { assertThat("Timers are not registered correctly", registry.getTimers().keySet(), is(equalTo(MetricsUtil.createMetricIDs(absoluteMetricNames())))); // Call the timed methods and assert they've all been timed once bean.publicTimedMethod(); bean.protectedTimedMethod(); bean.packagePrivateTimedMethod(); assertThat("Timer counts are incorrect", registry.getTimers().values(), everyItem(Matchers.<Timer>hasProperty("count", equalTo(1L)))); }
Example #20
Source File: OverloadedTimedMethodBeanTest.java From microprofile-metrics with Apache License 2.0 | 5 votes |
@Test @InSequence(1) public void overloadedTimedMethodNotCalledYet() { Assert.assertTrue("Metrics are not registered correctly", registry.getMetricIDs().containsAll(MetricsUtil.createMetricIDs(absoluteMetricNames()))); // Make sure that all the timers haven't been called yet assertThat("Timer counts are incorrect", registry.getTimers().values(), hasItem(Matchers.<Timer> hasProperty("count", equalTo(0L)))); }
Example #21
Source File: TimerTest.java From microprofile-metrics with Apache License 2.0 | 5 votes |
@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 #22
Source File: ConcreteExtendedTimedBeanTest.java From microprofile-metrics with Apache License 2.0 | 5 votes |
@Test @InSequence(4) public void callExtendedTimedMethodOnce(MetricRegistry registry) { Timer timer = registry.getTimer(extendedTimedMID); assertThat("Timer is not registered correctly", timer, notNullValue()); // Call the timed method and assert it's been timed bean.anotherTimedMethod(); // Make sure that the timer has been called assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(1L))); }
Example #23
Source File: ApplicationScopedTimedMethodBeanTest.java From microprofile-metrics with Apache License 2.0 | 5 votes |
@Test @InSequence(2) public void callTimedMethodOnce() { Timer timer = registry.getTimer(timerMID); assertThat("Timer is not registered correctly", timer, notNullValue()); // Call the timed method and assert it's been timed bean.applicationScopedTimedMethod(); // Make sure that the timer has been called assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(1L))); }
Example #24
Source File: ConcreteExtendedTimedBeanTest.java From microprofile-metrics with Apache License 2.0 | 5 votes |
@Test @InSequence(3) public void callTimedMethodOnce(MetricRegistry registry) { Timer timer = registry.getTimer(timedMID); assertThat("Timer is not registered correctly", timer, notNullValue()); // Call the timed method and assert it's been timed bean.timedMethod(); // Make sure that the timer has been called assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(1L))); }
Example #25
Source File: ConcreteExtendedTimedBeanTest.java From microprofile-metrics with Apache License 2.0 | 5 votes |
@Test @InSequence(2) public void extendedTimedMethodNotCalledYet(MetricRegistry registry) { Timer timer = registry.getTimer(extendedTimedMID); assertThat("Timer is not registered correctly on the methods on the abstract class", timer, notNullValue()); // Make sure that the timer hasn't been called yet assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(0L))); }
Example #26
Source File: ConcreteExtendedTimedBeanTest.java From microprofile-metrics with Apache License 2.0 | 5 votes |
@Test @InSequence(1) public void timedMethodNotCalledYet(MetricRegistry registry) { Timer timer = registry.getTimer(timedMID); assertThat("Timer is not registered correctly", timer, notNullValue()); // Make sure that the timer hasn't been called yet assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(0L))); }
Example #27
Source File: TimedConstructorBeanTest.java From microprofile-metrics with Apache License 2.0 | 5 votes |
@Test @InSequence(1) public void timedConstructorCalled() { long count = 1L + Math.round(Math.random() * 10); for (int i = 0; i < count; i++) { instance.get(); } Timer timer = registry.getTimer(timerMID); assertThat("Timer is not registered correctly", timer, notNullValue()); // Make sure that the timer has been called assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(count))); }
Example #28
Source File: MetricAppBean.java From microprofile-metrics with Apache License 2.0 | 5 votes |
public void timeMe() { Timer timer = metrics.timer("metricTest.test1.timer"); Timer.Context context = timer.time(); try { Thread.sleep((long) (Math.random() * 1000)); } catch (InterruptedException e) { } finally { context.stop(); } }
Example #29
Source File: AllMetricsOfGivenTypeTest.java From thorntail with Apache License 2.0 | 5 votes |
@Test public void testGetMetricsOfGivenType() { hello.hello(); hello.howdy(); SortedMap<MetricID, Timer> timers = summary.getAppMetrics().getTimers(); assertEquals(1, timers.size()); assertTrue(timers.containsKey(new MetricID("howdy-time"))); assertFalse(timers.containsKey(new MetricID("hello-count"))); }
Example #30
Source File: TimerTest.java From microprofile-metrics with Apache License 2.0 | 5 votes |
@Test @InSequence(4) public void timesCallableInstances() throws Exception { Timer timer = registry.timer("testCallable"); final String value = timer.time(() -> "one"); Assert.assertEquals(timer.getCount(), 1); Assert.assertEquals(value, "one"); }