/
benchmark.go
265 lines (221 loc) · 5.59 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
package test
// This file collects some helpers for generating data used in
// benchmarks,
// - topdown/topdown_bench_test.go
import (
"bytes"
"encoding/json"
"fmt"
"text/template"
)
// PartialObjectBenchmarkCrossModule returns a module with n "bench_test_" prefixed rules
// that each refer to another "cond_bench_" prefixed rule
func PartialObjectBenchmarkCrossModule(n int) []string {
fooMod := `package test.foo
import data.test.bar
import data.test.baz
output[key] := value {
value := bar[key]
startswith("bench_test_", key)
}`
barMod := "package test.bar\n"
barMod += `
cond_bench_0 {
contains(lower(input.test_input_0), lower("input_01"))
}
cond_bench_1 {
contains(lower(input.test_input_1), lower("input"))
}
cond_bench_2 {
contains(lower(input.test_input_2), lower("input_10"))
}
bench_test_out_result := load_tests(test_collector)
load_tests(in) := out {
out := in
}
`
bazMod := "package test.baz\nimport data.test.bar\n"
ruleBuilder := ""
for idx := 1; idx <= n; idx++ {
barMod += fmt.Sprintf(`
bench_test_%[1]d := result {
input.bench_test_collector_mambo_number_%[3]d
result := input.bench_test_collector_mambo_number_%[3]d
} else := result {
is_null(bench_test_out_result.mambo_number_%[3]d.error)
result := bench_test_out_result.mambo_number_%[3]d.result
}
test_collector["mambo_number_%[3]d"] := result {
cond_bench_%[2]d
not %[3]d == 2
not %[3]d == 3
not input.bench_test_collector_mambo_number_%[3]d
result := { "result": %[3]d, "error": null }
}
`, idx, idx%3, idx%5)
ruleBuilder += fmt.Sprintf(" bar.bench_test_%[1]d == %[1]d\n", idx)
if idx%10 == 0 {
bazMod += fmt.Sprintf(`rule_%d {
%s
}`, idx, ruleBuilder)
fooMod += fmt.Sprintf(`
final_decision = "allow" {
baz.rule_%d
}
`, idx)
ruleBuilder = ""
}
}
return []string{fooMod, barMod, bazMod}
}
// ArrayIterationBenchmarkModule returns a module that iterates an array
// with `n` elements
func ArrayIterationBenchmarkModule(n int) string {
return fmt.Sprintf(`package test
fixture = [ x | x := numbers.range(1, %d)[_] ]
main { fixture[i] }`, n)
}
// SetIterationBenchmarkModule returns a module that iterates a set
// with `n` elements
func SetIterationBenchmarkModule(n int) string {
return fmt.Sprintf(`package test
fixture = { x | x := numbers.range(1, %d)[_] }
main { fixture[i] }`, n)
}
// ObjectIterationBenchmarkModule returns a module that iterates an object
// with `n` key/val pairs
func ObjectIterationBenchmarkModule(n int) string {
return fmt.Sprintf(`package test
fixture = { x: x | x := numbers.range(1, %d)[_] }
main { fixture[i] }`, n)
}
// GenerateLargeJSONBenchmarkData returns a map of 100 keys and 100.000 key/value
// pairs.
func GenerateLargeJSONBenchmarkData() map[string]interface{} {
return GenerateJSONBenchmarkData(100, 100*1000)
}
// GenerateJSONBenchmarkData returns a map of `k` keys and `v` key/value pairs.
func GenerateJSONBenchmarkData(k, v int) map[string]interface{} {
// create array of null values that can be iterated over
keys := make([]interface{}, k)
for i := range keys {
keys[i] = nil
}
// create large JSON object value (100,000 entries is about 2MB on disk)
values := map[string]interface{}{}
for i := 0; i < v; i++ {
values[fmt.Sprintf("key%d", i)] = fmt.Sprintf("value%d", i)
}
return map[string]interface{}{
"keys": keys,
"values": values,
}
}
// GenerateConcurrencyBenchmarkData returns a module and data; the module
// checks some input parameters against that data in a simple API authz
// scheme.
func GenerateConcurrencyBenchmarkData() (string, map[string]interface{}) {
obj := []byte(`
{
"objs": [
{
"attr1": "get",
"path": "/foo/bar",
"user": "bob"
},
{
"attr1": "set",
"path": "/foo/bar/baz",
"user": "alice"
},
{
"attr1": "get",
"path": "/foo",
"groups": [
"admin",
"eng"
]
},
{
"path": "/foo/bar",
"user": "alice"
}
]
}
`)
var data map[string]interface{}
if err := json.Unmarshal(obj, &data); err != nil {
panic(err)
}
mod := `package test
import data.objs
p {
objs[i].attr1 = "get"
objs[i].groups[j] = "eng"
}
p {
objs[i].user = "alice"
}
`
return mod, data
}
// GenerateVirtualDocsBenchmarkData generates a module and input; the
// numTotalRules and numHitRules create as many rules in the module to
// match/miss the returned input.
func GenerateVirtualDocsBenchmarkData(numTotalRules, numHitRules int) (string, map[string]interface{}) {
hitRule := `
allow {
input.method = "POST"
input.path = ["accounts", account_id]
input.user_id = account_id
}
`
missRule := `
allow {
input.method = "GET"
input.path = ["salaries", account_id]
input.user_id = account_id
}
`
testModuleTmpl := `package a.b.c
{{range .MissRules }}
{{ . }}
{{end}}
{{range .HitRules }}
{{ . }}
{{end}}
`
tmpl, err := template.New("Test").Parse(testModuleTmpl)
if err != nil {
panic(err)
}
var buf bytes.Buffer
var missRules []string
if numTotalRules > numHitRules {
missRules = make([]string, numTotalRules-numHitRules)
for i := range missRules {
missRules[i] = missRule
}
}
hitRules := make([]string, numHitRules)
for i := range hitRules {
hitRules[i] = hitRule
}
params := struct {
MissRules []string
HitRules []string
}{
MissRules: missRules,
HitRules: hitRules,
}
err = tmpl.Execute(&buf, params)
if err != nil {
panic(err)
}
input := map[string]interface{}{
"path": []interface{}{"accounts", "alice"},
"method": "POST",
"user_id": "alice",
}
return buf.String(), input
}