Search in sources :

Example 1 with SimpleRate

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

the class MetricsTest method testSimpleRate.

@Test
public void testSimpleRate() {
    SimpleRate rate = new SimpleRate();
    // Given
    MetricConfig config = new MetricConfig().timeWindow(1, TimeUnit.SECONDS).samples(10);
    // In the first window the rate is a fraction of the whole (1s) window
    // So when we record 1000 at t0, the rate should be 1000 until the window completes, or more data is recorded.
    record(rate, config, 1000);
    assertEquals(1000, measure(rate, config), 0);
    time.sleep(100);
    // 1000B / 0.1s
    assertEquals(1000, measure(rate, config), 0);
    time.sleep(100);
    // 1000B / 0.2s
    assertEquals(1000, measure(rate, config), 0);
    time.sleep(200);
    // 1000B / 0.4s
    assertEquals(1000, measure(rate, config), 0);
    // In the second (and subsequent) window(s), the rate will be in proportion to the elapsed time
    // So the rate will degrade over time, as the time between measurement and the initial recording grows.
    time.sleep(600);
    // 1000B / 1.0s
    assertEquals(1000, measure(rate, config), 0);
    time.sleep(200);
    // 1000B / 1.2s
    assertEquals(1000 / 1.2, measure(rate, config), 0);
    time.sleep(200);
    // 1000B / 1.4s
    assertEquals(1000 / 1.4, measure(rate, config), 0);
    // Adding another value, inside the same window should double the rate
    record(rate, config, 1000);
    // 2000B / 1.4s
    assertEquals(2000 / 1.4, measure(rate, config), 0);
    // Going over the next window, should not change behaviour
    time.sleep(1100);
    // 2000B / 2.5s
    assertEquals(2000 / 2.5, measure(rate, config), 0);
    record(rate, config, 1000);
    // 3000B / 2.5s
    assertEquals(3000 / 2.5, measure(rate, config), 0);
    // Sleeping for another 6.5 windows also should be the same
    time.sleep(6500);
    // 3000B / 9s
    assertEquals(3000 / 9, measure(rate, config), 1);
    record(rate, config, 1000);
    // 4000B / 9s
    assertEquals(4000 / 9, measure(rate, config), 1);
    // Going over the 10 window boundary should cause the first window's values (1000) will be purged.
    // So the rate is calculated based on the oldest reading, which is inside the second window, at 1.4s
    time.sleep(1500);
    assertEquals((4000 - 1000) / (10.5 - 1.4), measure(rate, config), 1);
    record(rate, config, 1000);
    assertEquals((5000 - 1000) / (10.5 - 1.4), measure(rate, config), 1);
}
Also used : SimpleRate(org.apache.kafka.common.metrics.stats.SimpleRate) Test(org.junit.Test)

Example 2 with SimpleRate

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

the class MetricsTest method testSimpleRate.

@Test
public void testSimpleRate() {
    SimpleRate rate = new SimpleRate();
    // Given
    MetricConfig config = new MetricConfig().timeWindow(1, TimeUnit.SECONDS).samples(10);
    // In the first window the rate is a fraction of the whole (1s) window
    // So when we record 1000 at t0, the rate should be 1000 until the window completes, or more data is recorded.
    record(rate, config, 1000);
    assertEquals(1000, measure(rate, config), 0);
    time.sleep(100);
    // 1000B / 0.1s
    assertEquals(1000, measure(rate, config), 0);
    time.sleep(100);
    // 1000B / 0.2s
    assertEquals(1000, measure(rate, config), 0);
    time.sleep(200);
    // 1000B / 0.4s
    assertEquals(1000, measure(rate, config), 0);
    // In the second (and subsequent) window(s), the rate will be in proportion to the elapsed time
    // So the rate will degrade over time, as the time between measurement and the initial recording grows.
    time.sleep(600);
    // 1000B / 1.0s
    assertEquals(1000, measure(rate, config), 0);
    time.sleep(200);
    // 1000B / 1.2s
    assertEquals(1000 / 1.2, measure(rate, config), 0);
    time.sleep(200);
    // 1000B / 1.4s
    assertEquals(1000 / 1.4, measure(rate, config), 0);
    // Adding another value, inside the same window should double the rate
    record(rate, config, 1000);
    // 2000B / 1.4s
    assertEquals(2000 / 1.4, measure(rate, config), 0);
    // Going over the next window, should not change behaviour
    time.sleep(1100);
    // 2000B / 2.5s
    assertEquals(2000 / 2.5, measure(rate, config), 0);
    record(rate, config, 1000);
    // 3000B / 2.5s
    assertEquals(3000 / 2.5, measure(rate, config), 0);
    // Sleeping for another 6.5 windows also should be the same
    time.sleep(6500);
    // 3000B / 9s
    assertEquals(3000 / 9, measure(rate, config), 1);
    record(rate, config, 1000);
    // 4000B / 9s
    assertEquals(4000 / 9, measure(rate, config), 1);
    // Going over the 10 window boundary should cause the first window's values (1000) will be purged.
    // So the rate is calculated based on the oldest reading, which is inside the second window, at 1.4s
    time.sleep(1500);
    assertEquals((4000 - 1000) / (10.5 - 1.4), measure(rate, config), 1);
    record(rate, config, 1000);
    assertEquals((5000 - 1000) / (10.5 - 1.4), measure(rate, config), 1);
}
Also used : SimpleRate(org.apache.kafka.common.metrics.stats.SimpleRate) Test(org.junit.jupiter.api.Test)

Aggregations

SimpleRate (org.apache.kafka.common.metrics.stats.SimpleRate)2 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1