use of com.codahale.metrics.Counter in project lucene-solr by apache.
the class MetricUtilsTest method testMetrics.
@Test
public void testMetrics() throws Exception {
MetricRegistry registry = new MetricRegistry();
Counter counter = registry.counter("counter");
counter.inc();
Timer timer = registry.timer("timer");
Timer.Context ctx = timer.time();
Thread.sleep(150);
ctx.stop();
Meter meter = registry.meter("meter");
meter.mark();
Histogram histogram = registry.histogram("histogram");
histogram.update(10);
AggregateMetric am = new AggregateMetric();
registry.register("aggregate", am);
am.set("foo", 10);
am.set("bar", 1);
am.set("bar", 2);
Gauge<String> gauge = () -> "foobar";
registry.register("gauge", gauge);
Gauge<Long> error = () -> {
throw new InternalError("Memory Pool not found error");
};
registry.register("memory.expected.error", error);
MetricUtils.toMaps(registry, Collections.singletonList(MetricFilter.ALL), MetricFilter.ALL, MetricUtils.PropertyFilter.ALL, false, false, false, false, (k, o) -> {
Map v = (Map) o;
if (k.startsWith("counter")) {
assertEquals(1L, v.get("count"));
} else if (k.startsWith("gauge")) {
assertEquals("foobar", v.get("value"));
} else if (k.startsWith("timer")) {
assertEquals(1L, v.get("count"));
assertTrue(((Number) v.get("min_ms")).intValue() > 100);
} else if (k.startsWith("meter")) {
assertEquals(1L, v.get("count"));
} else if (k.startsWith("histogram")) {
assertEquals(1L, v.get("count"));
} else if (k.startsWith("aggregate")) {
assertEquals(2, v.get("count"));
Map<String, Object> values = (Map<String, Object>) v.get("values");
assertNotNull(values);
assertEquals(2, values.size());
Map<String, Object> update = (Map<String, Object>) values.get("foo");
assertEquals(10, update.get("value"));
assertEquals(1, update.get("updateCount"));
update = (Map<String, Object>) values.get("bar");
assertEquals(2, update.get("value"));
assertEquals(2, update.get("updateCount"));
} else if (k.startsWith("memory.expected.error")) {
assertNull(v);
}
});
// test compact format
MetricUtils.toMaps(registry, Collections.singletonList(MetricFilter.ALL), MetricFilter.ALL, MetricUtils.PropertyFilter.ALL, false, false, true, false, (k, o) -> {
if (k.startsWith("counter")) {
assertTrue(o instanceof Long);
assertEquals(1L, o);
} else if (k.startsWith("gauge")) {
assertTrue(o instanceof String);
assertEquals("foobar", o);
} else if (k.startsWith("timer")) {
assertTrue(o instanceof Map);
Map v = (Map) o;
assertEquals(1L, v.get("count"));
assertTrue(((Number) v.get("min_ms")).intValue() > 100);
} else if (k.startsWith("meter")) {
assertTrue(o instanceof Map);
Map v = (Map) o;
assertEquals(1L, v.get("count"));
} else if (k.startsWith("histogram")) {
assertTrue(o instanceof Map);
Map v = (Map) o;
assertEquals(1L, v.get("count"));
} else if (k.startsWith("aggregate")) {
assertTrue(o instanceof Map);
Map v = (Map) o;
assertEquals(2, v.get("count"));
Map<String, Object> values = (Map<String, Object>) v.get("values");
assertNotNull(values);
assertEquals(2, values.size());
Map<String, Object> update = (Map<String, Object>) values.get("foo");
assertEquals(10, update.get("value"));
assertEquals(1, update.get("updateCount"));
update = (Map<String, Object>) values.get("bar");
assertEquals(2, update.get("value"));
assertEquals(2, update.get("updateCount"));
} else if (k.startsWith("memory.expected.error")) {
assertNull(o);
} else {
Map v = (Map) o;
assertEquals(1L, v.get("count"));
}
});
}
use of com.codahale.metrics.Counter in project lucene-solr by apache.
the class SolrJmxReporterTest method testReportMetrics.
@Test
public void testReportMetrics() throws Exception {
Random random = random();
Map<String, Counter> registered = new HashMap<>();
String scope = SolrMetricTestUtils.getRandomScope(random, true);
SolrInfoBean.Category category = SolrMetricTestUtils.getRandomCategory(random, true);
int iterations = TestUtil.nextInt(random, 0, MAX_ITERATIONS);
for (int i = 0; i < iterations; ++i) {
Map<String, Counter> metrics = SolrMetricTestUtils.getRandomMetricsWithReplacements(random, registered);
SolrMetricProducer producer = SolrMetricTestUtils.getProducerOf(metricManager, category, scope, metrics);
coreMetricManager.registerMetricProducer(scope, producer);
registered.putAll(metrics);
//waitForListener();
Set<ObjectInstance> objects = mBeanServer.queryMBeans(null, null);
assertEquals(registered.size(), objects.stream().filter(o -> scope.equals(o.getObjectName().getKeyProperty("scope")) && rootName.equals(o.getObjectName().getDomain())).count());
}
}
use of com.codahale.metrics.Counter in project lucene-solr by apache.
the class SolrMetricTestUtils method getRandomMetricsWithReplacements.
public static Map<String, Counter> getRandomMetricsWithReplacements(Random random, Map<String, Counter> existing) {
HashMap<String, Counter> metrics = new HashMap<>();
ArrayList<String> existingKeys = new ArrayList<>(existing.keySet());
int numMetrics = TestUtil.nextInt(random, 1, MAX_ITERATIONS);
for (int i = 0; i < numMetrics; ++i) {
boolean shouldReplaceMetric = !existing.isEmpty() && random.nextBoolean();
String name = shouldReplaceMetric ? existingKeys.get(TestUtil.nextInt(random, 0, existingKeys.size() - 1)) : // must be simple string for JMX publishing
TestUtil.randomSimpleString(random, 5, 10) + SUFFIX;
Counter counter = new Counter();
counter.inc(random.nextLong());
metrics.put(name, counter);
}
return metrics;
}
use of com.codahale.metrics.Counter in project lucene-solr by apache.
the class SolrCoreMetricManagerTest method testRegisterMetricsWithReplacements.
@Test
public void testRegisterMetricsWithReplacements() {
Random random = random();
Map<String, Counter> registered = new HashMap<>();
String scope = SolrMetricTestUtils.getRandomScope(random, true);
SolrInfoBean.Category category = SolrMetricTestUtils.getRandomCategory(random, true);
int iterations = TestUtil.nextInt(random, 0, MAX_ITERATIONS);
for (int i = 0; i < iterations; ++i) {
Map<String, Counter> metrics = SolrMetricTestUtils.getRandomMetricsWithReplacements(random, registered);
if (metrics.isEmpty()) {
continue;
}
SolrMetricProducer producer = SolrMetricTestUtils.getProducerOf(metricManager, category, scope, metrics);
coreMetricManager.registerMetricProducer(scope, producer);
registered.putAll(metrics);
assertRegistered(scope, registered, coreMetricManager);
}
}
use of com.codahale.metrics.Counter in project lucene-solr by apache.
the class SolrCoreMetricManagerTest method testRegisterMetrics.
@Test
public void testRegisterMetrics() {
Random random = random();
String scope = SolrMetricTestUtils.getRandomScope(random);
SolrInfoBean.Category category = SolrMetricTestUtils.getRandomCategory(random);
Map<String, Counter> metrics = SolrMetricTestUtils.getRandomMetrics(random);
SolrMetricProducer producer = SolrMetricTestUtils.getProducerOf(metricManager, category, scope, metrics);
try {
coreMetricManager.registerMetricProducer(scope, producer);
assertNotNull(scope);
assertNotNull(category);
assertRegistered(scope, metrics, coreMetricManager);
} catch (final IllegalArgumentException e) {
assertTrue("expected at least one null but got: scope=" + scope + ", category=" + category, (scope == null || category == null));
assertRegistered(scope, new HashMap<>(), coreMetricManager);
}
}
Aggregations