-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix slowTickIfNecessary with infrequently used EWMA
EWMA.tickIfNecessary does an amount of work that is linear to the amount of time that has passed since the last time the EWMA was ticked. For infrequently used EWMA this can lead to pauses observed in the 700-800 millisecond range after a few hundred days. It's not really necessary to perform every tick as all that is doing is slowly approaching the smallest representable positive number in a double. Instead pick a number close to zero and if the number of ticks required is greater then that don't do the ticks just set the value to close to zero immediately. Actually approaching the smallest representable number is still measurably slow and not particularly useful. To avoid changing the observed output of the EWMA (which previous was only 0.0 if never used) set it close to Double.MIN_NORMAL rather then to 0.0
- Loading branch information
Showing
3 changed files
with
63 additions
and
4 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
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
26 changes: 26 additions & 0 deletions
26
metrics-core/src/test/java/com/codahale/metrics/ExponentialMovingAveragesTest.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,26 @@ | ||
package com.codahale.metrics; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class ExponentialMovingAveragesTest | ||
{ | ||
@Test | ||
public void testMaxTicks() | ||
{ | ||
Clock clock = mock(Clock.class); | ||
when(clock.getTick()).thenReturn(0L, Long.MAX_VALUE); | ||
ExponentialMovingAverages ema = new ExponentialMovingAverages(clock); | ||
ema.update(Long.MAX_VALUE); | ||
ema.tickIfNecessary(); | ||
long secondNanos = TimeUnit.SECONDS.toNanos(1); | ||
assertEquals(ema.getM1Rate(), Double.MIN_NORMAL * secondNanos, 0.0); | ||
assertEquals(ema.getM5Rate(), Double.MIN_NORMAL * secondNanos, 0.0); | ||
assertEquals(ema.getM15Rate(), Double.MIN_NORMAL * secondNanos, 0.0); | ||
} | ||
} |