use of org.apache.kafka.common.metrics.CompoundStat.NamedMeasurable in project apache-kafka-on-k8s by banzaicloud.
the class FrequenciesTest method testBooleanFrequencies.
@Test
public void testBooleanFrequencies() {
MetricName metricTrue = name("true");
MetricName metricFalse = name("false");
Frequencies frequencies = Frequencies.forBooleanValues(metricFalse, metricTrue);
final NamedMeasurable falseMetric = frequencies.stats().get(0);
final NamedMeasurable trueMetric = frequencies.stats().get(1);
// Record 2 windows worth of values
for (int i = 0; i != 25; ++i) {
frequencies.record(config, 0.0, time.milliseconds());
}
for (int i = 0; i != 75; ++i) {
frequencies.record(config, 1.0, time.milliseconds());
}
assertEquals(0.25, falseMetric.stat().measure(config, time.milliseconds()), DELTA);
assertEquals(0.75, trueMetric.stat().measure(config, time.milliseconds()), DELTA);
// Record 2 more windows worth of values
for (int i = 0; i != 40; ++i) {
frequencies.record(config, 0.0, time.milliseconds());
}
for (int i = 0; i != 60; ++i) {
frequencies.record(config, 1.0, time.milliseconds());
}
assertEquals(0.40, falseMetric.stat().measure(config, time.milliseconds()), DELTA);
assertEquals(0.60, trueMetric.stat().measure(config, time.milliseconds()), DELTA);
}
use of org.apache.kafka.common.metrics.CompoundStat.NamedMeasurable in project apache-kafka-on-k8s by banzaicloud.
the class Sensor method add.
/**
* Register a compound statistic with this sensor which yields multiple measurable quantities (like a histogram)
* @param stat The stat to register
* @param config The configuration for this stat. If null then the stat will use the default configuration for this
* sensor.
* @return true if stat is added to sensor, false if sensor is expired
*/
public synchronized boolean add(CompoundStat stat, MetricConfig config) {
if (hasExpired())
return false;
this.stats.add(Utils.notNull(stat));
Object lock = new Object();
for (NamedMeasurable m : stat.stats()) {
KafkaMetric metric = new KafkaMetric(lock, m.name(), m.stat(), config == null ? this.config : config, time);
this.registry.registerMetric(metric);
this.metrics.add(metric);
}
return true;
}
use of org.apache.kafka.common.metrics.CompoundStat.NamedMeasurable in project apache-kafka-on-k8s by banzaicloud.
the class MeterTest method testMeter.
@Test
public void testMeter() {
Map<String, String> emptyTags = Collections.emptyMap();
MetricName rateMetricName = new MetricName("rate", "test", "", emptyTags);
MetricName totalMetricName = new MetricName("total", "test", "", emptyTags);
Meter meter = new Meter(rateMetricName, totalMetricName);
List<NamedMeasurable> stats = meter.stats();
assertEquals(2, stats.size());
NamedMeasurable total = stats.get(0);
NamedMeasurable rate = stats.get(1);
assertEquals(rateMetricName, rate.name());
assertEquals(totalMetricName, total.name());
Rate rateStat = (Rate) rate.stat();
Total totalStat = (Total) total.stat();
MetricConfig config = new MetricConfig();
double nextValue = 0.0;
double expectedTotal = 0.0;
long now = 0;
double intervalMs = 100;
double delta = 5.0;
// for time windows and that the total is cumulative.
for (int i = 1; i <= 100; i++) {
for (; now < i * 1000; now += intervalMs, nextValue += delta) {
expectedTotal += nextValue;
meter.record(config, nextValue, now);
}
assertEquals(expectedTotal, totalStat.measure(config, now), EPS);
long windowSizeMs = rateStat.windowSize(config, now);
long windowStartMs = Math.max(now - windowSizeMs, 0);
double sampledTotal = 0.0;
double prevValue = nextValue - delta;
for (long timeMs = now - 100; timeMs >= windowStartMs; timeMs -= intervalMs, prevValue -= delta) sampledTotal += prevValue;
assertEquals(sampledTotal * 1000 / windowSizeMs, rateStat.measure(config, now), EPS);
}
}
use of org.apache.kafka.common.metrics.CompoundStat.NamedMeasurable in project kafka by apache.
the class FrequenciesTest method testBooleanFrequencies.
@Test
public void testBooleanFrequencies() {
MetricName metricTrue = name("true");
MetricName metricFalse = name("false");
Frequencies frequencies = Frequencies.forBooleanValues(metricFalse, metricTrue);
final NamedMeasurable falseMetric = frequencies.stats().get(0);
final NamedMeasurable trueMetric = frequencies.stats().get(1);
// Record 2 windows worth of values
for (int i = 0; i != 25; ++i) {
frequencies.record(config, 0.0, time.milliseconds());
}
for (int i = 0; i != 75; ++i) {
frequencies.record(config, 1.0, time.milliseconds());
}
assertEquals(0.25, falseMetric.stat().measure(config, time.milliseconds()), DELTA);
assertEquals(0.75, trueMetric.stat().measure(config, time.milliseconds()), DELTA);
// Record 2 more windows worth of values
for (int i = 0; i != 40; ++i) {
frequencies.record(config, 0.0, time.milliseconds());
}
for (int i = 0; i != 60; ++i) {
frequencies.record(config, 1.0, time.milliseconds());
}
assertEquals(0.40, falseMetric.stat().measure(config, time.milliseconds()), DELTA);
assertEquals(0.60, trueMetric.stat().measure(config, time.milliseconds()), DELTA);
}
use of org.apache.kafka.common.metrics.CompoundStat.NamedMeasurable in project kafka by apache.
the class MeterTest method testMeter.
@Test
public void testMeter() {
Map<String, String> emptyTags = Collections.emptyMap();
MetricName rateMetricName = new MetricName("rate", "test", "", emptyTags);
MetricName totalMetricName = new MetricName("total", "test", "", emptyTags);
Meter meter = new Meter(rateMetricName, totalMetricName);
List<NamedMeasurable> stats = meter.stats();
assertEquals(2, stats.size());
NamedMeasurable total = stats.get(0);
NamedMeasurable rate = stats.get(1);
assertEquals(rateMetricName, rate.name());
assertEquals(totalMetricName, total.name());
Rate rateStat = (Rate) rate.stat();
CumulativeSum totalStat = (CumulativeSum) total.stat();
MetricConfig config = new MetricConfig();
double nextValue = 0.0;
double expectedTotal = 0.0;
long now = 0;
double intervalMs = 100;
double delta = 5.0;
// for time windows and that the total is cumulative.
for (int i = 1; i <= 100; i++) {
for (; now < i * 1000; now += intervalMs, nextValue += delta) {
expectedTotal += nextValue;
meter.record(config, nextValue, now);
}
assertEquals(expectedTotal, totalStat.measure(config, now), EPS);
long windowSizeMs = rateStat.windowSize(config, now);
long windowStartMs = Math.max(now - windowSizeMs, 0);
double sampledTotal = 0.0;
double prevValue = nextValue - delta;
for (long timeMs = now - 100; timeMs >= windowStartMs; timeMs -= intervalMs, prevValue -= delta) sampledTotal += prevValue;
assertEquals(sampledTotal * 1000 / windowSizeMs, rateStat.measure(config, now), EPS);
}
}
Aggregations