forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_bench_test.go
48 lines (38 loc) · 1 KB
/
cache_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
// Copyright 2019 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package topdown
import (
"fmt"
"testing"
"github.com/open-policy-agent/opa/ast"
)
func BenchmarkVirtualCache(b *testing.B) {
n := 10
max := n * n * n
keys := make([]ast.Ref, 0, max)
values := make([]*ast.Term, 0, max)
for i := 0; i < n; i++ {
k1 := ast.StringTerm(fmt.Sprintf("aaaa%v", i))
for j := 0; j < n; j++ {
k2 := ast.StringTerm(fmt.Sprintf("bbbb%v", j))
for k := 0; k < n; k++ {
k3 := ast.StringTerm(fmt.Sprintf("cccc%v", k))
key := ast.Ref{ast.DefaultRootDocument, k1, k2, k3}
value := ast.ArrayTerm(k1, k2, k3)
keys = append(keys, key)
values = append(values, value)
}
}
}
cache := newVirtualCache()
b.ResetTimer()
for i := 0; i < b.N; i++ {
idx := i % max
cache.Put(keys[idx], values[idx])
result := cache.Get(keys[idx])
if !result.Equal(values[idx]) {
b.Fatal("expected equal")
}
}
}