Search in sources :

Example 1 with WindowedCount

use of org.apache.kafka.common.metrics.stats.WindowedCount in project kafka by apache.

the class MetricsTest method testRemoveMetric.

@Test
public void testRemoveMetric() {
    int size = metrics.metrics().size();
    metrics.addMetric(metrics.metricName("test1", "grp1"), new WindowedCount());
    metrics.addMetric(metrics.metricName("test2", "grp1"), new WindowedCount());
    assertNotNull(metrics.removeMetric(metrics.metricName("test1", "grp1")));
    assertNull(metrics.metrics().get(metrics.metricName("test1", "grp1")));
    assertNotNull(metrics.metrics().get(metrics.metricName("test2", "grp1")));
    assertNotNull(metrics.removeMetric(metrics.metricName("test2", "grp1")));
    assertNull(metrics.metrics().get(metrics.metricName("test2", "grp1")));
    assertEquals(size, metrics.metrics().size());
}
Also used : WindowedCount(org.apache.kafka.common.metrics.stats.WindowedCount) Test(org.junit.jupiter.api.Test)

Example 2 with WindowedCount

use of org.apache.kafka.common.metrics.stats.WindowedCount in project kafka by apache.

the class MetricsTest method verifyStats.

private void verifyStats(Function<KafkaMetric, Double> metricValueFunc) {
    ConstantMeasurable measurable = new ConstantMeasurable();
    metrics.addMetric(metrics.metricName("direct.measurable", "grp1", "The fraction of time an appender waits for space allocation."), measurable);
    Sensor s = metrics.sensor("test.sensor");
    s.add(metrics.metricName("test.avg", "grp1"), new Avg());
    s.add(metrics.metricName("test.max", "grp1"), new Max());
    s.add(metrics.metricName("test.min", "grp1"), new Min());
    s.add(new Meter(TimeUnit.SECONDS, metrics.metricName("test.rate", "grp1"), metrics.metricName("test.total", "grp1")));
    s.add(new Meter(TimeUnit.SECONDS, new WindowedCount(), metrics.metricName("test.occurences", "grp1"), metrics.metricName("test.occurences.total", "grp1")));
    s.add(metrics.metricName("test.count", "grp1"), new WindowedCount());
    s.add(new Percentiles(100, -100, 100, BucketSizing.CONSTANT, new Percentile(metrics.metricName("test.median", "grp1"), 50.0), new Percentile(metrics.metricName("test.perc99_9", "grp1"), 99.9)));
    Sensor s2 = metrics.sensor("test.sensor2");
    s2.add(metrics.metricName("s2.total", "grp1"), new CumulativeSum());
    s2.record(5.0);
    int sum = 0;
    int count = 10;
    for (int i = 0; i < count; i++) {
        s.record(i);
        sum += i;
    }
    // prior to any time passing
    double elapsedSecs = (config.timeWindowMs() * (config.samples() - 1)) / 1000.0;
    assertEquals(count / elapsedSecs, metricValueFunc.apply(metrics.metrics().get(metrics.metricName("test.occurences", "grp1"))), EPS, String.format("Occurrences(0...%d) = %f", count, count / elapsedSecs));
    // pretend 2 seconds passed...
    long sleepTimeMs = 2;
    time.sleep(sleepTimeMs * 1000);
    elapsedSecs += sleepTimeMs;
    assertEquals(5.0, metricValueFunc.apply(metrics.metric(metrics.metricName("s2.total", "grp1"))), EPS, "s2 reflects the constant value");
    assertEquals(4.5, metricValueFunc.apply(metrics.metric(metrics.metricName("test.avg", "grp1"))), EPS, "Avg(0...9) = 4.5");
    assertEquals(count - 1, metricValueFunc.apply(metrics.metric(metrics.metricName("test.max", "grp1"))), EPS, "Max(0...9) = 9");
    assertEquals(0.0, metricValueFunc.apply(metrics.metric(metrics.metricName("test.min", "grp1"))), EPS, "Min(0...9) = 0");
    assertEquals(sum / elapsedSecs, metricValueFunc.apply(metrics.metric(metrics.metricName("test.rate", "grp1"))), EPS, "Rate(0...9) = 1.40625");
    assertEquals(count / elapsedSecs, metricValueFunc.apply(metrics.metric(metrics.metricName("test.occurences", "grp1"))), EPS, String.format("Occurrences(0...%d) = %f", count, count / elapsedSecs));
    assertEquals(count, metricValueFunc.apply(metrics.metric(metrics.metricName("test.count", "grp1"))), EPS, "Count(0...9) = 10");
}
Also used : Percentile(org.apache.kafka.common.metrics.stats.Percentile) Max(org.apache.kafka.common.metrics.stats.Max) Meter(org.apache.kafka.common.metrics.stats.Meter) Percentiles(org.apache.kafka.common.metrics.stats.Percentiles) WindowedCount(org.apache.kafka.common.metrics.stats.WindowedCount) CumulativeSum(org.apache.kafka.common.metrics.stats.CumulativeSum) Avg(org.apache.kafka.common.metrics.stats.Avg) Min(org.apache.kafka.common.metrics.stats.Min)

Example 3 with WindowedCount

use of org.apache.kafka.common.metrics.stats.WindowedCount in project kafka by apache.

the class MetricsTest method testTimeWindowing.

@Test
public void testTimeWindowing() {
    WindowedCount count = new WindowedCount();
    MetricConfig config = new MetricConfig().timeWindow(1, TimeUnit.MILLISECONDS).samples(2);
    count.record(config, 1.0, time.milliseconds());
    time.sleep(1);
    count.record(config, 1.0, time.milliseconds());
    assertEquals(2.0, count.measure(config, time.milliseconds()), EPS);
    time.sleep(1);
    // oldest event times out
    count.record(config, 1.0, time.milliseconds());
    assertEquals(2.0, count.measure(config, time.milliseconds()), EPS);
}
Also used : WindowedCount(org.apache.kafka.common.metrics.stats.WindowedCount) Test(org.junit.jupiter.api.Test)

Example 4 with WindowedCount

use of org.apache.kafka.common.metrics.stats.WindowedCount in project kafka by apache.

the class MetricsBench method main.

public static void main(String[] args) {
    long iters = Long.parseLong(args[0]);
    Metrics metrics = new Metrics();
    try {
        Sensor parent = metrics.sensor("parent");
        Sensor child = metrics.sensor("child", parent);
        for (Sensor sensor : Arrays.asList(parent, child)) {
            sensor.add(metrics.metricName(sensor.name() + ".avg", "grp1"), new Avg());
            sensor.add(metrics.metricName(sensor.name() + ".count", "grp1"), new WindowedCount());
            sensor.add(metrics.metricName(sensor.name() + ".max", "grp1"), new Max());
            sensor.add(new Percentiles(1024, 0.0, iters, BucketSizing.CONSTANT, new Percentile(metrics.metricName(sensor.name() + ".median", "grp1"), 50.0), new Percentile(metrics.metricName(sensor.name() + ".p_99", "grp1"), 99.0)));
        }
        long start = System.nanoTime();
        for (int i = 0; i < iters; i++) parent.record(i);
        double ellapsed = (System.nanoTime() - start) / (double) iters;
        System.out.println(String.format("%.2f ns per metric recording.", ellapsed));
    } finally {
        metrics.close();
    }
}
Also used : Metrics(org.apache.kafka.common.metrics.Metrics) Percentile(org.apache.kafka.common.metrics.stats.Percentile) Avg(org.apache.kafka.common.metrics.stats.Avg) Max(org.apache.kafka.common.metrics.stats.Max) Percentiles(org.apache.kafka.common.metrics.stats.Percentiles) Sensor(org.apache.kafka.common.metrics.Sensor) WindowedCount(org.apache.kafka.common.metrics.stats.WindowedCount)

Example 5 with WindowedCount

use of org.apache.kafka.common.metrics.stats.WindowedCount in project kafka by apache.

the class KafkaMbeanTest method setup.

@BeforeEach
public void setup() throws Exception {
    metrics = new Metrics();
    metrics.addReporter(new JmxReporter());
    sensor = metrics.sensor("kafka.requests");
    countMetricName = metrics.metricName("pack.bean1.count", "grp1");
    sensor.add(countMetricName, new WindowedCount());
    sumMetricName = metrics.metricName("pack.bean1.sum", "grp1");
    sensor.add(sumMetricName, new WindowedSum());
}
Also used : WindowedSum(org.apache.kafka.common.metrics.stats.WindowedSum) WindowedCount(org.apache.kafka.common.metrics.stats.WindowedCount) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

WindowedCount (org.apache.kafka.common.metrics.stats.WindowedCount)11 Test (org.junit.jupiter.api.Test)8 Avg (org.apache.kafka.common.metrics.stats.Avg)2 Max (org.apache.kafka.common.metrics.stats.Max)2 Meter (org.apache.kafka.common.metrics.stats.Meter)2 Percentile (org.apache.kafka.common.metrics.stats.Percentile)2 Percentiles (org.apache.kafka.common.metrics.stats.Percentiles)2 WindowedSum (org.apache.kafka.common.metrics.stats.WindowedSum)2 MetricName (org.apache.kafka.common.MetricName)1 Metrics (org.apache.kafka.common.metrics.Metrics)1 Sensor (org.apache.kafka.common.metrics.Sensor)1 CumulativeSum (org.apache.kafka.common.metrics.stats.CumulativeSum)1 Min (org.apache.kafka.common.metrics.stats.Min)1 Rate (org.apache.kafka.common.metrics.stats.Rate)1 SimpleRate (org.apache.kafka.common.metrics.stats.SimpleRate)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1