|
| 1 | +/* |
| 2 | + * Copyright 2019, Yahoo! Inc. Licensed under the terms of the |
| 3 | + * Apache License 2.0. See LICENSE file at the project root for terms. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.yahoo.sketches.quantiles; |
| 7 | + |
| 8 | +import static com.yahoo.sketches.quantiles.Util.LS; |
| 9 | +import static org.testng.Assert.assertEquals; |
| 10 | +import static org.testng.Assert.assertTrue; |
| 11 | + |
| 12 | +import java.util.HashSet; |
| 13 | +import java.util.Random; |
| 14 | + |
| 15 | +import org.testng.annotations.Test; |
| 16 | + |
| 17 | +import com.yahoo.memory.WritableDirectHandle; |
| 18 | +import com.yahoo.memory.WritableMemory; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Lee Rhodes |
| 22 | + */ |
| 23 | +public class DebugUnionTest { |
| 24 | + |
| 25 | + @Test |
| 26 | + public void test() { |
| 27 | + final int n = 70_000; |
| 28 | + final int valueLimit = 1000; |
| 29 | + final int numSketches = 3; |
| 30 | + final int sketchK = 8; |
| 31 | + final int unionK = 8; |
| 32 | + UpdateDoublesSketch[] sketchArr = new UpdateDoublesSketch[numSketches]; |
| 33 | + |
| 34 | + //builds the input sketches, all on heap |
| 35 | + DoublesSketch.rand = new Random(1); //make deterministic for test |
| 36 | + final HashSet<Double> set = new HashSet<>(); //holds input values |
| 37 | + for (int s = 0; s < numSketches; s++) { |
| 38 | + sketchArr[s] = buildHeapSketch(sketchK, n, valueLimit, set); |
| 39 | + } |
| 40 | + |
| 41 | + //loads the on heap union |
| 42 | + DoublesSketch.rand = new Random(1); //make deterministic for test |
| 43 | + DoublesUnion hUnion = DoublesUnion.builder().setMaxK(unionK).build(); |
| 44 | + for (int s = 0; s < numSketches; s++) { hUnion.update(sketchArr[s]); } |
| 45 | + DoublesSketch hSketch = hUnion.getResult(); |
| 46 | + |
| 47 | + //loads the direct union |
| 48 | + DoublesSketch.rand = new Random(1); //make deterministic for test |
| 49 | + DoublesUnion dUnion; |
| 50 | + DoublesSketch dSketch; |
| 51 | + try ( WritableDirectHandle wdh = WritableMemory.allocateDirect(10_000_000) ) { |
| 52 | + WritableMemory wmem = wdh.get(); |
| 53 | + dUnion = DoublesUnion.builder().setMaxK(8).build(wmem); |
| 54 | + for (int s = 0; s < numSketches; s++) { dUnion.update(sketchArr[s]); } |
| 55 | + dSketch = dUnion.getResult(); //result is on heap |
| 56 | + } |
| 57 | + |
| 58 | + //iterates and counts errors |
| 59 | + int hCount = hSketch.getRetainedItems(); |
| 60 | + int dCount = dSketch.getRetainedItems(); |
| 61 | + |
| 62 | + assertEquals(hCount, dCount); //Retained items must be the same |
| 63 | + |
| 64 | + double[] heapItems = new double[hCount]; |
| 65 | + double[] directItems = new double[dCount]; |
| 66 | + int hErrors = 0; |
| 67 | + int dErrors = 0; |
| 68 | + |
| 69 | + DoublesSketchIterator hit = hSketch.iterator(); |
| 70 | + DoublesSketchIterator dit = dSketch.iterator(); |
| 71 | + int i = 0; |
| 72 | + while (hit.next() && dit.next()) { |
| 73 | + double v = hit.getValue(); |
| 74 | + heapItems[i] = v; |
| 75 | + if (!set.contains(v)) { hErrors++; } |
| 76 | + |
| 77 | + double w = dit.getValue(); |
| 78 | + directItems[i] = w; |
| 79 | + if (!set.contains(w)) { dErrors++; } |
| 80 | + i++; |
| 81 | + assertEquals(v, w, 0); //Items must be returned in same order and be equal |
| 82 | + } |
| 83 | + assertTrue(hErrors == 0); |
| 84 | + assertTrue(dErrors == 0); |
| 85 | + |
| 86 | + //println("HeapUnion : Values: " + hCount + ", errors: " + hErrors); |
| 87 | + //println(hSketch.toString(true, true)); |
| 88 | + |
| 89 | + //println("DirectUnion: Values: " + dCount + ", errors: " + dErrors); |
| 90 | + //println(dSketch.toString(true, true)); |
| 91 | + } |
| 92 | + |
| 93 | + private static UpdateDoublesSketch buildHeapSketch(final int k, final int n, final int valueLimit, |
| 94 | + final HashSet<Double> set) { |
| 95 | + final UpdateDoublesSketch uSk = DoublesSketch.builder().setK(k).build(); |
| 96 | + for (int i = 0; i < n; i++) { |
| 97 | + final double value = DoublesSketch.rand.nextInt(valueLimit) + 1; |
| 98 | + uSk.update(value); |
| 99 | + set.add(value); |
| 100 | + } |
| 101 | + return uSk; |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void printlnTest() { |
| 106 | + println("PRINTING: " + this.getClass().getName()); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @param s value to print |
| 111 | + */ |
| 112 | + static void println(String s) { |
| 113 | + print(s+LS); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @param s value to print |
| 118 | + */ |
| 119 | + static void print(String s) { |
| 120 | + //System.out.print(s); //disable here |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments