forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopdown_partial_bench_test.go
80 lines (60 loc) · 1.47 KB
/
topdown_partial_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
package topdown
import (
"context"
"fmt"
"testing"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/storage"
"github.com/open-policy-agent/opa/storage/inmem"
)
func BenchmarkInliningFullScan(b *testing.B) {
ctx := context.Background()
body := ast.MustParseBody("data.test.p = true")
unknowns := []*ast.Term{ast.MustParseTerm("input")}
compiler := ast.MustCompileModules(map[string]string{
"test.rego": `
package test
p {
data.a[i] == input
}
`,
})
sizes := []int{1000, 10000, 300000}
for _, n := range sizes {
b.Run(fmt.Sprint(n), func(b *testing.B) {
store := inmem.NewFromObject(generateInlineFullScanBenchmarkData(n))
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := storage.Txn(ctx, store, storage.TransactionParams{}, func(txn storage.Transaction) error {
q := NewQuery(body).
WithCompiler(compiler).
WithStore(store).
WithTransaction(txn).
WithUnknowns(unknowns)
queries, support, err := q.PartialRun(ctx)
if err != nil {
b.Fatal(err)
}
if len(queries) != n {
b.Fatal("Expected", n, "queries")
} else if len(support) != 0 {
b.Fatal("Unexpected support")
}
return nil
})
if err != nil {
b.Fatal(err)
}
}
})
}
}
func generateInlineFullScanBenchmarkData(n int) map[string]interface{} {
sl := make([]interface{}, n)
for i := range sl {
sl[i] = fmt.Sprint(i)
}
return map[string]interface{}{
"a": sl,
}
}