emitskevich-blp commented on code in PR #15889: URL: https://github.com/apache/kafka/pull/15889#discussion_r1617784166
########## clients/src/test/java/org/apache/kafka/common/metrics/stats/FrequenciesTest.java: ########## @@ -102,58 +110,69 @@ public void testBooleanFrequencies() { } @Test - public void testUseWithMetrics() { - MetricName name1 = name("1"); - MetricName name2 = name("2"); - MetricName name3 = name("3"); - MetricName name4 = name("4"); - Frequencies frequencies = new Frequencies(4, 1.0, 4.0, - new Frequency(name1, 1.0), - new Frequency(name2, 2.0), - new Frequency(name3, 3.0), - new Frequency(name4, 4.0)); + public void testWithMetricsStrategy1() { + Frequencies frequencies = new Frequencies(4, 1.0, 4.0, freq("1", 1.0), + freq("2", 2.0), freq("3", 3.0), freq("4", 4.0)); Sensor sensor = metrics.sensor("test", config); sensor.add(frequencies); - Metric metric1 = this.metrics.metrics().get(name1); - Metric metric2 = this.metrics.metrics().get(name2); - Metric metric3 = this.metrics.metrics().get(name3); - Metric metric4 = this.metrics.metrics().get(name4); - // Record 2 windows worth of values - for (int i = 0; i != 100; ++i) { + // Record 100 events uniformly between all buckets + for (int i = 0; i < 100; ++i) { frequencies.record(config, i % 4 + 1, time.milliseconds()); } - assertEquals(0.25, (Double) metric1.metricValue(), DELTA); - assertEquals(0.25, (Double) metric2.metricValue(), DELTA); - assertEquals(0.25, (Double) metric3.metricValue(), DELTA); - assertEquals(0.25, (Double) metric4.metricValue(), DELTA); + assertEquals(0.25, metricValueFor("1"), DELTA); + assertEquals(0.25, metricValueFor("2"), DELTA); + assertEquals(0.25, metricValueFor("3"), DELTA); + assertEquals(0.25, metricValueFor("4"), DELTA); + } + + @Test + public void testWithMetricsStrategy2() { + Frequencies frequencies = new Frequencies(4, 1.0, 4.0, freq("1", 1.0), + freq("2", 2.0), freq("3", 3.0), freq("4", 4.0)); + Sensor sensor = metrics.sensor("test", config); + sensor.add(frequencies); + + // Record 100 events half-half between 1st and 2nd buckets + for (int i = 0; i < 100; ++i) { + frequencies.record(config, i % 2 + 1, time.milliseconds()); + } + assertEquals(0.50, metricValueFor("1"), DELTA); + assertEquals(0.50, metricValueFor("2"), DELTA); + assertEquals(0.00, metricValueFor("3"), DELTA); + assertEquals(0.00, metricValueFor("4"), DELTA); + } - // Record 2 windows worth of values - for (int i = 0; i != 100; ++i) { + @Test + public void testWithMetricsStrategy3() { + Frequencies frequencies = new Frequencies(4, 1.0, 4.0, freq("1", 1.0), + freq("2", 2.0), freq("3", 3.0), freq("4", 4.0)); + Sensor sensor = metrics.sensor("test", config); + sensor.add(frequencies); + + // Record 50 events half-half between 1st and 2nd buckets + for (int i = 0; i < 50; ++i) { frequencies.record(config, i % 2 + 1, time.milliseconds()); } - assertEquals(0.50, (Double) metric1.metricValue(), DELTA); - assertEquals(0.50, (Double) metric2.metricValue(), DELTA); - assertEquals(0.00, (Double) metric3.metricValue(), DELTA); - assertEquals(0.00, (Double) metric4.metricValue(), DELTA); - - // Record 1 window worth of values to overlap with the last window - // that is half 1.0 and half 2.0 - for (int i = 0; i != 50; ++i) { + // Record 50 events to 4th bucket + for (int i = 0; i < 50; ++i) { frequencies.record(config, 4.0, time.milliseconds()); } - assertEquals(0.25, (Double) metric1.metricValue(), DELTA); - assertEquals(0.25, (Double) metric2.metricValue(), DELTA); - assertEquals(0.00, (Double) metric3.metricValue(), DELTA); - assertEquals(0.50, (Double) metric4.metricValue(), DELTA); + assertEquals(0.25, metricValueFor("1"), DELTA); + assertEquals(0.25, metricValueFor("2"), DELTA); + assertEquals(0.00, metricValueFor("3"), DELTA); + assertEquals(0.50, metricValueFor("4"), DELTA); } - protected MetricName name(String metricName) { + private MetricName name(String metricName) { Review Comment: Tried to make now `metricName`, `freq` and `metricValue` consistent in naming, please check if it's ok -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org