Skip to content

Commit

Permalink
DBZ-6998: Fix StatisticsTest
Browse files Browse the repository at this point in the history
Co-authored-by: cjho0316 <[email protected]>
  • Loading branch information
2 people authored and nancyxu123 committed Oct 21, 2024
1 parent a690077 commit 1d03ec2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public abstract class Metric {

private final AtomicReference<Duration> minimum = new AtomicReference<>(Duration.ZERO);
private final AtomicReference<Duration> minimum = new AtomicReference<>(Duration.ofSeconds(Long.MAX_VALUE));
private final AtomicReference<Duration> maximum = new AtomicReference<>(Duration.ZERO);
private final AtomicReference<Duration> last = new AtomicReference<>(Duration.ZERO);
private final AtomicReference<Duration> total = new AtomicReference<>(Duration.ZERO);
Expand All @@ -28,7 +28,7 @@ public Metric(Duration percentageMetricsClearInterval, Consumer<Throwable> error
* Resets the duration metric
*/
public void reset() {
minimum.set(Duration.ZERO);
minimum.set(Duration.ofSeconds(Long.MAX_VALUE));
maximum.set(Duration.ZERO);
last.set(Duration.ZERO);
total.set(Duration.ZERO);
Expand Down Expand Up @@ -60,20 +60,20 @@ void set(Duration lastDuration) {
}

total.accumulateAndGet(lastDuration, Duration::plus);
count.incrementAndGet();

average.set(
average.get()
.multipliedBy(count.get() - 1)
.plus(lastDuration)
.dividedBy(count.get()));
total.get()
.dividedBy(count.get())
);

last.set(lastDuration);

quantileMeter.addValue((double) lastDuration.toMillis());
quantileMeter.addValue((double) lastDuration.toSeconds());
}

public synchronized void update(long value) {
set(Duration.ofMillis(value));
set(Duration.ofSeconds(value));
}

public Duration getMinValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
import org.junit.jupiter.api.Test;

class StatisticsTest {
Double durationToFloat(Duration duration) {
return duration.getSeconds() + (double) duration.getNano() / 1_000_000_000;
}

@Test
void updateAndReset() {
double epsilon = 0.000000001;

Statistics statistics = new Statistics(Duration.ofSeconds(10), null);
statistics.update(148);
statistics.update(197);
Expand All @@ -23,13 +28,13 @@ void updateAndReset() {
statistics.update(10);
statistics.update(298);

Assertions.assertEquals(170.71428571428572, statistics.getAverageValue());
Assertions.assertEquals(170.71428571428572, durationToFloat(statistics.getAverageValue()), epsilon);

Assertions.assertEquals(298, statistics.getLastValue());
Assertions.assertEquals(298, durationToFloat(statistics.getLastValue()));

Assertions.assertEquals(397, statistics.getMaxValue());
Assertions.assertEquals(397, durationToFloat(statistics.getMaxValue()));

Assertions.assertEquals(10, statistics.getMinValue());
Assertions.assertEquals(10, durationToFloat(statistics.getMinValue()));

statistics.reset();

Expand Down

0 comments on commit 1d03ec2

Please sign in to comment.