Repository: apache/kafka
File: metadata/src/test/java/org/apache/kafka/controller/metrics/QuorumControllerMetricsTest.java (Java)

Code before:
            assertEquals(1000, timeSinceLastHeartbeatReceivedMs.value());
            metrics.updateBrokerContactTime(brokerId);
            assertEquals(0, timeSinceLastHeartbeatReceivedMs.value());
            time.sleep(100000);
            assertEquals(sessionTimeoutMs, timeSinceLastHeartbeatReceivedMs.value());
            metrics.removeTimeSinceLastHeartbeatMetrics();
            assertEquals(numMetrics - 1, registry.allMetrics().size());
        } finally {
            registry.shutdown();
        }
    }

    @Test
    public void testAvgIdleRatio() {
        MetricsRegistry registry = new MetricsRegistry();
        MockTime time = new MockTime();
        try (QuorumControllerMetrics metrics = new QuorumControllerMetrics(Optional.of(registry), time, 9000)) {
            Gauge<Double> avgIdleRatio = (Gauge<Double>) registry.allMetrics().get(metricName("ControllerEventManager", "AvgIdleRatio"));

            // No idle time recorded yet
            assertEquals(1.0, avgIdleRatio.value());

            metrics.updateIdleTime(10);
            time.sleep(40);
            metrics.updateIdleTime(20);
            assertEquals(0.5, avgIdleRatio.value());

            time.sleep(20);
            metrics.updateIdleTime(1);
            assertEquals(0.05, avgIdleRatio.value());

        } finally {
            registry.shutdown();
        }
    }

    private static void assertMetricHistogram(MetricsRegistry registry, MetricName metricName, long count, double sum) {
        Histogram histogram = (Histogram) registry.allMetrics().get(metricName);

        assertEquals(count, histogram.count());
        assertEquals(sum, histogram.sum(), .1);
    }

    private static MetricName metricName(String type, String name) {
        String mBeanName = String.format("kafka.controller:type=%s,name=%s", type, name);
        return new MetricName("kafka.controller", type, name, null, mBeanName);
    }

    private static MetricName metricName(String type, String name, String scope) {
        String mBeanName = String.format("kafka.controller:type=%s,name=%s,%s", type, name, scope);
        return new MetricName("kafka.controller", type, name, scope, mBeanName);

Review comment (suggestion):
The `value()` of avg idle ration is .5 is because this recording is dropped. Please document this.

Code after:
            assertEquals(1000, timeSinceLastHeartbeatReceivedMs.value());
            metrics.updateBrokerContactTime(brokerId);
            assertEquals(0, timeSinceLastHeartbeatReceivedMs.value());
            time.sleep(100000);
            assertEquals(sessionTimeoutMs, timeSinceLastHeartbeatReceivedMs.value());
            metrics.removeTimeSinceLastHeartbeatMetrics();
            assertEquals(numMetrics - 1, registry.allMetrics().size());
        } finally {
            registry.shutdown();
        }
    }

    @Test
    public void testAvgIdleRatio() {
        final double delta = 0.001;
        MetricsRegistry registry = new MetricsRegistry();
        MockTime time = new MockTime();
        try (QuorumControllerMetrics metrics = new QuorumControllerMetrics(Optional.of(registry), time, 9000)) {
            Gauge<Double> avgIdleRatio = (Gauge<Double>) registry.allMetrics().get(metricName("ControllerEventManager", "AvgIdleRatio"));

            // No idle time recorded yet; returns default ratio of 1.0
            assertEquals(1.0, avgIdleRatio.value(), delta);

            // First recording is dropped to establish the interval start time
            // This is because TimeRatio needs an initial timestamp to measure intervals from
            metrics.updateIdleTime(10);
            time.sleep(40);
            metrics.updateIdleTime(20);
            // avgIdleRatio = (20ms idle) / (40ms interval) = 0.5
            assertEquals(0.5, avgIdleRatio.value(), delta);

            time.sleep(20);
            metrics.updateIdleTime(1);
            // avgIdleRatio = (1ms idle) / (20ms interval) = 0.05
            assertEquals(0.05, avgIdleRatio.value(), delta);

        } finally {
            registry.shutdown();
        }
    }

    private static void assertMetricHistogram(MetricsRegistry registry, MetricName metricName, long count, double sum) {
        Histogram histogram = (Histogram) registry.allMetrics().get(metricName);

        assertEquals(count, histogram.count());
        assertEquals(sum, histogram.sum(), .1);
    }

    private static MetricName metricName(String type, String name) {
        String mBeanName = String.format("kafka.controller:type=%s,name=%s", type, name);
        return new MetricName("kafka.controller", type, name, null, mBeanName);