forked from optimizely/hyperloglog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.js
107 lines (80 loc) · 2.85 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var HyperLogLog = require('./hyperloglog')
var hash = HyperLogLog.hash
var vows = require('vows')
var assert = require('assert')
vows.describe('HyperLogLog').addBatch({
'empty hll has cardinality of zero': function () {
assert.equal(HyperLogLog(8).count(), 0)
},
'counts unique things': function () {
var thing1 = hash('thing1')
var thing2 = hash('thing2')
var thing3 = hash('thing3')
var hll = HyperLogLog(8)
hll.add(thing1)
assert.equal(hll.count(), 1)
for (var i = 0; i < 100; ++i) {
hll.add(thing1)
hll.add(thing2)
hll.add(thing3)
}
assert.equal(hll.count(), 3)
},
'counts large set of unique values': function () {
const valueSetSize = 1000
const randomSeed = 8383838383
const randomValueArray = Array.from({ length: valueSetSize }, () => Math.floor(Math.random() * randomSeed))
const randomValueSet = Array.from(new Set(randomValueArray))
var hll = HyperLogLog(12)
randomValueSet.forEach(value => hll.add(hash(value.toString())))
const smoothedRelativeError = Math.ceil(hll.relative_error() * 100) / 100
const errorMargin = smoothedRelativeError * valueSetSize
const minAcceptable = Math.floor(valueSetSize - errorMargin)
const maxAcceptable = Math.ceil(valueSetSize + errorMargin)
const got = hll.count()
console.log(`max ${maxAcceptable} | min ${minAcceptable} - got ${hll.count()}`)
assert.equal(hll.count() <= maxAcceptable && hll.count() >= minAcceptable, true)
},
'merges overlapping counts': function () {
var hll = HyperLogLog(15)
var hll2 = HyperLogLog(15)
for (var i = 0; i < 100; ++i) {
hll.add(hash('Just hll ' + i))
hll2.add(hash('Just hll2 ' + i))
var both = hash('Both ' + i)
hll.add(both)
hll2.add(both)
}
assert(Math.abs(hll.count() - 200) <= 2)
assert(Math.abs(hll2.count() - 200) <= 2)
hll.merge(hll2.output())
assert(Math.abs(hll.count() - 300) <= 3)
},
'merges bigger HLL into a smaller one': function () {
var hll = HyperLogLog(8)
var hll2 = HyperLogLog(14)
var original_relative_error = hll.relative_error()
hll.add(hash('Just hll'))
hll2.add(hash('Just hll2'))
var both = hash('both')
hll.add(both)
hll2.add(both)
hll.merge(hll2.output())
assert.equal(hll.count(), 3)
assert(hll.relative_error() == original_relative_error)
},
'merges a smaller HLL into a bigger one': function () {
// The result is the same size as the smaller one.
var hll = HyperLogLog(14)
var hll2 = HyperLogLog(8)
var original_relative_error = hll.relative_error()
hll.add(hash('Just hll'))
hll2.add(hash('Just hll2'))
var both = hash('both')
hll.add(both)
hll2.add(both)
hll.merge(hll2.output())
assert.equal(hll.count(), 3)
assert(hll.relative_error() > original_relative_error)
}
}).export(module)