Files
releases/v1/topdown/cache_bench_test.go
Anders Eknert b942136a4a Use Go 1.22+ int ranges (#7328)
With "some" help from `golangci-lint run --fix ./...`

Signed-off-by: Anders Eknert <anders@styra.com>
2025-01-30 09:57:27 +01:00

49 lines
1009 B
Go

// 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/v1/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 := range n {
k1 := ast.StringTerm(fmt.Sprintf("aaaa%v", i))
for j := range n {
k2 := ast.StringTerm(fmt.Sprintf("bbbb%v", j))
for k := range n {
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 := range b.N {
idx := i % max
cache.Put(keys[idx], values[idx])
result, _ := cache.Get(keys[idx])
if !result.Equal(values[idx]) {
b.Fatal("expected equal")
}
}
}