You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by li...@apache.org on 2019/11/14 04:27:19 UTC

[incubator-iotdb] branch dynamic_config updated: test(HyperLogLogTest): add HyperLogLogTest

This is an automated email from the ASF dual-hosted git repository.

liurui pushed a commit to branch dynamic_config
in repository https://gitbox.apache.org/repos/asf/incubator-iotdb.git


The following commit(s) were added to refs/heads/dynamic_config by this push:
     new 2056cf0  test(HyperLogLogTest): add HyperLogLogTest
2056cf0 is described below

commit 2056cf0a45846997f61bb6f39b69270a34ab46ab
Author: liuruiyiyang <24...@qq.com>
AuthorDate: Thu Nov 14 12:27:02 2019 +0800

    test(HyperLogLogTest): add HyperLogLogTest
---
 .../db/conf/adapter/CompressionRatioTest.java      |  2 -
 .../iotdb/db/conf/adapter/HyperLogLogTest.java     | 69 ++++++++++++++++++++++
 2 files changed, 69 insertions(+), 2 deletions(-)

diff --git a/server/src/test/java/org/apache/iotdb/db/conf/adapter/CompressionRatioTest.java b/server/src/test/java/org/apache/iotdb/db/conf/adapter/CompressionRatioTest.java
index bc7f7f7..0a0c9e0 100644
--- a/server/src/test/java/org/apache/iotdb/db/conf/adapter/CompressionRatioTest.java
+++ b/server/src/test/java/org/apache/iotdb/db/conf/adapter/CompressionRatioTest.java
@@ -19,13 +19,11 @@
 package org.apache.iotdb.db.conf.adapter;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import java.io.File;
 import java.io.IOException;
 import java.util.Locale;
-
 import org.apache.commons.io.FileUtils;
 import org.apache.iotdb.db.conf.IoTDBConfig;
 import org.apache.iotdb.db.conf.IoTDBDescriptor;
diff --git a/server/src/test/java/org/apache/iotdb/db/conf/adapter/HyperLogLogTest.java b/server/src/test/java/org/apache/iotdb/db/conf/adapter/HyperLogLogTest.java
new file mode 100644
index 0000000..f7b9a19
--- /dev/null
+++ b/server/src/test/java/org/apache/iotdb/db/conf/adapter/HyperLogLogTest.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.conf.adapter;
+
+import static org.junit.Assert.assertEquals;
+
+import com.clearspring.analytics.stream.cardinality.HyperLogLog;
+import com.clearspring.analytics.stream.cardinality.ICardinality;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Random;
+import java.util.Set;
+import org.junit.Test;
+
+public class HyperLogLogTest {
+
+  @Test
+  public void testStreamlibHll() {
+    final int seed = 12345;
+    // data on which to calculate distinct count
+    Random random = new Random(seed);
+    int sampleSize = 10000;
+    double[] floor = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0};
+    String[] avgErrors = new String[floor.length];
+    int testNum = 1000;
+
+    for (int floorIndex = 0; floorIndex < floor.length; floorIndex++) {
+      double errorSum = 0;
+      for (int testIndex = 0; testIndex < testNum; testIndex++) {
+        final ArrayList<Integer> list = new ArrayList<>();
+        for (int i = 0; i < sampleSize; i++) {
+          list.add(random.nextInt((int) (sampleSize * floor[floorIndex])));
+        }
+        Set<Integer> set = new HashSet<>();
+        ICardinality card = new HyperLogLog(13);
+        for (int a : list) {
+          set.add(a);
+          card.offer(a);
+        }
+        double p = (card.cardinality() - set.size()) / (double) set.size();
+        errorSum += Math.abs(p);
+      }
+      // allow 1% error
+      assertEquals(0,errorSum / testNum, 0.01);
+      avgErrors[floorIndex] = String.format("%.5f", errorSum / testNum);
+    }
+    System.out.println(Arrays.toString(avgErrors));
+    // output: [0.00299, 0.00914, 0.00331, 0.00294, 0.00348, 0.00450, 0.00605, 0.00581, 0.00580, 0.00585, 0.00668, 0.00672, 0.00748, 0.00753, 0.00742]
+    // time cost: 11s
+  }
+
+}