Skip to content

Instantly share code, notes, and snippets.

@altery
Created August 10, 2017 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save altery/0efd06c4f8d0c83a44bd718e8170a49e to your computer and use it in GitHub Desktop.
Save altery/0efd06c4f8d0c83a44bd718e8170a49e to your computer and use it in GitHub Desktop.
import com.codahale.metrics.ExponentiallyDecayingReservoir;
import com.codahale.metrics.Timer;
import com.codahale.metrics.Timer.Context;
public class DropwizardMeanBug {
public static void main (String[] args) throws InterruptedException {
MyClock clock= new MyClock(System.currentTimeMillis());
ExponentiallyDecayingReservoir reservoir= new ExponentiallyDecayingReservoir(1028, 0.015, clock);
Timer timer= new Timer(reservoir, clock);
Context context= timer.time();
clock.progressInTime(100);
context.stop();
for (int i= 1; i < 24; i++) {
System.out.println("Time: " + clock.getTime() + " Mean: " + timer.getSnapshot().getMean());
clock.progressInTime(i * 60 * 60 * 1000);
}
}
}
import com.codahale.metrics.Clock;
public class MyClock extends Clock {
private long millis;
public MyClock (long millis) {
this.millis= millis;
}
@Override
public long getTick () {
return this.millis * 1000000;
}
@Override
public long getTime () {
return this.millis;
}
public void progressInTime (long millis) {
this.millis+= millis;
}
}
@altery
Copy link
Author

altery commented Aug 10, 2017

The output of this example is:

Time: 1502354348353 Mean: 1.0E8
Time: 1502357948353 Mean: 1.0E8
Time: 1502365148353 Mean: 1.0E8
Time: 1502375948353 Mean: 1.0E8
Time: 1502390348353 Mean: 1.0E8
Time: 1502408348353 Mean: NaN
Time: 1502429948353 Mean: NaN
Time: 1502455148353 Mean: NaN
Time: 1502483948353 Mean: NaN
Time: 1502516348353 Mean: NaN
Time: 1502552348353 Mean: NaN
Time: 1502591948353 Mean: NaN
Time: 1502635148353 Mean: NaN
Time: 1502681948353 Mean: NaN
Time: 1502732348353 Mean: 0.0
Time: 1502786348353 Mean: 0.0
Time: 1502843948353 Mean: 0.0
Time: 1502905148353 Mean: 0.0
Time: 1502969948353 Mean: 0.0
Time: 1503038348353 Mean: 0.0
Time: 1503110348353 Mean: 0.0
Time: 1503185948353 Mean: 0.0
Time: 1503265148353 Mean: 0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment