use of org.apache.kafka.clients.consumer.internals.ConsumerMetadata in project kafka by apache.
the class KafkaConsumerTest method testPollIdleRatio.
@Test
public void testPollIdleRatio() {
ConsumerMetadata metadata = createMetadata(subscription);
MockClient client = new MockClient(time, metadata);
initMetadata(client, Collections.singletonMap(topic, 1));
KafkaConsumer<String, String> consumer = newConsumer(time, client, subscription, metadata, assignor, true, groupInstanceId);
// MetricName object to check
Metrics metrics = consumer.metrics;
MetricName pollIdleRatio = metrics.metricName("poll-idle-ratio-avg", "consumer-metrics");
// Test default value
assertEquals(Double.NaN, consumer.metrics().get(pollIdleRatio).metricValue());
// 1st poll
// Spend 50ms in poll so value = 1.0
consumer.kafkaConsumerMetrics.recordPollStart(time.milliseconds());
time.sleep(50);
consumer.kafkaConsumerMetrics.recordPollEnd(time.milliseconds());
assertEquals(1.0d, consumer.metrics().get(pollIdleRatio).metricValue());
// 2nd poll
// Spend 50m outside poll and 0ms in poll so value = 0.0
time.sleep(50);
consumer.kafkaConsumerMetrics.recordPollStart(time.milliseconds());
consumer.kafkaConsumerMetrics.recordPollEnd(time.milliseconds());
// Avg of first two data points
assertEquals((1.0d + 0.0d) / 2, consumer.metrics().get(pollIdleRatio).metricValue());
// 3rd poll
// Spend 25ms outside poll and 25ms in poll so value = 0.5
time.sleep(25);
consumer.kafkaConsumerMetrics.recordPollStart(time.milliseconds());
time.sleep(25);
consumer.kafkaConsumerMetrics.recordPollEnd(time.milliseconds());
// Avg of three data points
assertEquals((1.0d + 0.0d + 0.5d) / 3, consumer.metrics().get(pollIdleRatio).metricValue());
}
Aggregations