|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.kafka.common.metrics.stats; |
| 18 | + |
| 19 | +import org.apache.kafka.common.metrics.MetricConfig; |
| 20 | +import org.apache.kafka.common.metrics.internals.MetricsUtils; |
| 21 | +import org.apache.kafka.common.utils.MockTime; |
| 22 | +import org.apache.kafka.common.utils.Time; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.params.ParameterizedTest; |
| 25 | +import org.junit.jupiter.params.provider.CsvSource; |
| 26 | + |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 31 | + |
| 32 | +public class RateTest { |
| 33 | + private static final double EPS = 0.000001; |
| 34 | + private Rate r; |
| 35 | + private Time timeClock; |
| 36 | + |
| 37 | + @BeforeEach |
| 38 | + public void setup() { |
| 39 | + r = new Rate(); |
| 40 | + timeClock = new MockTime(); |
| 41 | + } |
| 42 | + |
| 43 | + // Tests the scenario where the recording and measurement is done before the window for first sample finishes |
| 44 | + // with no prior samples retained. |
| 45 | + @ParameterizedTest |
| 46 | + @CsvSource({"1,1", "1,11", "11,1", "11,11"}) |
| 47 | + public void testRateWithNoPriorAvailableSamples(int numSample, int sampleWindowSizeSec) { |
| 48 | + final MetricConfig config = new MetricConfig().samples(numSample).timeWindow(sampleWindowSizeSec, TimeUnit.SECONDS); |
| 49 | + final double sampleValue = 50.0; |
| 50 | + // record at beginning of the window |
| 51 | + r.record(config, sampleValue, timeClock.milliseconds()); |
| 52 | + // forward time till almost the end of window |
| 53 | + final long measurementTime = TimeUnit.SECONDS.toMillis(sampleWindowSizeSec) - 1; |
| 54 | + timeClock.sleep(measurementTime); |
| 55 | + // calculate rate at almost the end of window |
| 56 | + final double observedRate = r.measure(config, timeClock.milliseconds()); |
| 57 | + assertFalse(Double.isNaN(observedRate)); |
| 58 | + |
| 59 | + // In a scenario where sufficient number of samples is not available yet, the rate calculation algorithm assumes |
| 60 | + // presence of N-1 (where N = numSample) prior samples with sample values of 0. Hence, the window size for rate |
| 61 | + // calculation accounts for N-1 prior samples |
| 62 | + final int dummyPriorSamplesAssumedByAlgorithm = numSample - 1; |
| 63 | + final double windowSize = MetricsUtils.convert(measurementTime, TimeUnit.SECONDS) + (dummyPriorSamplesAssumedByAlgorithm * sampleWindowSizeSec); |
| 64 | + double expectedRatePerSec = sampleValue / windowSize; |
| 65 | + assertEquals(expectedRatePerSec, observedRate, EPS); |
| 66 | + } |
| 67 | +} |
0 commit comments