-
Notifications
You must be signed in to change notification settings - Fork 804
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Signed-off-by: Fabian Stäber <fabian@fstab.de>
- Loading branch information
Showing
2 changed files
with
84 additions
and
2 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
77 changes: 77 additions & 0 deletions
77
...heus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/SlidingWindowTest.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,77 @@ | ||
package io.prometheus.metrics.core.metrics; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
public class SlidingWindowTest { | ||
|
||
static class Observer { | ||
|
||
List<Double> values = new ArrayList<>(); | ||
|
||
public void observe(double value) { | ||
values.add(value); | ||
} | ||
|
||
void assertValues(double... expectedValues) { | ||
ArrayList<Double> expectedList = new ArrayList<>(); | ||
for (double expectedValue : expectedValues) { | ||
expectedList.add(expectedValue); | ||
} | ||
Assert.assertEquals(expectedList, values); | ||
} | ||
} | ||
|
||
private final AtomicLong currentTimeMillis = new AtomicLong(); | ||
private SlidingWindow<Observer> ringBuffer; | ||
private final long maxAgeSeconds = 30; | ||
private final int ageBuckets = 5; | ||
private final long timeBetweenRotateMillis = maxAgeSeconds * 1000 / ageBuckets + 2; | ||
|
||
@Before | ||
public void setUp() { | ||
currentTimeMillis.set(System.currentTimeMillis()); | ||
ringBuffer = new SlidingWindow<>(Observer.class, Observer::new, Observer::observe, maxAgeSeconds, ageBuckets); | ||
ringBuffer.currentTimeMillis = currentTimeMillis::get; | ||
} | ||
|
||
@Test | ||
public void testRotate() { | ||
for (int i=0; i<ageBuckets; i++) { | ||
currentTimeMillis.addAndGet(timeBetweenRotateMillis); | ||
ringBuffer.observe(1.0); | ||
} | ||
ringBuffer.current().assertValues(1.0, 1.0, 1.0, 1.0, 1.0); | ||
currentTimeMillis.addAndGet(timeBetweenRotateMillis); | ||
ringBuffer.current().assertValues(1.0, 1.0, 1.0, 1.0); | ||
currentTimeMillis.addAndGet(timeBetweenRotateMillis); | ||
ringBuffer.current().assertValues(1.0, 1.0, 1.0); | ||
currentTimeMillis.addAndGet(timeBetweenRotateMillis); | ||
ringBuffer.current().assertValues(1.0, 1.0); | ||
currentTimeMillis.addAndGet(timeBetweenRotateMillis); | ||
ringBuffer.current().assertValues(1.0); | ||
currentTimeMillis.addAndGet(timeBetweenRotateMillis); | ||
ringBuffer.current().assertValues(); | ||
} | ||
|
||
@Test | ||
public void testMultiRotate() { | ||
ringBuffer.observe(1.0); | ||
currentTimeMillis.addAndGet(2 * timeBetweenRotateMillis); // 2/5 of max aqe | ||
ringBuffer.observe(2.0); | ||
ringBuffer.current().assertValues(1.0, 2.0); | ||
currentTimeMillis.addAndGet(3 * timeBetweenRotateMillis); // 5/5 of max age -> first observation evicted | ||
ringBuffer.current().assertValues(2.0); | ||
ringBuffer.observe(3.0); | ||
ringBuffer.current().assertValues(2.0, 3.0); | ||
currentTimeMillis.addAndGet(2 * timeBetweenRotateMillis); // 7/5 of max age | ||
ringBuffer.current().assertValues(3.0); | ||
currentTimeMillis.addAndGet(3 * timeBetweenRotateMillis); // 10/5 of max age | ||
ringBuffer.current().assertValues(); // empty | ||
} | ||
} |