-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhyperloglog_test.go
135 lines (113 loc) · 2.43 KB
/
hyperloglog_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
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
package hyperloglog
import (
"bufio"
"fmt"
"hash/fnv"
"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, low_b, high_b int) {
words := dictionary(n)
bad := 0
n_words := uint64(len(words))
for i := low_b; i < high_b; i++ {
m := uint(math.Pow(2, float64(i)))
h, err := New(m)
if err != nil {
t.Fatalf("can't make New(%d): %v", m, err)
}
hash := fnv.New32()
for _, word := range words {
hash.Write([]byte(word))
h.Add(hash.Sum32())
hash.Reset()
}
expected_error := 1.04 / math.Sqrt(float64(m))
actual_error := math.Abs(geterror(n_words, h.Count()))
if actual_error > expected_error {
bad++
t.Logf("m=%d: error=%.5f, expected <%.5f; actual=%d, estimated=%d\n",
m, actual_error, expected_error, n_words, h.Count())
}
}
t.Logf("%d of %d tests exceeded estimated error", bad, high_b-low_b)
}
func TestHyperLogLogSmall(t *testing.T) {
testHyperLogLog(t, 5, 4, 17)
}
func TestHyperLogLogBig(t *testing.T) {
testHyperLogLog(t, 0, 4, 17)
}
func benchmarkCount(b *testing.B, registers int) {
words := dictionary(0)
m := uint(math.Pow(2, float64(registers)))
h, err := New(m)
if err != nil {
return
}
hash := fnv.New32()
for _, word := range words {
hash.Write([]byte(word))
h.Add(hash.Sum32())
hash.Reset()
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
h.Count()
}
}
func BenchmarkCount4(b *testing.B) {
benchmarkCount(b, 4)
}
func BenchmarkCount5(b *testing.B) {
benchmarkCount(b, 5)
}
func BenchmarkCount6(b *testing.B) {
benchmarkCount(b, 6)
}
func BenchmarkCount7(b *testing.B) {
benchmarkCount(b, 7)
}
func BenchmarkCount8(b *testing.B) {
benchmarkCount(b, 8)
}
func BenchmarkCount9(b *testing.B) {
benchmarkCount(b, 9)
}
func BenchmarkCount10(b *testing.B) {
benchmarkCount(b, 10)
}