|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.datasketches.count; |
| 21 | + |
| 22 | +import org.apache.datasketches.common.SketchesArgumentException; |
| 23 | +import org.apache.datasketches.common.SketchesException; |
| 24 | +import org.testng.annotations.Test; |
| 25 | + |
| 26 | +import java.io.ByteArrayOutputStream; |
| 27 | +import java.lang.annotation.Repeatable; |
| 28 | +import java.nio.ByteBuffer; |
| 29 | + |
| 30 | +import static org.testng.Assert.*; |
| 31 | + |
| 32 | +public class CountMinSketchTest { |
| 33 | + @Test |
| 34 | + public void createNewCountMinSketchTest() throws Exception { |
| 35 | + assertThrows(SketchesArgumentException.class, () -> new CountMinSketch((byte) 5, 1, 123)); |
| 36 | + assertThrows(SketchesArgumentException.class, () -> new CountMinSketch((byte) 4, 268435456, 123)); |
| 37 | + |
| 38 | + final byte numHashes = 3; |
| 39 | + final int numBuckets = 5; |
| 40 | + final long seed = 1234567; |
| 41 | + CountMinSketch c = new CountMinSketch(numHashes, numBuckets, seed); |
| 42 | + |
| 43 | + assertEquals(c.getNumHashes_(), numHashes); |
| 44 | + assertEquals(c.getNumBuckets_(), numBuckets); |
| 45 | + assertEquals(c.getSeed_(), seed); |
| 46 | + assertTrue(c.isEmpty()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void parameterSuggestionsTest() { |
| 51 | + // Bucket suggestions |
| 52 | + assertThrows("Relative error must be at least 0.", SketchesException.class, () -> CountMinSketch.suggestNumBuckets(-1.0)); |
| 53 | + assertEquals(CountMinSketch.suggestNumBuckets(0.2), 14); |
| 54 | + assertEquals(CountMinSketch.suggestNumBuckets(0.1), 28); |
| 55 | + assertEquals(CountMinSketch.suggestNumBuckets(0.05), 55); |
| 56 | + assertEquals(CountMinSketch.suggestNumBuckets(0.01), 272); |
| 57 | + |
| 58 | + // Check that the sketch get_epsilon acts inversely to suggest_num_buckets |
| 59 | + final byte numHashes = 3; |
| 60 | + final long seed = 1234567; |
| 61 | + assertTrue(new CountMinSketch(numHashes, 14, seed).getRelativeError() <= 0.2); |
| 62 | + assertTrue(new CountMinSketch(numHashes, 28, seed).getRelativeError() <= 0.1); |
| 63 | + assertTrue(new CountMinSketch(numHashes, 55, seed).getRelativeError() <= 0.05); |
| 64 | + assertTrue(new CountMinSketch(numHashes, 272, seed).getRelativeError() <= 0.01); |
| 65 | + |
| 66 | + // Hash suggestions |
| 67 | + assertThrows("Confidence must be between 0 and 1.0 (inclusive).", SketchesException.class, () -> CountMinSketch.suggestNumHashes(10)); |
| 68 | + assertThrows("Confidence must be between 0 and 1.0 (inclusive).", SketchesException.class, () -> CountMinSketch.suggestNumHashes(-1.0)); |
| 69 | + assertEquals(CountMinSketch.suggestNumHashes(0.682689492), 2); |
| 70 | + assertEquals(CountMinSketch.suggestNumHashes(0.954499736), 4); |
| 71 | + assertEquals(CountMinSketch.suggestNumHashes(0.997300204), 6); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void countMinSketchOneUpdateTest() { |
| 76 | + final byte numHashes = 3; |
| 77 | + final int numBuckets = 5; |
| 78 | + final long seed = 1234567; |
| 79 | + long insertedWeights = 0; |
| 80 | + CountMinSketch c = new CountMinSketch(numHashes, numBuckets, seed); |
| 81 | + final String x = "x"; |
| 82 | + |
| 83 | + assertTrue(c.isEmpty()); |
| 84 | + assertEquals(c.getEstimate(x), 0); |
| 85 | + c.update(x, 1); |
| 86 | + assertFalse(c.isEmpty()); |
| 87 | + assertEquals(c.getEstimate(x), 1); |
| 88 | + insertedWeights++; |
| 89 | + |
| 90 | + final long w = 9; |
| 91 | + insertedWeights += w; |
| 92 | + c.update(x, w); |
| 93 | + assertEquals(c.getEstimate(x), insertedWeights); |
| 94 | + |
| 95 | + final double w1 = 10.0; |
| 96 | + insertedWeights += (long) w1; |
| 97 | + c.update(x, (long) w1); |
| 98 | + assertEquals(c.getEstimate(x), insertedWeights); |
| 99 | + assertEquals(c.getTotalWeight_(), insertedWeights); |
| 100 | + assertTrue(c.getEstimate(x) <= c.getUpperBound(x)); |
| 101 | + assertTrue(c.getEstimate(x) >= c.getLowerBound(x)); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void frequencyCancellationTest() { |
| 106 | + CountMinSketch c = new CountMinSketch((byte) 1, 5, 123456); |
| 107 | + c.update("x", 1); |
| 108 | + c.update("y", -1); |
| 109 | + assertEquals(c.getTotalWeight_(), 2); |
| 110 | + assertEquals(c.getEstimate("x"), 1); |
| 111 | + assertEquals(c.getEstimate("y"), -1); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void frequencyEstimates() { |
| 116 | + final int numItems = 10; |
| 117 | + long[] data = new long[numItems]; |
| 118 | + long[] frequencies = new long[numItems]; |
| 119 | + |
| 120 | + for (int i = 0; i < numItems; i++) { |
| 121 | + data[i] = i; |
| 122 | + frequencies[i] = (long) 1 << (numItems - i); |
| 123 | + } |
| 124 | + |
| 125 | + final double relativeError = 0.1; |
| 126 | + final double confidence = 0.99; |
| 127 | + final int numBuckets = CountMinSketch.suggestNumBuckets(relativeError); |
| 128 | + final byte numHashes = CountMinSketch.suggestNumHashes(confidence); |
| 129 | + |
| 130 | + CountMinSketch c = new CountMinSketch(numHashes, numBuckets, 1234567); |
| 131 | + for (int i = 0; i < numItems; i++) { |
| 132 | + final long value = data[i]; |
| 133 | + final long freq = frequencies[i]; |
| 134 | + c.update(value, freq); |
| 135 | + } |
| 136 | + |
| 137 | + for (final long i : data) { |
| 138 | + final long est = c.getEstimate(i); |
| 139 | + final long upp = c.getUpperBound(i); |
| 140 | + final long low = c.getLowerBound(i); |
| 141 | + assertTrue(est <= upp); |
| 142 | + assertTrue(est >= low); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void mergeFailTest() { |
| 148 | + final double relativeError = 0.25; |
| 149 | + final double confidence = 0.9; |
| 150 | + final long seed = 1234567; |
| 151 | + final int numBuckets = CountMinSketch.suggestNumBuckets(relativeError); |
| 152 | + final byte numHashes = CountMinSketch.suggestNumHashes(confidence); |
| 153 | + CountMinSketch s = new CountMinSketch(numHashes, numBuckets, seed); |
| 154 | + |
| 155 | + assertThrows("Cannot merge a sketch with itself.", SketchesException.class, () -> s.merge(s)); |
| 156 | + |
| 157 | + CountMinSketch s1 = new CountMinSketch((byte) (numHashes + 1), numBuckets, seed); |
| 158 | + CountMinSketch s2 = new CountMinSketch(numHashes, numBuckets + 1, seed); |
| 159 | + CountMinSketch s3 = new CountMinSketch(numHashes, numBuckets, seed + 1); |
| 160 | + |
| 161 | + CountMinSketch[] sketches = {s1, s2, s3}; |
| 162 | + for (final CountMinSketch sk : sketches) { |
| 163 | + assertThrows("Incompatible sketch configuration.", SketchesException.class, () -> s.merge(sk)); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void mergeTest() { |
| 169 | + final double relativeError = 0.25; |
| 170 | + final double confidence = 0.9; |
| 171 | + final long seed = 123456; |
| 172 | + final int numBuckets = CountMinSketch.suggestNumBuckets(relativeError); |
| 173 | + final byte numHashes = CountMinSketch.suggestNumHashes(confidence); |
| 174 | + CountMinSketch c = new CountMinSketch(numHashes, numBuckets, seed); |
| 175 | + |
| 176 | + final byte sHashes = c.getNumHashes_(); |
| 177 | + final int sBuckets = c.getNumBuckets_(); |
| 178 | + final long sSeed = c.getSeed_(); |
| 179 | + CountMinSketch s = new CountMinSketch(sHashes, sBuckets, sSeed); |
| 180 | + |
| 181 | + c.merge(s); |
| 182 | + assertEquals(c.getTotalWeight_(), 0); |
| 183 | + |
| 184 | + final long[] data = {2, 3, 5, 7}; |
| 185 | + for (final long d : data) { |
| 186 | + c.update(d, 1); |
| 187 | + s.update(d, 1); |
| 188 | + } |
| 189 | + c.merge(s); |
| 190 | + |
| 191 | + assertEquals(c.getTotalWeight_(), 2 * s.getTotalWeight_()); |
| 192 | + |
| 193 | + for (final long d : data) { |
| 194 | + assertTrue(c.getEstimate(d) <= c.getUpperBound(d)); |
| 195 | + assertTrue(s.getEstimate(d) <= 2); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + public void serializeDeserializeEmptyTest() { |
| 201 | + final byte numHashes = 3; |
| 202 | + final int numBuckets = 32; |
| 203 | + final long seed = 123456; |
| 204 | + CountMinSketch c = new CountMinSketch(numHashes, numBuckets, seed); |
| 205 | + |
| 206 | + ByteArrayOutputStream buf = new ByteArrayOutputStream(); |
| 207 | + c.serialize(buf); |
| 208 | + |
| 209 | + byte[] b = buf.toByteArray(); |
| 210 | + assertThrows(SketchesArgumentException.class, () -> CountMinSketch.deserialize(b, seed - 1)); |
| 211 | + |
| 212 | + CountMinSketch d = CountMinSketch.deserialize(b, seed); |
| 213 | + assertEquals(d.getNumHashes_(), c.getNumHashes_()); |
| 214 | + assertEquals(d.getNumBuckets_(), c.getNumBuckets_()); |
| 215 | + assertEquals(d.getSeed_(), c.getSeed_()); |
| 216 | + final long zero = 0; |
| 217 | + assertEquals(d.getEstimate(zero), c.getEstimate(zero)); |
| 218 | + assertEquals(d.getTotalWeight_(), c.getTotalWeight_()); |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + public void serializeDeserializeTest() { |
| 223 | + final byte numHashes = 5; |
| 224 | + final int numBuckets = 64; |
| 225 | + final long seed = 1234456; |
| 226 | + CountMinSketch c = new CountMinSketch(numHashes, numBuckets, seed); |
| 227 | + for (long i = 0; i < 10; i++) { |
| 228 | + c.update(i, 10*i*i); |
| 229 | + } |
| 230 | + |
| 231 | + ByteArrayOutputStream buf = new ByteArrayOutputStream(); |
| 232 | + c.serialize(buf); |
| 233 | + |
| 234 | + assertThrows(SketchesArgumentException.class, () -> CountMinSketch.deserialize(buf.toByteArray(), seed - 1)); |
| 235 | + CountMinSketch d = CountMinSketch.deserialize(buf.toByteArray(), seed); |
| 236 | + |
| 237 | + assertEquals(d.getNumHashes_(), c.getNumHashes_()); |
| 238 | + assertEquals(d.getNumBuckets_(), c.getNumBuckets_()); |
| 239 | + assertEquals(d.getSeed_(), c.getSeed_()); |
| 240 | + assertEquals(d.getTotalWeight_(), c.getTotalWeight_()); |
| 241 | + |
| 242 | + for (long i = 0; i < 10; i++) { |
| 243 | + assertEquals(d.getEstimate(i), c.getEstimate(i)); |
| 244 | + } |
| 245 | + } |
| 246 | +} |
0 commit comments