@Override public long getCount() { return delegate.getCount(); }
@Override public Double getValue() { return (double) hits.getCount() / (double) calls.getCount(); } };
@Test public void testCount() throws Exception { // test mark() long countBefore = injectedMeter.getCount(); injectedMeter.mark(); long countAfter = injectedMeter.getCount(); Assert.assertEquals(countBefore + 1, countAfter); // test mark(2) countBefore = injectedMeter.getCount(); injectedMeter.mark(2); countAfter = injectedMeter.getCount(); Assert.assertEquals(countBefore + 2, countAfter); // test mark(-3) countBefore = injectedMeter.getCount(); injectedMeter.mark(-3); countAfter = injectedMeter.getCount(); Assert.assertEquals(countBefore - 3, countAfter); }
@Test @InSequence(1) public void meteredMethodNotCalledYet() { // Make sure that the meter hasn't been called yet assertThat("Meter count is incorrect", meter.getCount(), is(equalTo(0L))); }
@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(2) public void callMeteredMethodOnce() { // Call the metered method and assert it's been marked bean.meteredMethod(); // Make sure that the meter has been called assertThat("Meter count is incorrect", meter.getCount(), is(equalTo(1L))); } }
@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(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(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(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()))); }
@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 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(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())))); }
Meter meter = (Meter)metric; json.writeStartObject(); json.writeObjectField("count", meter.getCount()); json.writeObjectField("meanRate", meter.getMeanRate()); json.writeObjectField("oneMinRate", meter.getOneMinuteRate());
final Meter meter = Meter.class.cast(entry.metric); return new StringBuilder() .append(value(registryKey, key + "_count", meter.getCount(), entry.metadata)) .append(value(registryKey, keyBase + "_rate_per_second", meter.getMeanRate(), entry.metadata)) .append(value(registryKey, keyBase + "_one_min_rate_per_second", meter.getOneMinuteRate(), entry.metadata))