Java Code Examples for com.codahale.metrics.Reservoir#update()

The following examples show how to use com.codahale.metrics.Reservoir#update() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UniformAccumulatorTest.java    From rolling-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCacheSnapshot() {
    Reservoir reservoir = new HdrBuilder().neverResetReservoir().buildReservoir();

    reservoir.update(10);
    reservoir.update(20);
    Snapshot firstSnapshot = reservoir.getSnapshot();

    reservoir.update(30);
    reservoir.update(40);
    Snapshot secondSnapshot = reservoir.getSnapshot();
    assertNotSame(firstSnapshot, secondSnapshot);
    assertEquals(10, secondSnapshot.getMin());
    assertEquals(40, secondSnapshot.getMax());

    reservoir.update(9);
    reservoir.update(60);
    Snapshot thirdSnapshot = reservoir.getSnapshot();
    assertNotSame(secondSnapshot, thirdSnapshot);
    assertEquals(9, thirdSnapshot.getMin());
    assertEquals(60, thirdSnapshot.getMax());
}
 
Example 2
Source File: ResetOnSnapshotAccumulatorTest.java    From rolling-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCacheSnapshot() {
    Reservoir reservoir = new HdrBuilder().resetReservoirOnSnapshot().buildReservoir();

    reservoir.update(10);
    reservoir.update(20);
    Snapshot firstSnapshot = reservoir.getSnapshot();

    reservoir.update(30);
    reservoir.update(40);
    Snapshot secondSnapshot = reservoir.getSnapshot();
    assertNotSame(firstSnapshot, secondSnapshot);
    assertEquals(30, secondSnapshot.getMin());
    assertEquals(40, secondSnapshot.getMax());

    reservoir.update(50);
    reservoir.update(60);
    Snapshot thirdSnapshot = reservoir.getSnapshot();
    assertNotSame(secondSnapshot, thirdSnapshot);
    assertEquals(50, thirdSnapshot.getMin());
    assertEquals(60, thirdSnapshot.getMax());
}
 
Example 3
Source File: OverflowResolverTest.java    From rolling-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void testSkipBigValues() {
    Reservoir reservoir = new HdrBuilder().withHighestTrackableValue(100, OverflowResolver.SKIP).buildReservoir();

    reservoir.update(101);
    Snapshot snapshot = reservoir.getSnapshot();
    assertEquals(0, snapshot.getMax());

    reservoir.update(100);
    snapshot = reservoir.getSnapshot();
    assertEquals(100, snapshot.getMax());

    reservoir.update(99);
    snapshot = reservoir.getSnapshot();
    assertEquals(99, snapshot.getMin());
}
 
Example 4
Source File: OverflowResolverTest.java    From rolling-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void testReduceBigValuesToMax() {
    Reservoir reservoir = new HdrBuilder().withHighestTrackableValue(100, OverflowResolver.REDUCE_TO_HIGHEST_TRACKABLE).buildReservoir();

    reservoir.update(101);
    Snapshot snapshot = reservoir.getSnapshot();
    assertEquals(100, snapshot.getMax());

    reservoir.update(100);
    snapshot = reservoir.getSnapshot();
    assertEquals(100, snapshot.getMax());

    reservoir.update(99);
    snapshot = reservoir.getSnapshot();
    assertEquals(99, snapshot.getMin());
}
 
Example 5
Source File: TDigestReservoirTest.java    From kite with Apache License 2.0 6 votes vote down vote up
@Test
public void testBasic() throws Exception {
  double maxDelta = 0.00000000001;
  Reservoir reservoir = new TDigestReservoir();
  assertEquals(0, reservoir.size());
  reservoir.update(100);
  reservoir.update(101);
  reservoir.update(300);
  assertEquals(3, reservoir.size());
  assertEquals(3, reservoir.getSnapshot().size());    
  assertEquals(100, reservoir.getSnapshot().getMin());    
  assertEquals(300, reservoir.getSnapshot().getMax());    
  assertEquals(101, reservoir.getSnapshot().getMedian(), maxDelta);    
  assertEquals(101, reservoir.getSnapshot().getValue(0.5), maxDelta);    
  assertEquals(501.0 / 3, reservoir.getSnapshot().getMean(), maxDelta);    
}
 
Example 6
Source File: HistogramUtil.java    From rolling-metrics with Apache License 2.0 5 votes vote down vote up
public static void runInParallel(Reservoir reservoir, long durationMillis) throws InterruptedException {
    AtomicReference<Throwable> errorRef = new AtomicReference<>();

    Thread[] threads = new Thread[Runtime.getRuntime().availableProcessors() * 2];
    long start = System.currentTimeMillis();
    final CountDownLatch latch = new CountDownLatch(threads.length);
    for (int i = 0; i < threads.length; i++) {
        threads[i] = new Thread(() -> {
            try {
                // update reservoir 100 times and take snapshot on each cycle
                while (errorRef.get() == null && System.currentTimeMillis() - start < durationMillis) {
                    for (int j = 1; j <= 10; j++) {
                        reservoir.update(ThreadLocalRandom.current().nextInt(j));
                    }
                    reservoir.getSnapshot();
                }
            } catch (Exception e){
                e.printStackTrace();
                errorRef.set(e);
            } finally {
                latch.countDown();
            }
        });
        threads[i].start();
    }
    latch.await();
    //latch.await(duration.toMillis() + 4000, TimeUnit.MILLISECONDS);
    if (latch.getCount() > 0) {
        throw new IllegalStateException("" + latch.getCount() + " was not completed");
    }
    if (errorRef.get() != null) {
        throw new RuntimeException(errorRef.get());
    }
}
 
Example 7
Source File: OverflowResolverTest.java    From rolling-metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void testPassThruBigValues2() {
    Reservoir reservoir = new HdrBuilder()
            .withHighestTrackableValue(100, OverflowResolver.PASS_THRU)
            .buildReservoir();
    reservoir.update(101);
    Snapshot snapshot = reservoir.getSnapshot();
    assertEquals(101, snapshot.getMax());
}
 
Example 8
Source File: ResetPeriodicallyAccumulatorTest.java    From rolling-metrics with Apache License 2.0 4 votes vote down vote up
@Test
public void test() {
    AtomicLong time = new AtomicLong(0);
    Clock wallClock = Clock.mock(time);
    Reservoir reservoir = new HdrBuilder(wallClock)
            .resetReservoirPeriodically(Duration.ofMillis(1000))
            .withBackgroundExecutor(MockExecutor.INSTANCE)
            .buildReservoir();

    reservoir.update(10);
    reservoir.update(20);
    Snapshot snapshot = reservoir.getSnapshot();
    assertEquals(10, snapshot.getMin());
    assertEquals(20, snapshot.getMax());

    time.getAndAdd(900); // 900
    reservoir.update(30);
    reservoir.update(40);
    snapshot = reservoir.getSnapshot();
    assertEquals(10, snapshot.getMin());
    assertEquals(40, snapshot.getMax());

    time.getAndAdd(99); // 999
    reservoir.update(8);
    reservoir.update(60);
    snapshot = reservoir.getSnapshot();
    assertEquals(8, snapshot.getMin());
    assertEquals(60, snapshot.getMax());

    time.getAndAdd(1); // 1000
    reservoir.update(70);
    reservoir.update(80);
    snapshot = reservoir.getSnapshot();
    assertEquals(70, snapshot.getMin());
    assertEquals(80, snapshot.getMax());

    time.getAndAdd(1001); // 2001
    reservoir.update(90);
    reservoir.update(100);
    snapshot = reservoir.getSnapshot();
    assertEquals(90, snapshot.getMin());
    assertEquals(100, snapshot.getMax());

    time.getAndAdd(1000); // 3001
    snapshot = reservoir.getSnapshot();
    assertEquals(0, snapshot.getMin());
    assertEquals(0, snapshot.getMax());

    time.getAndAdd(1); // 3002
    reservoir.update(42);
    snapshot = reservoir.getSnapshot();
    assertEquals(42, snapshot.getMin());
    assertEquals(42, snapshot.getMax());

    time.getAndAdd(2000); // 5002
    snapshot = reservoir.getSnapshot();
    assertEquals(0, snapshot.getMin());
    assertEquals(0, snapshot.getMax());
}
 
Example 9
Source File: ResetByChunksAccumulatorTest.java    From rolling-metrics with Apache License 2.0 4 votes vote down vote up
@Test
public void test() {
    AtomicLong time = new AtomicLong(0);
    Clock wallClock = Clock.mock(time);
    Reservoir reservoir = new HdrBuilder(wallClock)
            .resetReservoirPeriodicallyByChunks(Duration.ofMillis(3000), 3)
            .withBackgroundExecutor(MockExecutor.INSTANCE)
            .buildReservoir();

    reservoir.update(10);
    reservoir.update(20);
    Snapshot snapshot = reservoir.getSnapshot();
    assertEquals(10, snapshot.getMin());
    assertEquals(20, snapshot.getMax());

    time.getAndAdd(900); // 900
    reservoir.update(30);
    reservoir.update(40);
    snapshot = reservoir.getSnapshot();
    assertEquals(10, snapshot.getMin());
    assertEquals(40, snapshot.getMax());

    time.getAndAdd(99); // 999
    reservoir.update(9);
    reservoir.update(60);
    snapshot = reservoir.getSnapshot();
    assertEquals(9, snapshot.getMin());
    assertEquals(60, snapshot.getMax());

    time.getAndAdd(1); // 1000
    reservoir.update(12);
    reservoir.update(70);
    snapshot = reservoir.getSnapshot();
    assertEquals(9, snapshot.getMin());
    assertEquals(70, snapshot.getMax());

    time.getAndAdd(1001); // 2001
    reservoir.update(13);
    reservoir.update(80);
    snapshot = reservoir.getSnapshot();
    assertEquals(9, snapshot.getMin());
    assertEquals(80, snapshot.getMax());

    time.getAndAdd(1000); // 3001
    snapshot = reservoir.getSnapshot();
    assertEquals(9, snapshot.getMin());
    assertEquals(80, snapshot.getMax());

    time.getAndAdd(999); // 4000
    snapshot = reservoir.getSnapshot();
    assertEquals(12, snapshot.getMin());
    assertEquals(80, snapshot.getMax());
    reservoir.update(1);
    reservoir.update(200);
    snapshot = reservoir.getSnapshot();
    assertEquals(1, snapshot.getMin());
    assertEquals(200, snapshot.getMax());

    time.getAndAdd(10000); // 14000
    snapshot = reservoir.getSnapshot();
    assertEquals(0, snapshot.getMin());
    assertEquals(0, snapshot.getMax());
    reservoir.update(3);

    time.addAndGet(3999); // 17999
    snapshot = reservoir.getSnapshot();
    assertEquals(3, snapshot.getMax());

    time.addAndGet(1); // 18000
    snapshot = reservoir.getSnapshot();
    assertEquals(0, snapshot.getMax());
}
 
Example 10
Source File: SnapshotCachingTest.java    From rolling-metrics with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldCacheSnapshot() {
    AtomicLong time = new AtomicLong(System.currentTimeMillis());
    Clock wallClock = Clock.mock(time);
    Reservoir reservoir = new HdrBuilder(wallClock)
            .resetReservoirOnSnapshot()
            .withSnapshotCachingDuration(Duration.ofMillis(1000))
            .buildReservoir();

    reservoir.update(10);
    reservoir.update(20);
    Snapshot firstSnapshot = reservoir.getSnapshot();

    time.getAndAdd(900);
    reservoir.update(30);
    reservoir.update(40);
    Snapshot firstCachedSnapshot = reservoir.getSnapshot();
    assertSame(firstSnapshot, firstCachedSnapshot);
    assertEquals(10, firstCachedSnapshot.getMin());
    assertEquals(20, firstCachedSnapshot.getMax());

    time.getAndAdd(99);
    reservoir.update(50);
    reservoir.update(60);
    Snapshot secondCachedSnapshot = reservoir.getSnapshot();
    assertSame(firstSnapshot, secondCachedSnapshot);
    assertEquals(10, secondCachedSnapshot.getMin());
    assertEquals(20, secondCachedSnapshot.getMax());

    time.getAndAdd(1);
    reservoir.update(70);
    reservoir.update(80);
    Snapshot firstNewSnapshot = reservoir.getSnapshot();
    assertNotSame(firstSnapshot, firstNewSnapshot);
    assertEquals(30, firstNewSnapshot.getMin());
    assertEquals(80, firstNewSnapshot.getMax());

    time.getAndAdd(1001);
    reservoir.update(90);
    reservoir.update(100);
    Snapshot secondNewSnapshot = reservoir.getSnapshot();
    assertNotSame(firstNewSnapshot, secondNewSnapshot);
    assertEquals(90, secondNewSnapshot.getMin());
    assertEquals(100, secondNewSnapshot.getMax());
}
 
Example 11
Source File: OverflowResolverTest.java    From rolling-metrics with Apache License 2.0 4 votes vote down vote up
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void testPassThruBigValues() {
    Reservoir reservoir = new HdrBuilder().withHighestTrackableValue(100, OverflowResolver.PASS_THRU).buildReservoir();
    reservoir.update(100000);
}