@Test @InSequence(1) public void meteredMethodsNotCalledYet() { assertThat("Meters are not registered correctly", registry.getMeters().keySet(), is(equalTo(METER_NAMES))); assertThat("Constructor meter count is incorrect", registry.getMeters().get(CONSTRUCTOR_METER_NAME).getCount(), is(equalTo(CONSTRUCTOR_COUNT.incrementAndGet()))); // Make sure that the method meters haven't been marked yet assertThat("Method meter counts are incorrect", registry.getMeters(METHOD_METERS).values(), everyItem(Matchers.<Meter>hasProperty("count", equalTo(METHOD_COUNT.get())))); }
@Test @InSequence(1) public void meteredMethodNotCalledYet() { assertThat("Meter is not registered correctly", registry.getMeters(), hasKey(METER_NAME)); Meter meter = registry.getMeters().get(METER_NAME); // Make sure that the meter hasn't been marked yet assertThat("Meter count is incorrect", meter.getCount(), is(equalTo(METER_COUNT.get()))); }
@Test @InSequence(1) public void meteredConstructorCalled() { long count = 1L + Math.round(Math.random() * 10); for (int i = 0; i < count; i++) { instance.get(); } assertThat("Meter is not registered correctly", registry.getMeters(), hasKey(METER_NAME)); Meter meter = registry.getMeters().get(METER_NAME); // Make sure that the meter has been called assertThat("Meter count is incorrect", meter.getCount(), is(equalTo(count))); } }
@Test @InSequence(2) public void callMeteredMethodOnce() { assertThat("Meter is not registered correctly", registry.getMeters(), hasKey(METER_NAME)); Meter meter = registry.getMeters().get(METER_NAME); // Call the metered method and assert it's been marked bean.meteredMethod(); // Make sure that the meter has been marked assertThat("Timer count is incorrect", meter.getCount(), is(equalTo(METER_COUNT.incrementAndGet()))); }
private <E extends Member & AnnotatedElement> Object applyInterceptor(InvocationContext context, E member) throws Exception { Metadata metadata = AnnotationMetadata.buildMetadata(bean.getBeanClass(), member, Metered.class); Meter meter = applicationRegistry.getMeters().get(metadata.getName()); if (meter == null) { throw new IllegalStateException("No meter with name [" + metadata.getName() + "] found in registry [" + applicationRegistry + "]"); } meter.mark(); return context.proceed(); } }
@Test @InSequence(2) public void callMeteredMethodsOnce() { assertThat("Meters are not registered correctly", registry.getMeters().keySet(), is(equalTo(METER_NAMES))); assertThat("Constructor meter count is incorrect", registry.getMeters().get(CONSTRUCTOR_METER_NAME).getCount(), is(equalTo(CONSTRUCTOR_COUNT.incrementAndGet()))); // Call the metered methods and assert they've been marked bean.meteredMethodOne(); bean.meteredMethodTwo(); // Let's call the non-public methods as well bean.meteredMethodProtected(); bean.meteredMethodPackagedPrivate(); // Make sure that the method meters have been marked assertThat("Method meter counts are incorrect", registry.getMeters(METHOD_METERS).values(), everyItem(Matchers.<Meter> hasProperty("count", equalTo(METHOD_COUNT.incrementAndGet())))); } }
@Test @InSequence(1) public void metricsConstructorCalled() { long count = 1L + Math.round(Math.random() * 10); for (int i = 0; i < count; i++) { instance.get(); } // Make sure that the metrics have been called assertThat("Counter count is incorrect", registry.getCounters().get(absoluteMetricName("counter")).getCount(), is(equalTo(count))); assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName("meter")).getCount(), is(equalTo(count))); assertThat("Timer count is incorrect", registry.getTimers().get(absoluteMetricName("timer")).getCount(), is(equalTo(count))); } }
@Test @InSequence(3) public void removeMeterFromRegistry() { assertThat("Meter is not registered correctly", registry.getMeters(), hasKey(METER_NAME)); Meter meter = registry.getMeters().get(METER_NAME); // Remove the meter from metrics registry registry.remove(METER_NAME); try { // Call the metered method and assert an exception is thrown bean.meteredMethod(); } catch (RuntimeException cause) { assertThat(cause, is(instanceOf(IllegalStateException.class))); assertThat(cause.getMessage(), is(equalTo("No meter with name [" + METER_NAME + "] found in registry [" + registry + "]"))); // Make sure that the meter hasn't been marked assertThat("Meter count is incorrect", meter.getCount(), is(equalTo(METER_COUNT.get()))); return; } fail("No exception has been re-thrown!"); } }
@Test @InSequence(2) public void callMetricsMethodOnce() { assertThat("Metrics are not registered correctly", registry.getMetrics().keySet(), is(equalTo(absoluteMetricNames()))); // Call the monitored method and assert it's been instrumented bean.metricsMethod(); // Make sure that the metrics have been called assertThat("Counter count is incorrect", registry.getCounters().get(absoluteMetricName("counter")).getCount(), is(equalTo(1L))); assertThat("Meter count is incorrect", registry.getMeters().get(absoluteMetricName("meter")).getCount(), is(equalTo(1L))); assertThat("Timer count is incorrect", registry.getTimers().get(absoluteMetricName("timer")).getCount(), is(equalTo(1L))); // Let's call the gauge at the end as Weld is intercepting the gauge // invocation while OWB not assertThat("Gauge value is incorrect", registry.getGauges().get(absoluteMetricName("gauge")).getValue(), hasToString((equalTo("value")))); } }
@Test @InSequence(2) public void callCachedMethodMultipleTimes() { assertThat("Metrics are not registered correctly", registry.getMetrics(), allOf( hasKey(CALLS_METRIC), hasKey(HITS_METRIC), hasKey(CACHE_HITS_METRIC) ) ); Timer calls = registry.getTimers().get(CALLS_METRIC); Meter hits = registry.getMeters().get(HITS_METRIC); @SuppressWarnings("unchecked") Gauge<Double> gauge = (Gauge<Double>) registry.getGauges().get(CACHE_HITS_METRIC); 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()))); } }
@Test @InSequence(1) public void cachedMethodNotCalledYet() { assertThat("Metrics are not registered correctly", registry.getMetrics(), allOf( hasKey(CALLS_METRIC), hasKey(HITS_METRIC), hasKey(CACHE_HITS_METRIC) ) ); Timer calls = registry.getTimers().get(CALLS_METRIC); Meter hits = registry.getMeters().get(HITS_METRIC); @SuppressWarnings("unchecked") Gauge<Double> gauge = (Gauge<Double>) registry.getGauges().get(CACHE_HITS_METRIC); assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo(((double) hits.getCount() / (double) calls.getCount())))); }
@Test @InSequence(2) public void registerTest() { metrics.register("regCountTemp", countTemp); Assert.assertTrue(metrics.getCounters().containsKey("regCountTemp")); metrics.register("regHistoTemp", histoTemp); Assert.assertTrue(metrics.getHistograms().containsKey("regHistoTemp")); metrics.register("regTimerTemp", timerTemp); Assert.assertTrue(metrics.getTimers().containsKey("regTimerTemp")); metrics.register("regMeterTemp", meterTemp); Assert.assertTrue(metrics.getMeters().containsKey("regMeterTemp")); }