diff options
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/java/com/geedgenetworks/sketch/hlld/HllTest.java | 266 | ||||
| -rw-r--r-- | src/test/java/org/HdrHistogram/HistogramSketchTest.java | 114 |
2 files changed, 380 insertions, 0 deletions
diff --git a/src/test/java/com/geedgenetworks/sketch/hlld/HllTest.java b/src/test/java/com/geedgenetworks/sketch/hlld/HllTest.java new file mode 100644 index 0000000..2c83fb5 --- /dev/null +++ b/src/test/java/com/geedgenetworks/sketch/hlld/HllTest.java @@ -0,0 +1,266 @@ +package com.geedgenetworks.sketch.hlld;
+
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+
+public class HllTest {
+
+ @Test
+ public void testSize() {
+ long[] ns = new long[]{100, 1000, 10000, 100000, 10000000, 50000000, 100000000};
+ for (long n : ns) {
+ Hll hll = new Hll(14);
+ for (int i = 0; i < n; i++) {
+ String key = i + "";
+ hll.add(key);
+ }
+ long estimate = Math.round(hll.size()) ;
+ double percentErr = Math.abs(estimate - n) * 100D / n;
+ System.out.println("n:" + n + ",estimate:" + estimate+ ",percentErr:" + percentErr);
+ }
+ }
+
+ @Test
+ public void testSizeByteBuffer() {
+ long[] ns = new long[]{100, 1000, 10000, 100000, 10000000, 100000000};
+ for (long n : ns) {
+ int p = 14;
+ int bytes = DirectHllIntArray.getUpdatableSerializationBytes(p);
+ Hll hll = new Hll(p, ByteBuffer.allocate(bytes));
+ for (int i = 0; i < n; i++) {
+ String key = i + "";
+ hll.add(key);
+ }
+ long estimate = Math.round(hll.size()) ;
+ double percentErr = Math.abs(estimate - n) * 100D / n;
+ System.out.println("n:" + n + ",estimate:" + estimate+ ",percentErr:" + percentErr);
+ }
+ }
+
+ @Test
+ public void testCopy() {
+ Hll hll1 = new Hll(14);
+ for (int i = 0; i < 1000000; i++) {
+ String key = i + "";
+ hll1.add(key);
+ }
+
+ Hll hll2 = new Hll(12, ByteBuffer.allocate(DirectHllIntArray.getUpdatableSerializationBytes(12)));
+ for (int i = 0; i < 1000000; i++) {
+ String key = i + "";
+ hll2.add(key);
+ }
+
+ Hll copy1 = hll1.copy();
+ Hll copy2 = hll2.copy();
+
+ System.out.println((long)hll1.size());
+ System.out.println((long)hll2.size());
+ System.out.println("#####################");
+ System.out.println(copy1.hllImpl.getPrecision());
+ System.out.println(copy2.hllImpl.getPrecision());
+ System.out.println((long)copy1.size());
+ System.out.println((long)copy2.size());
+ }
+
+ @Test
+ public void testMerge0() {
+ // 两个普通Hll, 普通HllUnion
+ Hll hll1 = new Hll(14);
+ for (int i = 0; i < 1000000; i++) {
+ String key = i + "";
+ hll1.add(key);
+ }
+
+ Hll hll2 = new Hll(12);
+ for (int i = 0; i < 1000000; i++) {
+ String key = i + "";
+ //String key = i + "a";
+ hll2.add(key);
+ }
+
+ System.out.println((long)hll1.size());
+ System.out.println((long)hll2.size());
+
+ HllUnion union = new HllUnion(14);
+ union.update(hll1);
+ union.update(hll2);
+
+ System.out.println((long) union.getResult().size());
+ }
+
+ @Test
+ public void testMerge1() {
+ // 两个普通Hll, 字节数组HllUnion
+ Hll hll1 = new Hll(14);
+ for (int i = 0; i < 1000000; i++) {
+ String key = i + "";
+ hll1.add(key);
+ }
+
+ Hll hll2 = new Hll(12);
+ for (int i = 0; i < 1000000; i++) {
+ //String key = i + "";
+ String key = i + "a";
+ hll2.add(key);
+ }
+
+ System.out.println((long)hll1.size());
+ System.out.println((long)hll2.size());
+
+ int p = 14;
+ int bytes = DirectHllIntArray.getUpdatableSerializationBytes(p);
+ HllUnion union = new HllUnion(p, ByteBuffer.allocate(bytes));
+ union.update(hll1);
+ union.update(hll2);
+
+ System.out.println((long) union.getResult().size());
+ System.out.println(union.getResult().hllImpl.isMemory());
+ }
+
+ @Test
+ public void testMerge2() {
+ // 一个普通Hll一个字节数组Hll, 普通HllUnion
+ Hll hll1 = new Hll(14);
+ for (int i = 0; i < 1000000; i++) {
+ String key = i + "";
+ hll1.add(key);
+ }
+
+ Hll hll2 = new Hll(12, ByteBuffer.allocate(DirectHllIntArray.getUpdatableSerializationBytes(12)));
+ for (int i = 0; i < 1000000; i++) {
+ //String key = i + "";
+ String key = i + "a";
+ hll2.add(key);
+ }
+
+ System.out.println((long)hll1.size());
+ System.out.println((long)hll2.size());
+
+ HllUnion union = new HllUnion(14);
+ union.update(hll1);
+ union.update(hll2);
+
+ System.out.println((long) union.getResult().size());
+ }
+
+ @Test
+ public void testMerge3() {
+ // 一个普通Hll一个字节数组Hll, 字节数组HllUnion
+ Hll hll1 = new Hll(14);
+ for (int i = 0; i < 1000000; i++) {
+ String key = i + "";
+ hll1.add(key);
+ }
+
+ Hll hll2 = new Hll(12, ByteBuffer.allocate(DirectHllIntArray.getUpdatableSerializationBytes(12)));
+ for (int i = 0; i < 1000000; i++) {
+ //String key = i + "";
+ String key = i + "a";
+ hll2.add(key);
+ }
+
+ System.out.println((long)hll1.size());
+ System.out.println((long)hll2.size());
+
+ int p = 14;
+ int bytes = DirectHllIntArray.getUpdatableSerializationBytes(p);
+ HllUnion union = new HllUnion(p, ByteBuffer.allocate(bytes));
+ union.update(hll1);
+ union.update(hll2);
+
+ System.out.println((long) union.getResult().size());
+ System.out.println(union.getResult().hllImpl.isMemory());
+ }
+
+ @Test
+ public void testMerge4() {
+ // 两个字节数组Hll, 字节数组HllUnion
+ Hll hll1 = new Hll(14, ByteBuffer.allocate(DirectHllIntArray.getUpdatableSerializationBytes(14)));
+ for (int i = 0; i < 1000000; i++) {
+ String key = i + "";
+ hll1.add(key);
+ }
+
+ Hll hll2 = new Hll(12, ByteBuffer.allocate(DirectHllIntArray.getUpdatableSerializationBytes(12)));
+ for (int i = 0; i < 1000000; i++) {
+ //String key = i + "";
+ String key = i + "a";
+ hll2.add(key);
+ }
+
+ System.out.println((long)hll1.size());
+ System.out.println((long)hll2.size());
+
+ int p = 14;
+ int bytes = DirectHllIntArray.getUpdatableSerializationBytes(p);
+ HllUnion union = new HllUnion(p, ByteBuffer.allocate(bytes));
+ union.update(hll1);
+ union.update(hll2);
+
+ System.out.println((long) union.getResult().size());
+ System.out.println(union.getResult().hllImpl.isMemory());
+ }
+
+ @Test
+ public void testSer() {
+ Hll hll1 = new Hll(14);
+ for (int i = 0; i < 1000000; i++) {
+ String key = i + "";
+ hll1.add(key);
+ }
+
+ int position = 8;
+ ByteBuffer byteBuffer = ByteBuffer.allocate(position + DirectHllIntArray.getUpdatableSerializationBytes(12));
+ byteBuffer.position(position);
+ Hll hll2 = new Hll(12, byteBuffer);
+ for (int i = 0; i < 1000000; i++) {
+ String key = i + "";
+ hll2.add(key);
+ }
+
+ System.out.println(hll1.size());
+ System.out.println(hll2.size());
+
+ byte[] bytes1 = hll1.toBytes();
+ byte[] bytes2 = hll2.toBytes();
+ Hll hllDer1 = Hll.fromBytes(bytes1);
+ Hll hllDer2 = Hll.fromBytes(bytes2);
+
+ System.out.println(hllDer1.size());
+ System.out.println(hllDer2.size());
+ assert !hllDer1.hllImpl.isMemory();
+ assert !hllDer2.hllImpl.isMemory();
+ }
+
+ @Test
+ public void testWrapBytes() {
+ Hll hll1 = new Hll(14);
+ for (int i = 0; i < 1000000; i++) {
+ String key = i + "";
+ hll1.add(key);
+ }
+
+ Hll hll2 = new Hll(12, ByteBuffer.allocate(DirectHllIntArray.getUpdatableSerializationBytes(12)));
+ for (int i = 0; i < 1000000; i++) {
+ String key = i + "";
+ hll2.add(key);
+ }
+
+ System.out.println(hll1.size());
+ System.out.println(hll2.size());
+
+ byte[] bytes1 = hll1.toBytes();
+ byte[] bytes2 = hll2.toBytes();
+
+ Hll hllDer1 = Hll.wrapBytes(bytes1);
+ Hll hllDer2 = Hll.wrapBytes(bytes2);
+
+ System.out.println(hllDer1.size());
+ System.out.println(hllDer2.size());
+ assert hllDer1.hllImpl.isMemory();
+ assert hllDer2.hllImpl.isMemory();
+ }
+
+}
\ No newline at end of file diff --git a/src/test/java/org/HdrHistogram/HistogramSketchTest.java b/src/test/java/org/HdrHistogram/HistogramSketchTest.java new file mode 100644 index 0000000..52f48bc --- /dev/null +++ b/src/test/java/org/HdrHistogram/HistogramSketchTest.java @@ -0,0 +1,114 @@ +package org.HdrHistogram;
+
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+import java.util.concurrent.ThreadLocalRandom;
+
+public class HistogramSketchTest {
+ String line = "###################";
+
+ @Test
+ public void testArrayHistogram() {
+ ArrayHistogram histogram = new ArrayHistogram(1);
+ System.out.println(histogram.describe());
+ for (int i = 0; i < 10000; i++) {
+ histogram.recordValue(i);
+ }
+ System.out.println(histogram.describe());
+ for (Percentile percentile : histogram.percentileList(5)) {
+ System.out.println(percentile);
+ }
+ System.out.println(line);
+ histogram = new ArrayHistogram(1);
+ for (int i = 0; i < 10000; i++) {
+ histogram.recordValue(ThreadLocalRandom.current().nextLong(100000));
+ }
+ System.out.println(histogram.describe());
+ for (Percentile percentile : histogram.percentileList(5)) {
+ System.out.println(percentile);
+ }
+ }
+
+ @Test
+ public void testDirectArrayHistogram() {
+ ByteBuffer byteBuffer = ByteBuffer.allocate(DirectArrayHistogram.getUpdatableSerializationBytes(1, 100000, 1));
+ DirectArrayHistogram histogram = new DirectArrayHistogram(1, 100000, 1,
+ byteBuffer);
+ System.out.println(histogram.describe());
+ for (int i = 0; i < 10000; i++) {
+ histogram.recordValue(i);
+ }
+ System.out.println(histogram.describe());
+ for (Percentile percentile : histogram.percentileList(5)) {
+ System.out.println(percentile);
+ }
+ System.out.println(line);
+ histogram.resetByteBuffer(ByteBuffer.allocate(DirectArrayHistogram.getUpdatableSerializationBytes(1, 100000, 1)));
+ histogram.reset();
+ for (int i = 0; i < 10000; i++) {
+ histogram.recordValue(ThreadLocalRandom.current().nextLong(100000));
+ }
+ System.out.println(histogram.describe());
+ for (Percentile percentile : histogram.percentileList(5)) {
+ System.out.println(percentile);
+ }
+ }
+
+ @Test
+ public void testCopy() {
+ ArrayHistogram histogram1 = new ArrayHistogram(1);
+ ByteBuffer byteBuffer = ByteBuffer.allocate(DirectArrayHistogram.getUpdatableSerializationBytes(1, 100000, 1));
+ DirectArrayHistogram histogram2 = new DirectArrayHistogram(1, 100000, 1,
+ byteBuffer);
+ for (int i = 0; i < 10000; i++) {
+ histogram1.recordValue(i);
+ histogram2.recordValue(i);
+ }
+
+ ArrayHistogram copy1 = histogram1.copy();
+ ArrayHistogram copy2 = histogram2.copy();
+
+ System.out.println(histogram1.describe());
+ System.out.println(histogram2.describe());
+ System.out.println(line);
+
+ System.out.println(copy1.describe());
+ System.out.println(copy2.describe());
+ }
+
+ @Test
+ public void testArrayHistogramMerge() {
+ ArrayHistogram histogram1 = new ArrayHistogram(1);
+ for (int i = 0; i < 1000; i++) {
+ histogram1.recordValue(i);
+ }
+ byte[] bytes1 = histogram1.toBytes();
+
+ DirectMapHistogram directHistogram = DirectMapHistogram.wrapBytes(bytes1);
+ ArrayHistogram histogram2 = new ArrayHistogram(1);
+ histogram2.merge(directHistogram);
+ System.out.println(histogram2.describe());
+ }
+
+ @Test
+ public void testDirectArrayHistogramMerge() {
+ ArrayHistogram histogram1 = new ArrayHistogram(1);
+ int updatableSerializationBytes = DirectArrayHistogram.getUpdatableSerializationBytes(1, 100000, 1);
+ DirectArrayHistogram histogram2 = new DirectArrayHistogram(1, 100000, 1, ByteBuffer.allocate(updatableSerializationBytes));
+ for (int i = 0; i < 10000; i++) {
+ histogram1.recordValue(i);
+ histogram2.recordValue(i);
+ }
+ byte[] bytes1 = histogram1.toBytes();
+ byte[] bytes2 = histogram2.toBytes();
+
+ DirectArrayHistogram histogram = new DirectArrayHistogram(1, 100000, 1, ByteBuffer.allocate(updatableSerializationBytes));
+ histogram.merge(DirectMapHistogram.wrapBytes(bytes1));
+ histogram.merge(DirectMapHistogram.wrapBytes(bytes2));
+ System.out.println(histogram.describe());
+ for (Percentile percentile : histogram.percentileList(5)) {
+ System.out.println(percentile);
+ }
+ }
+}
\ No newline at end of file |
