Search in sources :

Example 16 with Counter

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"));
        }
    });
}
Also used : Histogram(com.codahale.metrics.Histogram) Meter(com.codahale.metrics.Meter) MetricRegistry(com.codahale.metrics.MetricRegistry) AggregateMetric(org.apache.solr.metrics.AggregateMetric) Counter(com.codahale.metrics.Counter) Timer(com.codahale.metrics.Timer) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 17 with Counter

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());
    }
}
Also used : Counter(com.codahale.metrics.Counter) Random(java.util.Random) SolrInfoBean(org.apache.solr.core.SolrInfoBean) HashMap(java.util.HashMap) SolrMetricProducer(org.apache.solr.metrics.SolrMetricProducer) ObjectInstance(javax.management.ObjectInstance) Test(org.junit.Test)

Example 18 with Counter

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;
}
Also used : Counter(com.codahale.metrics.Counter) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 19 with Counter

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);
    }
}
Also used : Counter(com.codahale.metrics.Counter) Random(java.util.Random) SolrInfoBean(org.apache.solr.core.SolrInfoBean) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 20 with Counter

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);
    }
}
Also used : Counter(com.codahale.metrics.Counter) Random(java.util.Random) SolrInfoBean(org.apache.solr.core.SolrInfoBean) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

Counter (com.codahale.metrics.Counter)55 Test (org.junit.Test)24 Map (java.util.Map)15 Timer (com.codahale.metrics.Timer)14 MetricRegistry (com.codahale.metrics.MetricRegistry)13 HashMap (java.util.HashMap)11 Gauge (com.codahale.metrics.Gauge)10 Histogram (com.codahale.metrics.Histogram)10 Metric (com.codahale.metrics.Metric)9 Meter (com.codahale.metrics.Meter)8 Random (java.util.Random)8 SolrInfoBean (org.apache.solr.core.SolrInfoBean)6 SortedMap (java.util.SortedMap)5 IOException (java.io.IOException)4 TreeMap (java.util.TreeMap)4 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)4 SolrMetricManager (org.apache.solr.metrics.SolrMetricManager)4 Description (com.google.gerrit.metrics.Description)3 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3