-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark_test.go
204 lines (167 loc) · 4.67 KB
/
benchmark_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
203
204
package store4_test
import (
"fmt"
"log"
"math/rand"
"runtime"
"testing"
"github.com/jimsmart/store4"
)
// $ go test -run=XXX -bench=.
// What are we timing here? I don't think much of this has any meaning.
var storeFind *store4.QuadStore
var lastDim int
// func init() {
// storeFind = newStoreForFind()
// }
const (
namespace = "urn:store4:benchmark/padded-iri#"
)
func newStoreWithRandomQuads(count uint64) *store4.QuadStore {
s := store4.NewQuadStore()
for s.Size() < count {
q := randomQuad("")
s.Add(q[0], q[1], q[2], q[3])
}
return s
}
func randomQuad(graph string) [4]string {
var q [4]string
// TODO(js) Random quads do not represent the distribution of real-world data!!! :/
q[0] = randomIRI()
q[1] = randomIRI()
q[2] = randomIRI()
q[3] = graph
return q
}
func randomIRI() string {
return namespace + fmt.Sprintf("%16x", rand.Int63())
}
func randomQuadn(n int, graph string) [4]string {
var q [4]string
// TODO(js) Random quads do not represent the distribution of real-world data!!! :/
q[0] = randomIRIn(n)
q[1] = randomIRIn(n)
q[2] = randomIRIn(n)
q[3] = graph
return q
}
func randomIRIn(n int) string {
return namespace + fmt.Sprintf("%16x", rand.Int63n(int64(n)))
}
func benchmarkAddRemoveRandomsTo(count uint64, b *testing.B) {
s := newStoreWithRandomQuads(count)
q := randomQuad("")
b.ResetTimer()
for n := 0; n < b.N; n++ {
s.Add(q[0], q[1], q[2], q[3])
s.Remove(q[0], q[1], q[2], q[3])
}
}
func BenchmarkAddRemoveRandomsToEmpty(b *testing.B) { benchmarkAddRemoveRandomsTo(0, b) }
func BenchmarkAddRemoveRandomsTo10kRandoms(b *testing.B) { benchmarkAddRemoveRandomsTo(10000, b) }
func BenchmarkAddRemoveRandomsTo100kRandoms(b *testing.B) { benchmarkAddRemoveRandomsTo(100000, b) }
func BenchmarkAddRemoveRandomsTo500kRandoms(b *testing.B) { benchmarkAddRemoveRandomsTo(500000, b) }
func BenchmarkAddRemoveRandomsTo1mRandoms(b *testing.B) { benchmarkAddRemoveRandomsTo(1000000, b) }
// // func BenchmarkAddRemoveRandomsTo10mRandoms(b *testing.B) { benchmarkAddRemoveRandomsTo(10000000, b) }
// // func BenchmarkAddRemoveRandomsTo50mRandoms(b *testing.B) { benchmarkAddRemoveRandomsTo(50000000, b) }
// // func BenchmarkAddRemoveRandomsTo100mRandoms(b *testing.B) { benchmarkAddRemoveRandomsTo(100000000, b) }
func logMemStatsDifference(before, after *runtime.MemStats) {
out := "Diff "
out = out + fmtMemStatMiB("Alloc", after.Alloc-before.Alloc)
out = out + fmtMemStatMiB("TotalAlloc", after.TotalAlloc-before.TotalAlloc)
out = out + fmtMemStatMiB("Sys", after.Sys-before.Sys)
log.Println(out)
}
func fmtMemStatMiB(name string, n uint64) string {
return fmt.Sprintf("- %s: %.2fMiB ", name, float64(n)/1024/1024)
}
func readMemStats() *runtime.MemStats {
m := &runtime.MemStats{}
runtime.ReadMemStats(m)
return m
}
func createStoreForFind(dim int) *store4.QuadStore {
if storeFind != nil && lastDim == dim {
return storeFind
}
before := readMemStats()
storeFind = newStoreWithDim(dim)
log.Println("QuadStore.Size", storeFind.Size())
lastDim = dim
after := readMemStats()
logMemStatsDifference(before, after)
return storeFind
}
func newStoreWithDim(dim int) *store4.QuadStore {
// dimSquared := dim * dim
// dimCubed := dimSquared * dim
// dimQuads := dimCubed * dim
s := store4.NewQuadStore()
for i := 0; i < dim; i++ {
for j := 0; j < dim; j++ {
for k := 0; k < dim; k++ {
s.Add(iri(i), iri(j), iri(k), "")
}
}
}
return s
}
func iri(i int) string {
return namespace + fmt.Sprintf("%16x", i)
}
func BenchmarkSomeWith_NoWildcards(b *testing.B) {
fn := func(s, p string, o interface{}, g string) bool {
return true
}
dim := 256
s := createStoreForFind(dim)
q := randomQuadn(dim, "")
b.ResetTimer()
for n := 0; n < b.N; n++ {
if !s.SomeWith(q[0], q[1], q[2], q[3], fn) {
panic("bad SomeWith")
}
}
}
func BenchmarkSomeWith_WildcardO(b *testing.B) {
fn := func(s, p string, o interface{}, g string) bool {
return true
}
dim := 256
s := createStoreForFind(dim)
q := randomQuadn(dim, "")
b.ResetTimer()
for n := 0; n < b.N; n++ {
if !s.SomeWith(q[0], q[1], "*", q[3], fn) {
panic("bad SomeWith")
}
}
}
func BenchmarkSomeWith_WildcardPO(b *testing.B) {
fn := func(s, p string, o interface{}, g string) bool {
return true
}
dim := 256
s := createStoreForFind(dim)
q := randomQuadn(dim, "")
b.ResetTimer()
for n := 0; n < b.N; n++ {
if !s.SomeWith(q[0], "*", "*", q[3], fn) {
panic("bad SomeWith")
}
}
}
func BenchmarkSomeWith_WildcardSPO(b *testing.B) {
fn := func(s, p string, o interface{}, g string) bool {
return true
}
dim := 256
s := createStoreForFind(dim)
b.ResetTimer()
for n := 0; n < b.N; n++ {
if !s.SomeWith("*", "*", "*", "", fn) {
panic("bad SomeWith")
}
}
}