Search in sources :

Example 1 with Precision

use of com.twitter.common.stats.Precision in project commons by twitter.

the class HistogramTest method testHistogram.

@Test
public void testHistogram() {
    int n = 10000;
    FakeClock clock = new FakeClock();
    Precision precision = new Precision(0.001, n);
    int slices = 2;
    HistogramInterface hist = new Histogram(name, Amount.of(1L, Time.MINUTES), slices, null, precision, DEFAULT_QUANTILES, clock);
    double error = precision.getEpsilon() * precision.getN() * slices;
    for (int i = 1; i <= n; ++i) {
        hist.add(i);
    }
    clock.advance(Amount.of(31L, Time.SECONDS));
    Snapshot sample = hist.snapshot();
    assertEquals(1L, sample.min());
    assertEquals((long) n, sample.max());
    assertEquals((long) n, sample.count());
    assertEquals((long) (n * (n + 1) / 2), sample.sum());
    assertEquals(n * (n + 1) / (2.0 * n), sample.avg(), 0.1);
    long[] expected = new long[DEFAULT_QUANTILES.length];
    for (int i = 0; i < DEFAULT_QUANTILES.length; i++) {
        expected[i] = (long) (DEFAULT_QUANTILES[i] * n);
    }
    checkQuantiles(expected, sample, error);
}
Also used : Precision(com.twitter.common.stats.Precision) FakeClock(com.twitter.common.util.testing.FakeClock) Test(org.junit.Test)

Aggregations

Precision (com.twitter.common.stats.Precision)1 FakeClock (com.twitter.common.util.testing.FakeClock)1 Test (org.junit.Test)1