-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent NaN values when calculating mean
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...ation/src/test/java/io/smallrye/metrics/histogram/ExponentiallyWeightedReservoirTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.smallrye.metrics.histogram; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.not; | ||
|
||
import org.eclipse.microprofile.metrics.Histogram; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import io.smallrye.metrics.app.ExponentiallyDecayingReservoir; | ||
import io.smallrye.metrics.app.HistogramImpl; | ||
|
||
public class ExponentiallyWeightedReservoirTest { | ||
|
||
@Test | ||
public void removeZeroWeightsInSamplesToPreventNaNInMeanValues() { | ||
final TestingClock clock = new TestingClock(); | ||
final ExponentiallyDecayingReservoir reservoir = new ExponentiallyDecayingReservoir(1028, 0.015, clock); | ||
Histogram histogram = new HistogramImpl(reservoir); | ||
|
||
histogram.update(100); | ||
|
||
for (int i = 1; i < 48; i++) { | ||
clock.addHours(1); | ||
Assert.assertThat(reservoir.getSnapshot().getMean(), not(equalTo(Double.NaN))); | ||
} | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
implementation/src/test/java/io/smallrye/metrics/histogram/TestingClock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.smallrye.metrics.histogram; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import io.smallrye.metrics.app.Clock; | ||
|
||
public class TestingClock extends Clock { | ||
private final long initialTicksInNanos; | ||
long ticksInNanos; | ||
|
||
public TestingClock(long initialTicksInNanos) { | ||
this.initialTicksInNanos = initialTicksInNanos; | ||
this.ticksInNanos = initialTicksInNanos; | ||
} | ||
|
||
public TestingClock() { | ||
this(0L); | ||
} | ||
|
||
public synchronized void addNanos(long nanos) { | ||
ticksInNanos += nanos; | ||
} | ||
|
||
public synchronized void addSeconds(long seconds) { | ||
ticksInNanos += TimeUnit.SECONDS.toNanos(seconds); | ||
} | ||
|
||
public synchronized void addMillis(long millis) { | ||
ticksInNanos += TimeUnit.MILLISECONDS.toNanos(millis); | ||
} | ||
|
||
public synchronized void addHours(long hours) { | ||
ticksInNanos += TimeUnit.HOURS.toNanos(hours); | ||
} | ||
|
||
@Override | ||
public synchronized long getTick() { | ||
return ticksInNanos; | ||
} | ||
|
||
@Override | ||
public synchronized long getTime() { | ||
return TimeUnit.NANOSECONDS.toMillis(ticksInNanos - initialTicksInNanos); | ||
} | ||
|
||
} |