Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent NaN values when calculating mean #1230

Merged
merged 3 commits into from Dec 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -174,6 +174,9 @@ private void rescale(long now, long next) {
for (Double key : keys) {
final WeightedSample sample = values.remove(key);
final WeightedSample newSample = new WeightedSample(sample.value, sample.weight * scalingFactor);
if (Double.compare(newSample.weight, 0) == 0) {
continue;
}
values.put(key * scalingFactor, newSample);
}
}
Expand Down
@@ -1,5 +1,6 @@
package com.codahale.metrics;

import com.codahale.metrics.Timer.Context;
import org.junit.Test;

import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -140,6 +141,22 @@ public void emptyReservoirSnapshot_shouldReturnZeroForAllValues() {
assertThat(snapshot.size()).isEqualTo(0);
}

@Test
public void removeZeroWeightsInSamplesToPreventNaNInMeanValues() {
final ManualClock clock = new ManualClock();
final ExponentiallyDecayingReservoir reservoir = new ExponentiallyDecayingReservoir(1028, 0.015, clock);
Timer timer = new Timer(reservoir, clock);

Context context = timer.time();
clock.addMillis(100);
context.stop();

for (int i = 1; i < 48; i++) {
clock.addHours(1);
assertThat(reservoir.getSnapshot().getMean()).isBetween(0.0, Double.MAX_VALUE);
}
}

@Test
public void multipleUpdatesAfterlongPeriodsOfInactivityShouldNotCorruptSamplingState() throws Exception {
// This test illustrates the potential race condition in rescale that
Expand Down