forked from HouzuoGuo/tiedot
/
benchmark.go
296 lines (289 loc) · 8.8 KB
/
benchmark.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package main
import (
"encoding/json"
"fmt"
"loveoneanother.at/tiedot/db"
"math/rand"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
const BENCH_SIZE = 400000 // don't make it too large... unmarshaled JSON takes lots of memory!
const BENCH2_SIZE = 400000 // feel free to make this one larger!
// Run function a number of times and calculate average time consumption per iteration.
func average(name string, total int, init func(), do func()) {
numThreads := runtime.GOMAXPROCS(-1)
wp := new(sync.WaitGroup)
init()
iter := float64(total)
start := float64(time.Now().UTC().UnixNano())
for i := 0; i < total; i += total / numThreads {
wp.Add(1)
go func() {
defer wp.Done()
for j := 0; j < total/numThreads; j++ {
do()
}
}()
}
wp.Wait()
end := float64(time.Now().UTC().UnixNano())
fmt.Printf("%s %d: %d ns/iter, %d iter/sec\n", name, int(total), int((end-start)/iter), int(1000000000/((end-start)/iter)))
}
// Benchmark document CRUD operations.
func benchmark() {
// initialization
rand.Seed(time.Now().UTC().UnixNano())
// prepare benchmark data
docs := [BENCH_SIZE]interface{}{}
for i := range docs {
if err := json.Unmarshal([]byte(
`{"a": {"b": {"c": `+strconv.Itoa(rand.Intn(BENCH_SIZE))+`}},`+
`"c": {"d": `+strconv.Itoa(rand.Intn(BENCH_SIZE))+`},`+
`"more": "abcdefghijklmnopqrstuvwxyz"}`), &docs[i]); err != nil {
panic("json error")
}
}
// prepare collection
tmp := "/tmp/tiedot_bench"
os.RemoveAll(tmp)
defer os.RemoveAll(tmp)
col, err := db.OpenCol(tmp)
if err != nil {
panic(err)
}
col.Index([]string{"a", "b", "c"})
col.Index([]string{"c", "d"})
// start benchmarks
average("insert", BENCH_SIZE, func() {}, func() {
if _, err := col.Insert(docs[rand.Intn(BENCH_SIZE)]); err != nil {
panic("insert error")
}
})
ids := make([]uint64, 0)
average("read", BENCH_SIZE, func() {
col.ForAll(func(id uint64, doc interface{}) bool {
ids = append(ids, id)
return true
})
}, func() {
var doc interface{}
col.Read(ids[rand.Intn(BENCH_SIZE)], &doc)
if doc == nil {
panic("read error")
}
})
average("lookup", BENCH_SIZE, func() {}, func() {
var query interface{}
if err := json.Unmarshal([]byte(`{"c": [{"eq": `+strconv.Itoa(rand.Intn(BENCH_SIZE))+`, "in": ["a", "b", "c"], "limit": 1}, `+
`{"eq": `+strconv.Itoa(rand.Intn(BENCH_SIZE))+`, "in": ["c", "d"], "limit": 1}]}`), &query); err != nil {
panic("json error")
}
result := make(map[uint64]struct{})
if err := db.EvalQueryV2(query, col, &result); err != nil {
panic("query error")
}
})
average("update", BENCH_SIZE, func() {}, func() {
if _, err := col.Update(ids[rand.Intn(BENCH_SIZE)], docs[rand.Intn(BENCH_SIZE)]); err != nil {
panic("update error")
}
})
average("delete", BENCH_SIZE, func() {}, func() {
col.Delete(ids[rand.Intn(BENCH_SIZE)])
})
col.Close()
}
// Insert/update/delete/query all running at once.
func benchmark2() {
numThreads := runtime.GOMAXPROCS(-1)
rand.Seed(time.Now().UTC().UnixNano())
// prepare collection
tmp := "/tmp/tiedot_bench"
os.RemoveAll(tmp)
col, err := db.OpenCol(tmp)
if err != nil {
panic(err)
}
col.Index([]string{"a", "b", "c"})
col.Index([]string{"c", "d"})
docs := make([]uint64, 0, BENCH2_SIZE*2+1000)
docsMutex := new(sync.Mutex)
// Prepare 1000 docs as a start
var docToInsert interface{}
for j := 0; j < 1000; j++ {
if err = json.Unmarshal([]byte(
`{"a": {"b": {"c": `+strconv.Itoa(rand.Intn(BENCH2_SIZE))+`}},`+
`"c": {"d": `+strconv.Itoa(rand.Intn(BENCH2_SIZE))+`},`+
`"more": "abcdefghijklmnopqrstuvwxyz"}`), &docToInsert); err != nil {
panic(err)
}
if newID, err := col.Insert(docToInsert); err == nil {
docs = append(docs, newID)
} else {
panic(err)
}
}
// benchmark begins
wp := new(sync.WaitGroup)
wp.Add(5 * numThreads) // (CRUD + query) * number of benchmark threads
start := float64(time.Now().UTC().UnixNano())
// insert BENCH2_SIZE * 2 documents
for i := 0; i < numThreads; i++ {
go func(i int) {
fmt.Printf("Insert thread %d starting\n", i)
defer wp.Done()
var docToInsert interface{}
var err error
for j := 0; j < BENCH2_SIZE/numThreads*2; j++ {
if err = json.Unmarshal([]byte(
`{"a": {"b": {"c": `+strconv.Itoa(rand.Intn(BENCH2_SIZE))+`}},`+
`"c": {"d": `+strconv.Itoa(rand.Intn(BENCH2_SIZE))+`},`+
`"more": "abcdefghijklmnopqrstuvwxyz"}`), &docToInsert); err != nil {
panic(err)
}
if newID, err := col.Insert(docToInsert); err == nil {
docsMutex.Lock()
docs = append(docs, newID)
docsMutex.Unlock()
} else {
panic(err)
}
}
fmt.Printf("Insert thread %d completed\n", i)
}(i)
}
// read BENCH2_SIZE * 2 documents
for i := 0; i < numThreads; i++ {
go func(i int) {
fmt.Printf("Read thread %d starting\n", i)
defer wp.Done()
var doc interface{}
for j := 0; j < BENCH2_SIZE/numThreads*2; j++ {
col.Read(docs[uint64(rand.Intn(len(docs)))], &doc)
}
fmt.Printf("Read thread %d completed\n", i)
}(i)
}
// query BENCH2_SIZE times
for i := 0; i < numThreads; i++ {
go func(i int) {
fmt.Printf("Query thread %d starting\n", i)
defer wp.Done()
var query interface{}
var err error
for j := 0; j < BENCH2_SIZE/numThreads; j++ {
if err = json.Unmarshal([]byte(`{"c": [{"eq": `+strconv.Itoa(rand.Intn(BENCH2_SIZE))+`, "in": ["a", "b", "c"], "limit": 1}, `+
`{"eq": `+strconv.Itoa(rand.Intn(BENCH2_SIZE))+`, "in": ["c", "d"], "limit": 1}]}`), &query); err != nil {
panic("json error")
}
result := make(map[uint64]struct{})
if err = db.EvalQueryV2(query, col, &result); err != nil {
panic("query error")
}
}
fmt.Printf("Query thread %d completed\n", i)
}(i)
}
// update BENCH2_SIZE documents
for i := 0; i < numThreads; i++ {
go func(i int) {
fmt.Printf("Update thread %d starting\n", i)
defer wp.Done()
var updated interface{}
var err error
for j := 0; j < BENCH2_SIZE/numThreads; j++ {
if err = json.Unmarshal([]byte(
`{"a": {"b": {"c": `+strconv.Itoa(rand.Intn(BENCH2_SIZE))+`}},`+
`"c": {"d": `+strconv.Itoa(rand.Intn(BENCH2_SIZE))+`},`+
`"more": "abcdefghijklmnopqrstuvwxyz"}`), &updated); err != nil {
panic(err)
}
if _, err = col.Update(docs[uint64(rand.Intn(len(docs)))], updated); err != nil {
// "does not exist" indicates that a deleted document is being updated, it is safe to ignore
if !strings.Contains(fmt.Sprint(err), "does not exist") {
fmt.Println(err)
}
}
}
fmt.Printf("Update thread %d completed\n", i)
}(i)
}
// delete BENCH2_SIZE documents
for i := 0; i < numThreads; i++ {
go func(i int) {
fmt.Printf("Delete thread %d starting\n", i)
defer wp.Done()
for j := 0; j < BENCH2_SIZE/numThreads; j++ {
col.Delete(docs[uint64(rand.Intn(len(docs)))])
}
fmt.Printf("Delete thread %d completed\n", i)
}(i)
}
wp.Wait()
end := float64(time.Now().UTC().UnixNano())
fmt.Printf("Total operations %d: %d ns/iter, %d iter/sec\n", BENCH2_SIZE*7, int((end-start)/BENCH2_SIZE/7), int(1000000000/((end-start)/BENCH2_SIZE/7)))
}
// Benchmark document operations involving UID.
func benchmark3() {
// initialization
rand.Seed(time.Now().UTC().UnixNano())
// prepare benchmark data
docs := [BENCH_SIZE]interface{}{}
for i := range docs {
if err := json.Unmarshal([]byte(
`{"a": {"b": {"c": `+strconv.Itoa(rand.Intn(BENCH_SIZE))+`}},`+
`"c": {"d": `+strconv.Itoa(rand.Intn(BENCH_SIZE))+`},`+
`"more": "abcdefghijklmnopqrstuvwxyz"}`), &docs[i]); err != nil {
panic("json error")
}
}
// prepare collection
tmp := "/tmp/tiedot_bench"
os.RemoveAll(tmp)
defer os.RemoveAll(tmp)
col, err := db.OpenCol(tmp)
if err != nil {
panic(err)
}
col.Index([]string{"a", "b", "c"})
col.Index([]string{"c", "d"})
uidsMutex := new(sync.Mutex)
uids := make([]string, 0, BENCH_SIZE)
// start benchmarks
average("insert", BENCH_SIZE, func() {}, func() {
if _, uid, err := col.InsertWithUID(docs[rand.Intn(BENCH_SIZE)]); err == nil {
uidsMutex.Lock()
uids = append(uids, uid)
uidsMutex.Unlock()
} else {
panic("insert error")
}
})
average("read", BENCH_SIZE, func() {
}, func() {
var doc interface{}
col.ReadByUID(uids[rand.Intn(BENCH_SIZE)], &doc)
})
average("lookup", BENCH_SIZE, func() {}, func() {
var query interface{}
if err := json.Unmarshal([]byte(`{"c": [{"eq": `+strconv.Itoa(rand.Intn(BENCH_SIZE))+`, "in": ["a", "b", "c"], "limit": 1}, `+
`{"eq": `+strconv.Itoa(rand.Intn(BENCH_SIZE))+`, "in": ["c", "d"], "limit": 1}]}`), &query); err != nil {
panic("json error")
}
result := make(map[uint64]struct{})
if err := db.EvalQueryV2(query, col, &result); err != nil {
panic("query error")
}
})
average("update", BENCH_SIZE, func() {}, func() {
col.UpdateByUID(uids[rand.Intn(BENCH_SIZE)], docs[rand.Intn(BENCH_SIZE)])
})
average("delete", BENCH_SIZE, func() {}, func() {
col.DeleteByUID(uids[rand.Intn(BENCH_SIZE)])
})
col.Close()
}