Files
releases/v1/topdown/object_bench_test.go
T
Ville Vesilehto f77322b3fb build: bump Go version requirement to 1.24 (#7839)
Go 1.23 is no longer supported as per Go release policy.

Changes:

- Use Go v1.24.6 as the project SDK requirement
- Apply lint fixes for Go 1.24
- Fix "non-constant format string in call" issues as seen in CI.

Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
2025-08-24 09:02:09 +02:00

110 lines
2.6 KiB
Go

// Copyright 2022 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"
"github.com/open-policy-agent/opa/v1/storage"
inmem "github.com/open-policy-agent/opa/v1/storage/inmem/test"
)
func genNxMObjectBenchmarkData(n, m int) ast.Value {
objList := make([]*ast.Term, n)
for i := range n {
v := ast.NewObject()
for j := range m {
v.Insert(ast.StringTerm(fmt.Sprintf("%d,%d", i, j)), ast.BooleanTerm(true))
}
objList[i] = ast.NewTerm(v)
}
return ast.NewArray(objList...)
}
func BenchmarkObjectUnionN(b *testing.B) {
ctx := b.Context()
sizes := []int{10, 100, 250}
for _, n := range sizes {
for _, m := range sizes {
b.Run(fmt.Sprintf("%dx%d", n, m), func(b *testing.B) {
store := inmem.NewFromObject(map[string]any{"objs": genNxMObjectBenchmarkData(n, m)})
module := `package test
combined := object.union_n(data.objs)`
query := ast.MustParseBody("data.test.combined")
compiler := ast.MustCompileModules(map[string]string{
"test.rego": module,
})
b.ResetTimer()
for range b.N {
err := storage.Txn(ctx, store, storage.TransactionParams{}, func(txn storage.Transaction) error {
_, err := NewQuery(query).
WithCompiler(compiler).
WithStore(store).
WithTransaction(txn).
Run(ctx)
return err
})
if err != nil {
b.Fatal(err)
}
}
})
}
}
}
func BenchmarkObjectUnionNSlow(b *testing.B) {
// This benchmarks the suggested means to implement union
// without using the builtin, to give us an idea of whether or not
// the builtin is actually making things any faster.
ctx := b.Context()
sizes := []int{10, 100, 250}
for _, n := range sizes {
for _, m := range sizes {
b.Run(fmt.Sprintf("%dx%d", n, m), func(b *testing.B) {
store := inmem.NewFromObject(map[string]any{"objs": genNxMObjectBenchmarkData(n, m)})
module := `package test
combined := {k: true | s := data.objs[_]; s[k]}`
query := ast.MustParseBody("data.test.combined")
compiler := ast.MustCompileModules(map[string]string{
"test.rego": module,
})
b.ResetTimer()
for range b.N {
err := storage.Txn(ctx, store, storage.TransactionParams{}, func(txn storage.Transaction) error {
_, err := NewQuery(query).
WithCompiler(compiler).
WithStore(store).
WithTransaction(txn).
Run(ctx)
return err
})
if err != nil {
b.Fatal(err)
}
}
})
}
}
}