Search in sources :

Example 6 with Percentiles

use of org.apache.kafka.common.metrics.stats.Percentiles 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 7 with Percentiles

use of org.apache.kafka.common.metrics.stats.Percentiles in project apache-kafka-on-k8s by banzaicloud.

the class MetricsTest method testSimpleStats.

@Test
public void testSimpleStats() throws Exception {
    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 Count(), metrics.metricName("test.occurences", "grp1"), metrics.metricName("test.occurences.total", "grp1")));
    s.add(metrics.metricName("test.count", "grp1"), new Count());
    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 Total());
    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(String.format("Occurrences(0...%d) = %f", count, count / elapsedSecs), count / elapsedSecs, metrics.metrics().get(metrics.metricName("test.occurences", "grp1")).value(), EPS);
    // pretend 2 seconds passed...
    long sleepTimeMs = 2;
    time.sleep(sleepTimeMs * 1000);
    elapsedSecs += sleepTimeMs;
    assertEquals("s2 reflects the constant value", 5.0, metrics.metrics().get(metrics.metricName("s2.total", "grp1")).value(), EPS);
    assertEquals("Avg(0...9) = 4.5", 4.5, metrics.metrics().get(metrics.metricName("test.avg", "grp1")).value(), EPS);
    assertEquals("Max(0...9) = 9", count - 1, metrics.metrics().get(metrics.metricName("test.max", "grp1")).value(), EPS);
    assertEquals("Min(0...9) = 0", 0.0, metrics.metrics().get(metrics.metricName("test.min", "grp1")).value(), EPS);
    assertEquals("Rate(0...9) = 1.40625", sum / elapsedSecs, metrics.metrics().get(metrics.metricName("test.rate", "grp1")).value(), EPS);
    assertEquals(String.format("Occurrences(0...%d) = %f", count, count / elapsedSecs), count / elapsedSecs, metrics.metrics().get(metrics.metricName("test.occurences", "grp1")).value(), EPS);
    assertEquals("Count(0...9) = 10", (double) count, metrics.metrics().get(metrics.metricName("test.count", "grp1")).value(), EPS);
}
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) Count(org.apache.kafka.common.metrics.stats.Count) Percentiles(org.apache.kafka.common.metrics.stats.Percentiles) Avg(org.apache.kafka.common.metrics.stats.Avg) Min(org.apache.kafka.common.metrics.stats.Min) Total(org.apache.kafka.common.metrics.stats.Total) Test(org.junit.Test)

Example 8 with Percentiles

use of org.apache.kafka.common.metrics.stats.Percentiles in project apache-kafka-on-k8s by banzaicloud.

the class MetricsTest method testPercentiles.

@Test
public void testPercentiles() {
    int buckets = 100;
    Percentiles percs = new Percentiles(4 * buckets, 0.0, 100.0, BucketSizing.CONSTANT, new Percentile(metrics.metricName("test.p25", "grp1"), 25), new Percentile(metrics.metricName("test.p50", "grp1"), 50), new Percentile(metrics.metricName("test.p75", "grp1"), 75));
    MetricConfig config = new MetricConfig().eventWindow(50).samples(2);
    Sensor sensor = metrics.sensor("test", config);
    sensor.add(percs);
    Metric p25 = this.metrics.metrics().get(metrics.metricName("test.p25", "grp1"));
    Metric p50 = this.metrics.metrics().get(metrics.metricName("test.p50", "grp1"));
    Metric p75 = this.metrics.metrics().get(metrics.metricName("test.p75", "grp1"));
    // record two windows worth of sequential values
    for (int i = 0; i < buckets; i++) sensor.record(i);
    assertEquals(25, p25.value(), 1.0);
    assertEquals(50, p50.value(), 1.0);
    assertEquals(75, p75.value(), 1.0);
    for (int i = 0; i < buckets; i++) sensor.record(0.0);
    assertEquals(0.0, p25.value(), 1.0);
    assertEquals(0.0, p50.value(), 1.0);
    assertEquals(0.0, p75.value(), 1.0);
    // record two more windows worth of sequential values
    for (int i = 0; i < buckets; i++) sensor.record(i);
    assertEquals(25, p25.value(), 1.0);
    assertEquals(50, p50.value(), 1.0);
    assertEquals(75, p75.value(), 1.0);
}
Also used : Percentile(org.apache.kafka.common.metrics.stats.Percentile) Metric(org.apache.kafka.common.Metric) Percentiles(org.apache.kafka.common.metrics.stats.Percentiles) Test(org.junit.Test)

Example 9 with Percentiles

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

the class MetricsTest method testPercentiles.

@Test
public void testPercentiles() {
    int buckets = 100;
    Percentiles percs = new Percentiles(4 * buckets, 0.0, 100.0, BucketSizing.CONSTANT, new Percentile(metrics.metricName("test.p25", "grp1"), 25), new Percentile(metrics.metricName("test.p50", "grp1"), 50), new Percentile(metrics.metricName("test.p75", "grp1"), 75));
    MetricConfig config = new MetricConfig().eventWindow(50).samples(2);
    Sensor sensor = metrics.sensor("test", config);
    sensor.add(percs);
    Metric p25 = this.metrics.metrics().get(metrics.metricName("test.p25", "grp1"));
    Metric p50 = this.metrics.metrics().get(metrics.metricName("test.p50", "grp1"));
    Metric p75 = this.metrics.metrics().get(metrics.metricName("test.p75", "grp1"));
    // record two windows worth of sequential values
    for (int i = 0; i < buckets; i++) sensor.record(i);
    assertEquals(25, (Double) p25.metricValue(), 1.0);
    assertEquals(50, (Double) p50.metricValue(), 1.0);
    assertEquals(75, (Double) p75.metricValue(), 1.0);
    for (int i = 0; i < buckets; i++) sensor.record(0.0);
    assertEquals(0.0, (Double) p25.metricValue(), 1.0);
    assertEquals(0.0, (Double) p50.metricValue(), 1.0);
    assertEquals(0.0, (Double) p75.metricValue(), 1.0);
    // record two more windows worth of sequential values
    for (int i = 0; i < buckets; i++) sensor.record(i);
    assertEquals(25, (Double) p25.metricValue(), 1.0);
    assertEquals(50, (Double) p50.metricValue(), 1.0);
    assertEquals(75, (Double) p75.metricValue(), 1.0);
}
Also used : Percentile(org.apache.kafka.common.metrics.stats.Percentile) Metric(org.apache.kafka.common.Metric) Percentiles(org.apache.kafka.common.metrics.stats.Percentiles) Test(org.junit.jupiter.api.Test)

Example 10 with Percentiles

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

the class MetricsTest method shouldPinLargerValuesToMax.

@Test
public void shouldPinLargerValuesToMax() {
    final double min = 0.0d;
    final double max = 100d;
    Percentiles percs = new Percentiles(1000, min, max, BucketSizing.LINEAR, new Percentile(metrics.metricName("test.p50", "grp1"), 50));
    MetricConfig config = new MetricConfig().eventWindow(50).samples(2);
    Sensor sensor = metrics.sensor("test", config);
    sensor.add(percs);
    Metric p50 = this.metrics.metrics().get(metrics.metricName("test.p50", "grp1"));
    sensor.record(max + 100);
    sensor.record(max + 100);
    assertEquals(max, (double) p50.metricValue(), 0d);
}
Also used : Percentile(org.apache.kafka.common.metrics.stats.Percentile) Metric(org.apache.kafka.common.Metric) Percentiles(org.apache.kafka.common.metrics.stats.Percentiles) Test(org.junit.jupiter.api.Test)

Aggregations

Percentile (org.apache.kafka.common.metrics.stats.Percentile)10 Percentiles (org.apache.kafka.common.metrics.stats.Percentiles)10 Metric (org.apache.kafka.common.Metric)5 Avg (org.apache.kafka.common.metrics.stats.Avg)5 Max (org.apache.kafka.common.metrics.stats.Max)5 Test (org.junit.jupiter.api.Test)4 Count (org.apache.kafka.common.metrics.stats.Count)3 Min (org.apache.kafka.common.metrics.stats.Min)3 Test (org.junit.Test)3 Metrics (org.apache.kafka.common.metrics.Metrics)2 Sensor (org.apache.kafka.common.metrics.Sensor)2 Meter (org.apache.kafka.common.metrics.stats.Meter)2 Total (org.apache.kafka.common.metrics.stats.Total)2 WindowedCount (org.apache.kafka.common.metrics.stats.WindowedCount)2 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 CumulativeSum (org.apache.kafka.common.metrics.stats.CumulativeSum)1 Rate (org.apache.kafka.common.metrics.stats.Rate)1 SimpleRate (org.apache.kafka.common.metrics.stats.SimpleRate)1