-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathbench_test.go
74 lines (59 loc) · 1.23 KB
/
bench_test.go
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
package hyperloglog
import (
"fmt"
"hash"
"hash/fnv"
"math"
"math/rand"
"testing"
)
func hash32(s string) hash.Hash32 {
h := fnv.New32a()
h.Write([]byte(s))
return h
}
func hash64(s string) hash.Hash64 {
h := fnv.New64a()
h.Write([]byte(s))
return h
}
func randStr(n int) string {
i := rand.Uint32()
return fmt.Sprintf("a%s %s", i, n)
}
func benchmark(precision uint8, n int) {
h, _ := New(precision)
hpp, _ := NewPlus(precision)
for i := 0; i < n; i++ {
s := randStr(i)
h.Add(hash32(s))
h.Add(hash32(s))
hpp.Add(hash64(s))
hpp.Add(hash64(s))
}
e, epp := h.Count(), hpp.Count()
var percentErr = func(est uint64) float64 {
return math.Abs(float64(n)-float64(est)) / float64(n)
}
fmt.Printf("\nReal Cardinality: %8d\n", n)
fmt.Printf("HyperLogLog : %8d, Error: %f%%\n", e, percentErr(e))
fmt.Printf("HyperLogLog++ : %8d, Error: %f%%\n", epp, percentErr(epp))
}
func BenchmarkHll4(b *testing.B) {
benchmark(4, b.N)
}
func BenchmarkHll6(b *testing.B) {
benchmark(6, b.N)
}
func BenchmarkHll8(b *testing.B) {
benchmark(8, b.N)
}
func BenchmarkHll10(b *testing.B) {
benchmark(10, b.N)
}
func BenchmarkHll14(b *testing.B) {
benchmark(14, b.N)
}
func BenchmarkHll16(b *testing.B) {
benchmark(16, b.N)
}