-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_test.go
120 lines (96 loc) · 2.89 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
package gobble
import (
"fmt"
"os"
"testing"
"time"
)
const short = 10
var timeLast = time.Now().UnixMicro()
func timing(msg string) {
timeNow := time.Now().UnixMicro()
fmt.Printf("%s: %fs\n", msg, float64(timeNow-timeLast)/1000000)
timeLast = timeNow
}
func TestBenchmark(t *testing.T) {
timing("Start")
db, _ := OpenDB("benchdb")
_ = db.DeleteCollection("testcollection")
collection, _ := OpenCollection[ExamplePersonStruct](db, "testcollection")
timing("DB Init")
for i := 0; i < 10000/short; i++ {
_ = collection.Insert(ExamplePersonStruct{Name: fmt.Sprintf("ExamplePersonStruct %d", i), Age: i})
}
timing("Insert 10000 records (before indexing)")
index, _ := OpenIndex[ExamplePersonStruct, string](&collection, func(p ExamplePersonStruct) string {
return p.Name
})
timing("Index init")
for i := 0; i < 10000/short; i++ {
_ = collection.Insert(ExamplePersonStruct{Name: fmt.Sprintf("ExamplePersonStruct %d 2", i), Age: -i})
}
timing("Insert 10000 records")
for i := 0; i < 5000/short; i++ {
_, _ = index.Get(fmt.Sprintf("ExamplePersonStruct %d", i))
}
timing("Get 5000 records")
_, _ = collection.Select(
func(p ExamplePersonStruct) bool {
return 0 < p.Age && p.Age < 5000/short
})
timing("Select 5000 records")
_, _ = index.Get("ExamplePersonStruct 0")
timing("Get 1 record")
_, _ = collection.Select(
func(p ExamplePersonStruct) bool {
return p.Name == "ExamplePersonStruct 0"
})
timing("Select 1 record")
_ = collection.Modify(
func(p ExamplePersonStruct) bool {
return p.Age%2 == 0
}, func(p ExamplePersonStruct) ExamplePersonStruct {
p.Age += 10000 / short
return p
})
timing("Modify 5000 records")
_, _ = collection.Select(
func(p ExamplePersonStruct) bool {
return p.Age < 10000/short
})
timing("Select 5000 records")
//fmt.Println(len(results), results[0], results[len(results)-1])
_ = collection.Delete(
func(p ExamplePersonStruct) bool {
return p.Age > 10000/short
})
timing("Delete 5000 records")
n, _ := collection.Number()
fmt.Println(n)
// benchmark index.Del and index.Mod
for i := 0; i < 5000/short; i++ {
_ = index.Del(fmt.Sprintf("ExamplePersonStruct %d", i))
}
timing("Index Del 5000 records")
n, _ = collection.Number()
fmt.Println(n)
timing("Number")
// Reset
_ = collection.Delete(func(p ExamplePersonStruct) bool { return true })
timing("Delete all records")
// reinsert
for i := 0; i < 10000/short; i++ {
_ = collection.Insert(ExamplePersonStruct{Name: fmt.Sprintf("ExamplePersonStruct %d", i), Age: i})
}
// benchmark index.Mod
for i := 0; i < 5000/short; i++ {
_ = index.Mod(fmt.Sprintf("ExamplePersonStruct %d", i), func(p ExamplePersonStruct) ExamplePersonStruct {
p.Age += 10000 / short
return p
})
}
timing("Index Mod 5000 records")
//x, _ := collection.Select(func(p ExamplePersonStruct) bool { return true })
//fmt.Println(sortPersonsByName(x))
_ = os.RemoveAll("benchdb")
}