-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbench_test.go
202 lines (177 loc) · 3.69 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package radixtree
import (
"bufio"
"fmt"
"io"
"net/http"
"os"
"testing"
)
const (
// web2: Webster's Second International Dictionary, all 234,936 words worth.
web2URL = "https://raw.githubusercontent.com/openbsd/src/master/share/dict/web2"
web2Path = "web2"
// web2a: hyphenated terms as well as assorted noun and adverbial
// phrasesfrom Webster's Second International Dictionary.
web2aURL = "https://raw.githubusercontent.com/openbsd/src/master/share/dict/web2a"
web2aPath = "web2a"
)
func BenchmarkGet(b *testing.B) {
err := getWords()
if err != nil {
b.Skip(err.Error())
}
b.Run("Words", func(b *testing.B) {
benchmarkGet(b, web2Path)
})
b.Run("Web2a", func(b *testing.B) {
benchmarkGet(b, web2aPath)
})
}
func BenchmarkPut(b *testing.B) {
b.Run("Words", func(b *testing.B) {
benchmarkPut(b, web2Path)
})
b.Run("Web2a", func(b *testing.B) {
benchmarkPut(b, web2aPath)
})
}
func BenchmarkIter(b *testing.B) {
b.Run("Words", func(b *testing.B) {
benchmarkIter(b, web2Path)
})
b.Run("Web2a", func(b *testing.B) {
benchmarkIter(b, web2aPath)
})
}
func BenchmarkIterPath(b *testing.B) {
b.Run("Words", func(b *testing.B) {
benchmarkIterPath(b, web2Path)
})
b.Run("Web2a", func(b *testing.B) {
benchmarkIterPath(b, web2aPath)
})
}
func benchmarkGet(b *testing.B, filePath string) {
words, err := loadWords(filePath)
if err != nil {
b.Skip(err.Error())
}
tree := new(Tree[string])
for _, w := range words {
tree.Put(w, w)
}
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
for _, w := range words {
if _, ok := tree.Get(w); !ok {
panic("missing value")
}
}
}
}
func benchmarkPut(b *testing.B, filePath string) {
words, err := loadWords(filePath)
if err != nil {
b.Skip(err.Error())
}
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
tree := new(Tree[string])
for _, w := range words {
tree.Put(w, w)
}
}
}
func benchmarkIter(b *testing.B, filePath string) {
words, err := loadWords(filePath)
if err != nil {
b.Skip(err.Error())
}
tree := new(Tree[string])
for _, w := range words {
tree.Put(w, w)
}
b.ResetTimer()
b.ReportAllocs()
var count int
for n := 0; n < b.N; n++ {
count = 0
for range tree.Iter() {
count++
}
}
if count != len(words) {
b.Fatalf("iter wrong count, expected %d got %d", len(words), count)
}
}
func benchmarkIterPath(b *testing.B, filePath string) {
words, err := loadWords(filePath)
if err != nil {
b.Skip(err.Error())
}
tree := new(Tree[string])
for _, w := range words {
tree.Put(w, w)
}
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
found := false
for _, w := range words {
for range tree.IterPath(w) {
found = true
}
}
if !found {
b.Fatal("IterPath did not find word")
}
}
}
func loadWords(wordsFile string) ([]string, error) {
f, err := os.Open(wordsFile)
if err != nil {
return nil, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
var words []string
// Scan through line-dilimited words.
for scanner.Scan() {
words = append(words, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
return words, nil
}
func getWords() error {
err := downloadFile(web2URL, web2Path)
if err != nil {
return err
}
return downloadFile(web2aURL, web2aPath)
}
func downloadFile(fileURL, filePath string) error {
_, err := os.Stat(filePath)
if err == nil {
return nil
}
rsp, err := http.Get(fileURL)
if err != nil {
return err
}
defer rsp.Body.Close()
if rsp.StatusCode != 200 {
return fmt.Errorf("error response getting file: %d", rsp.StatusCode)
}
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, rsp.Body)
return err
}