-
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 then bound the number of ticks to allow that to be reachable from the largest value representable by the EWMA. Actually approaching the smallest representable number is still measurably slow and not particularly useful.
- Loading branch information
Showing
2 changed files
with
40 additions
and
1 deletion.
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
23 changes: 23 additions & 0 deletions
23
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,23 @@ | ||
package com.codahale.metrics; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
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(); | ||
assertTrue (ema.getM1Rate() < ExponentialMovingAverages.maxTickZeroTarget); | ||
assertTrue (ema.getM5Rate() < ExponentialMovingAverages.maxTickZeroTarget); | ||
assertTrue (ema.getM15Rate() < ExponentialMovingAverages.maxTickZeroTarget); | ||
} | ||
} |