use of org.apache.kafka.common.metrics.stats.WindowedCount in project kafka by apache.
the class MetricsTest method testSampledStatReturnsInitialValueWhenNoValuesExist.
/**
* Some implementations of SampledStat make sense to return the initial value
* when there are no values set
*/
@Test
public void testSampledStatReturnsInitialValueWhenNoValuesExist() {
WindowedCount count = new WindowedCount();
WindowedSum sampledTotal = new WindowedSum();
long windowMs = 100;
int samples = 2;
MetricConfig config = new MetricConfig().timeWindow(windowMs, TimeUnit.MILLISECONDS).samples(samples);
count.record(config, 50, time.milliseconds());
sampledTotal.record(config, 50, time.milliseconds());
time.sleep(samples * windowMs);
assertEquals(0, count.measure(config, time.milliseconds()), EPS);
assertEquals(0.0, sampledTotal.measure(config, time.milliseconds()), EPS);
}
use of org.apache.kafka.common.metrics.stats.WindowedCount in project kafka by apache.
the class MetricsTest method testHierarchicalSensors.
@Test
public void testHierarchicalSensors() {
Sensor parent1 = metrics.sensor("test.parent1");
parent1.add(metrics.metricName("test.parent1.count", "grp1"), new WindowedCount());
Sensor parent2 = metrics.sensor("test.parent2");
parent2.add(metrics.metricName("test.parent2.count", "grp1"), new WindowedCount());
Sensor child1 = metrics.sensor("test.child1", parent1, parent2);
child1.add(metrics.metricName("test.child1.count", "grp1"), new WindowedCount());
Sensor child2 = metrics.sensor("test.child2", parent1);
child2.add(metrics.metricName("test.child2.count", "grp1"), new WindowedCount());
Sensor grandchild = metrics.sensor("test.grandchild", child1);
grandchild.add(metrics.metricName("test.grandchild.count", "grp1"), new WindowedCount());
/* increment each sensor one time */
parent1.record();
parent2.record();
child1.record();
child2.record();
grandchild.record();
double p1 = (double) parent1.metrics().get(0).metricValue();
double p2 = (double) parent2.metrics().get(0).metricValue();
double c1 = (double) child1.metrics().get(0).metricValue();
double c2 = (double) child2.metrics().get(0).metricValue();
double gc = (double) grandchild.metrics().get(0).metricValue();
/* each metric should have a count equal to one + its children's count */
assertEquals(1.0, gc, EPS);
assertEquals(1.0 + gc, c1, EPS);
assertEquals(1.0, c2, EPS);
assertEquals(1.0 + c1, p2, EPS);
assertEquals(1.0 + c1 + c2, p1, EPS);
assertEquals(Arrays.asList(child1, child2), metrics.childrenSensors().get(parent1));
assertEquals(Arrays.asList(child1), metrics.childrenSensors().get(parent2));
assertNull(metrics.childrenSensors().get(grandchild));
}
use of org.apache.kafka.common.metrics.stats.WindowedCount in project kafka by apache.
the class MetricsTest method testEventWindowing.
@Test
public void testEventWindowing() {
WindowedCount count = new WindowedCount();
MetricConfig config = new MetricConfig().eventWindow(1).samples(2);
count.record(config, 1.0, time.milliseconds());
count.record(config, 1.0, time.milliseconds());
assertEquals(2.0, count.measure(config, time.milliseconds()), EPS);
// first event times out
count.record(config, 1.0, time.milliseconds());
assertEquals(2.0, count.measure(config, time.milliseconds()), EPS);
}
use of org.apache.kafka.common.metrics.stats.WindowedCount in project kafka by apache.
the class MetricsTest method testRateWindowing.
@Test
public void testRateWindowing() throws Exception {
// Use the default time window. Set 3 samples
MetricConfig cfg = new MetricConfig().samples(3);
Sensor s = metrics.sensor("test.sensor", cfg);
MetricName rateMetricName = metrics.metricName("test.rate", "grp1");
MetricName totalMetricName = metrics.metricName("test.total", "grp1");
MetricName countRateMetricName = metrics.metricName("test.count.rate", "grp1");
MetricName countTotalMetricName = metrics.metricName("test.count.total", "grp1");
s.add(new Meter(TimeUnit.SECONDS, rateMetricName, totalMetricName));
s.add(new Meter(TimeUnit.SECONDS, new WindowedCount(), countRateMetricName, countTotalMetricName));
KafkaMetric totalMetric = metrics.metrics().get(totalMetricName);
KafkaMetric countTotalMetric = metrics.metrics().get(countTotalMetricName);
int sum = 0;
int count = cfg.samples() - 1;
// Advance 1 window after every record
for (int i = 0; i < count; i++) {
s.record(100);
sum += 100;
time.sleep(cfg.timeWindowMs());
assertEquals(sum, (Double) totalMetric.metricValue(), EPS);
}
// Sleep for half the window.
time.sleep(cfg.timeWindowMs() / 2);
// prior to any time passing
double elapsedSecs = (cfg.timeWindowMs() * (cfg.samples() - 1) + cfg.timeWindowMs() / 2) / 1000.0;
KafkaMetric rateMetric = metrics.metrics().get(rateMetricName);
KafkaMetric countRateMetric = metrics.metrics().get(countRateMetricName);
assertEquals(sum / elapsedSecs, (Double) rateMetric.metricValue(), EPS, "Rate(0...2) = 2.666");
assertEquals(count / elapsedSecs, (Double) countRateMetric.metricValue(), EPS, "Count rate(0...2) = 0.02666");
assertEquals(elapsedSecs, ((Rate) rateMetric.measurable()).windowSize(cfg, time.milliseconds()) / 1000, EPS, "Elapsed Time = 75 seconds");
assertEquals(sum, (Double) totalMetric.metricValue(), EPS);
assertEquals(count, (Double) countTotalMetric.metricValue(), EPS);
// Verify that rates are expired, but total is cumulative
time.sleep(cfg.timeWindowMs() * cfg.samples());
assertEquals(0, (Double) rateMetric.metricValue(), EPS);
assertEquals(0, (Double) countRateMetric.metricValue(), EPS);
assertEquals(sum, (Double) totalMetric.metricValue(), EPS);
assertEquals(count, (Double) countTotalMetric.metricValue(), EPS);
}
use of org.apache.kafka.common.metrics.stats.WindowedCount in project kafka by apache.
the class MetricsTest method testRemoveInactiveMetrics.
@Test
public void testRemoveInactiveMetrics() {
Sensor s1 = metrics.sensor("test.s1", null, 1);
s1.add(metrics.metricName("test.s1.count", "grp1"), new WindowedCount());
Sensor s2 = metrics.sensor("test.s2", null, 3);
s2.add(metrics.metricName("test.s2.count", "grp1"), new WindowedCount());
Metrics.ExpireSensorTask purger = metrics.new ExpireSensorTask();
purger.run();
assertNotNull(metrics.getSensor("test.s1"), "Sensor test.s1 must be present");
assertNotNull(metrics.metrics().get(metrics.metricName("test.s1.count", "grp1")), "MetricName test.s1.count must be present");
assertNotNull(metrics.getSensor("test.s2"), "Sensor test.s2 must be present");
assertNotNull(metrics.metrics().get(metrics.metricName("test.s2.count", "grp1")), "MetricName test.s2.count must be present");
time.sleep(1001);
purger.run();
assertNull(metrics.getSensor("test.s1"), "Sensor test.s1 should have been purged");
assertNull(metrics.metrics().get(metrics.metricName("test.s1.count", "grp1")), "MetricName test.s1.count should have been purged");
assertNotNull(metrics.getSensor("test.s2"), "Sensor test.s2 must be present");
assertNotNull(metrics.metrics().get(metrics.metricName("test.s2.count", "grp1")), "MetricName test.s2.count must be present");
// record a value in sensor s2. This should reset the clock for that sensor.
// It should not get purged at the 3 second mark after creation
s2.record();
time.sleep(2000);
purger.run();
assertNotNull(metrics.getSensor("test.s2"), "Sensor test.s2 must be present");
assertNotNull(metrics.metrics().get(metrics.metricName("test.s2.count", "grp1")), "MetricName test.s2.count must be present");
// After another 1001 ms sleep, the metric should be purged
time.sleep(1001);
purger.run();
assertNull(metrics.getSensor("test.s2"), "Sensor test.s2 should have been purged");
assertNull(metrics.metrics().get(metrics.metricName("test.s2.count", "grp1")), "MetricName test.s2.count should have been purged");
// After purging, it should be possible to recreate a metric
s1 = metrics.sensor("test.s1", null, 1);
s1.add(metrics.metricName("test.s1.count", "grp1"), new WindowedCount());
assertNotNull(metrics.getSensor("test.s1"), "Sensor test.s1 must be present");
assertNotNull(metrics.metrics().get(metrics.metricName("test.s1.count", "grp1")), "MetricName test.s1.count must be present");
}
Aggregations