Search in sources :

Example 6 with ApproximateHistogram

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

the class ApproximateHistogramTest method testRecCollapse.

@Test
public void testRecCollapse() {
    long[] empty = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    long[] full = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
    ApproximateHistogram hist = new ApproximateHistogram(b, h);
    assertArrayEquals(empty, hist.buffer[0]);
    assertArrayEquals(empty, hist.buffer[1]);
    initializeValues(hist, b, Suppliers.ofInstance(1L));
    assertArrayEquals(full, hist.buffer[0]);
    assertArrayEquals(empty, hist.buffer[1]);
    initializeValues(hist, b, Suppliers.ofInstance(1L));
    assertArrayEquals(full, hist.buffer[0]);
    assertArrayEquals(full, hist.buffer[1]);
    hist.add(1);
    assertEquals(2, hist.currentTop);
    // Buffers are not cleared so we can't check that!
    assertArrayEquals(full, hist.buffer[2]);
    initializeValues(hist, 2 * b, Suppliers.ofInstance(1L));
    assertEquals(3, hist.currentTop);
    assertArrayEquals(full, hist.buffer[3]);
}
Also used : ApproximateHistogram(com.twitter.common.stats.ApproximateHistogram) Test(org.junit.Test)

Example 7 with ApproximateHistogram

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

the class ApproximateHistogramTest method testCollapse.

@Test
public void testCollapse() {
    ApproximateHistogram hist = new ApproximateHistogram();
    long[] buf1 = { 2, 5, 7 };
    long[] buf2 = { 3, 8, 9 };
    long[] expected = { 3, 7, 9 };
    long[] result = new long[3];
    // [2,5,7] weight 2 and [3,8,9] weight 3
    // weight x array + concat = [2,2,5,5,7,7,3,3,3,8,8,8,9,9,9]
    // sort = [2,2,3,3,3,5,5,7,7,8,8,8,9,9,9]
    // select every nth elems = [3,7,9]  (n = sum weight / 2, ie. 5/3 = 2)
    // [2,2,3,3,3,5,5,7,7,8,8,8,9,9,9]
    // . . ^ . . . . ^ . . . . ^ . .
    // [-------] [-------] [-------] we make 3 packets of 5 elements and take the middle
    ApproximateHistogram.collapse(buf1, 2, buf2, 3, result);
    assertArrayEquals(result, expected);
    long[] buf3 = { 2, 5, 7, 9 };
    long[] buf4 = { 3, 8, 9, 12 };
    long[] expected2 = { 3, 7, 9, 12 };
    long[] result2 = new long[4];
    ApproximateHistogram.collapse(buf3, 2, buf4, 2, result2);
    assertArrayEquals(expected2, result2);
}
Also used : ApproximateHistogram(com.twitter.common.stats.ApproximateHistogram) Test(org.junit.Test)

Example 8 with ApproximateHistogram

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

the class ApproximateHistogramTest method testReachingMaxDepth.

@Test
public void testReachingMaxDepth() {
    ApproximateHistogram hist = new ApproximateHistogram(b, h);
    initializeValues(hist, 8 * b, Suppliers.ofInstance(1L));
    assertEquals(3, hist.currentTop);
    hist.add(1);
    assertEquals(3, hist.currentTop);
}
Also used : ApproximateHistogram(com.twitter.common.stats.ApproximateHistogram) Test(org.junit.Test)

Example 9 with ApproximateHistogram

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

the class ApproximateHistogramTest method testLowMemoryPrecision.

@Test
public void testLowMemoryPrecision() {
    double e = ApproximateHistogram.DEFAULT_PRECISION.getEpsilon();
    int n = ApproximateHistogram.DEFAULT_PRECISION.getN();
    int defaultDepth = ApproximateHistogram.computeDepth(e, n);
    int defaultBufferSize = ApproximateHistogram.computeBufferSize(defaultDepth, n);
    ApproximateHistogram hist = new ApproximateHistogram(Amount.of(1L, Data.KB));
    int depth = hist.buffer.length - 1;
    int bufferSize = hist.buffer[0].length;
    assertTrue(depth > defaultDepth);
    assertTrue(bufferSize < defaultBufferSize);
}
Also used : ApproximateHistogram(com.twitter.common.stats.ApproximateHistogram) Test(org.junit.Test)

Example 10 with ApproximateHistogram

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

the class MetricsCreationBench method timeCreatingApproxHistogram.

/**
 * ApproximateHistogram is the underlying datastructure backing the Histogram primitive.
 */
public void timeCreatingApproxHistogram(int n) {
    ApproximateHistogram h;
    int i = n;
    while (i != 0) {
        h = new ApproximateHistogram();
        h.add(1);
        i--;
    }
}
Also used : ApproximateHistogram(com.twitter.common.stats.ApproximateHistogram)

Aggregations

ApproximateHistogram (com.twitter.common.stats.ApproximateHistogram)18 Test (org.junit.Test)16 Data (com.twitter.common.quantity.Data)2 ImmutableList (com.google.common.collect.ImmutableList)1 Amount (com.twitter.common.quantity.Amount)1 RealHistogram (com.twitter.common.stats.testing.RealHistogram)1