forked from tylertreat/BoomFilters
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhyperloglog_test.go
More file actions
155 lines (128 loc) · 3.19 KB
/
hyperloglog_test.go
File metadata and controls
155 lines (128 loc) · 3.19 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
Original work Copyright 2013 Eric Lesh
Modified work Copyright 2015 Tyler Treat
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
*/
package boom
import (
"bufio"
"fmt"
"io"
"math"
"os"
"testing"
)
// Return a dictionary up to n words. If n is zero, return the entire
// dictionary.
func dictionary(n int) []string {
var words []string
dict := "/usr/share/dict/words"
f, err := os.Open(dict)
if err != nil {
fmt.Printf("can't open dictionary file '%s': %v\n", dict, err)
os.Exit(1)
}
count := 0
buf := bufio.NewReader(f)
for {
if n != 0 && count >= n {
break
}
word, err := buf.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
continue
}
words = append(words, word)
count++
}
f.Close()
return words
}
func geterror(actual uint64, estimate uint64) (result float64) {
return (float64(estimate) - float64(actual)) / float64(actual)
}
func testHyperLogLog(t *testing.T, n, lowB, highB int) {
words := dictionary(n)
bad := 0
nWords := uint64(len(words))
for i := lowB; i < highB; i++ {
m := uint(math.Pow(2, float64(i)))
h, err := NewHyperLogLog(m)
if err != nil {
t.Fatalf("can't make NewHyperLogLog(%d): %v", m, err)
}
for _, word := range words {
h.Add([]byte(word))
}
expectedError := 1.04 / math.Sqrt(float64(m))
actualError := math.Abs(geterror(nWords, h.Count()))
if actualError > expectedError {
bad++
t.Logf("m=%d: error=%.5f, expected <%.5f; actual=%d, estimated=%d\n",
m, actualError, expectedError, nWords, h.Count())
}
}
t.Logf("%d of %d tests exceeded estimated error", bad, highB-lowB)
}
func TestHyperLogLogSmall(t *testing.T) {
testHyperLogLog(t, 5, 4, 17)
}
func TestHyperLogLogBig(t *testing.T) {
testHyperLogLog(t, 0, 4, 17)
}
func TestNewDefaultHyperLogLog(t *testing.T) {
hll, err := NewDefaultHyperLogLog(0.1)
if err != nil {
t.Fatalf("can't make NewDefaultHyperLogLog(0.1): %v", err)
}
if hll.m != 128 {
t.Errorf("expected 128, got %d", hll.m)
}
}
func benchmarkCount(b *testing.B, registers int) {
words := dictionary(0)
m := uint(math.Pow(2, float64(registers)))
h, err := NewHyperLogLog(m)
if err != nil {
return
}
for _, word := range words {
h.Add([]byte(word))
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
h.Count()
}
}
func BenchmarkHLLCount4(b *testing.B) {
benchmarkCount(b, 4)
}
func BenchmarkHLLCount5(b *testing.B) {
benchmarkCount(b, 5)
}
func BenchmarkHLLCount6(b *testing.B) {
benchmarkCount(b, 6)
}
func BenchmarkHLLCount7(b *testing.B) {
benchmarkCount(b, 7)
}
func BenchmarkHLLCount8(b *testing.B) {
benchmarkCount(b, 8)
}
func BenchmarkHLLCount9(b *testing.B) {
benchmarkCount(b, 9)
}
func BenchmarkHLLCount10(b *testing.B) {
benchmarkCount(b, 10)
}