Search in sources :

Example 96 with Memory

use of com.yahoo.memory.Memory in project sketches-core by DataSketches.

the class SketchesTest method checkStaticEstimators.

@Test
public void checkStaticEstimators() {
    int k = 4096;
    int u = 4 * k;
    Memory srcMem = getCompactSketch(k, 0, u);
    double est = Sketches.getEstimate(srcMem);
    assertEquals(est, u, 0.05 * u);
    double rse = 1.0 / Math.sqrt(k);
    double ub = Sketches.getUpperBound(1, srcMem);
    assertEquals(ub, est + rse, 0.05 * u);
    double lb = Sketches.getLowerBound(1, srcMem);
    assertEquals(lb, est - rse, 0.05 * u);
    Memory memV1 = convertSerV3toSerV1(srcMem);
    boolean empty = Sketches.getEmpty(memV1);
    assertFalse(empty);
    Memory emptyMemV3 = getCompactSketch(k, 0, 0);
    assertEquals(Sketches.getRetainedEntries(emptyMemV3), 0);
    assertEquals(Sketches.getThetaLong(emptyMemV3), Long.MAX_VALUE);
    Memory emptyMemV1 = convertSerV3toSerV1(emptyMemV3);
    empty = Sketches.getEmpty(emptyMemV1);
    assertTrue(empty);
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 97 with Memory

use of com.yahoo.memory.Memory in project sketches-core by DataSketches.

the class SketchesTest method checkBadSketchFamily.

@Test(expectedExceptions = SketchesArgumentException.class)
public void checkBadSketchFamily() {
    Union union = setOperationBuilder().buildUnion();
    byte[] byteArr = union.toByteArray();
    Memory srcMem = Memory.wrap(byteArr);
    Sketches.getEstimate(srcMem);
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 98 with Memory

use of com.yahoo.memory.Memory in project sketches-core by DataSketches.

the class SketchesTest method checkSketchMethods.

@Test
public void checkSketchMethods() {
    int k = 1024;
    Memory mem = getCompactSketch(k, 0, k);
    CompactSketch csk2 = (CompactSketch) heapifySketch(mem);
    assertEquals((int) csk2.getEstimate(), k);
    csk2 = (CompactSketch) heapifySketch(mem, Util.DEFAULT_UPDATE_SEED);
    assertEquals((int) csk2.getEstimate(), k);
    csk2 = (CompactSketch) wrapSketch(mem);
    //TODO fails
    assertEquals((int) csk2.getEstimate(), k);
    csk2 = (CompactSketch) wrapSketch(mem, Util.DEFAULT_UPDATE_SEED);
    assertEquals((int) csk2.getEstimate(), k);
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 99 with Memory

use of com.yahoo.memory.Memory in project sketches-core by DataSketches.

the class HeapQuickSelectSketchTest method checkHeapifyByteArrayEstimating.

@Test
public void checkHeapifyByteArrayEstimating() {
    int k = 4096;
    int u = 2 * k;
    long seed = DEFAULT_UPDATE_SEED;
    UpdateSketch usk = UpdateSketch.builder().setFamily(fam_).setSeed(seed).setNominalEntries(k).build();
    for (int i = 0; i < u; i++) usk.update(i);
    double uskEst = usk.getEstimate();
    double uskLB = usk.getLowerBound(2);
    double uskUB = usk.getUpperBound(2);
    assertEquals(usk.isEstimationMode(), true);
    byte[] byteArray = usk.toByteArray();
    Memory srcMem = Memory.wrap(byteArray);
    UpdateSketch usk2 = UpdateSketch.heapify(srcMem, seed);
    assertEquals(usk2.getEstimate(), uskEst);
    assertEquals(usk2.getLowerBound(2), uskLB);
    assertEquals(usk2.getUpperBound(2), uskUB);
    assertEquals(usk2.isEmpty(), false);
    assertEquals(usk2.isEstimationMode(), true);
    assertEquals(usk2.getClass().getSimpleName(), usk.getClass().getSimpleName());
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 100 with Memory

use of com.yahoo.memory.Memory in project sketches-core by DataSketches.

the class HeapQuickSelectSketchTest method checkHeapifyMemoryEstimating.

@Test
public void checkHeapifyMemoryEstimating() {
    int k = 512;
    int u = 2 * k;
    long seed = DEFAULT_UPDATE_SEED;
    boolean estimating = (u > k);
    UpdateSketch sk1 = UpdateSketch.builder().setFamily(fam_).setSeed(seed).setNominalEntries(k).build();
    for (int i = 0; i < u; i++) sk1.update(i);
    double sk1est = sk1.getEstimate();
    double sk1lb = sk1.getLowerBound(2);
    double sk1ub = sk1.getUpperBound(2);
    assertEquals(sk1.isEstimationMode(), estimating);
    byte[] byteArray = sk1.toByteArray();
    Memory mem = Memory.wrap(byteArray);
    UpdateSketch sk2 = UpdateSketch.heapify(mem, DEFAULT_UPDATE_SEED);
    assertEquals(sk2.getEstimate(), sk1est);
    assertEquals(sk2.getLowerBound(2), sk1lb);
    assertEquals(sk2.getUpperBound(2), sk1ub);
    assertEquals(sk2.isEmpty(), false);
    assertEquals(sk2.isEstimationMode(), estimating);
    assertEquals(sk2.getClass().getSimpleName(), sk1.getClass().getSimpleName());
}
Also used : Memory(com.yahoo.memory.Memory) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Aggregations

Memory (com.yahoo.memory.Memory)141 Test (org.testng.annotations.Test)121 WritableMemory (com.yahoo.memory.WritableMemory)111 ArrayOfLongsSerDe (com.yahoo.sketches.ArrayOfLongsSerDe)11 ArrayOfStringsSerDe (com.yahoo.sketches.ArrayOfStringsSerDe)10 SketchesArgumentException (com.yahoo.sketches.SketchesArgumentException)10 SketchesReadOnlyException (com.yahoo.sketches.SketchesReadOnlyException)6 MemoryRegion (com.yahoo.memory.MemoryRegion)2 NativeMemory (com.yahoo.memory.NativeMemory)2 ArrayOfNumbersSerDe (com.yahoo.sketches.ArrayOfNumbersSerDe)2 WritableDirectHandle (com.yahoo.memory.WritableDirectHandle)1 Util.checkIsCompactMemory (com.yahoo.sketches.quantiles.Util.checkIsCompactMemory)1 CompactSketch.createCompactSketch (com.yahoo.sketches.theta.CompactSketch.createCompactSketch)1 Union (com.yahoo.sketches.theta.Union)1 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1