Files
opencloud/services/policies/pkg/engine/opa/engine_bench_test.go
Pascal Bleser 8821053ff8 chore(policies): disable gRPC or event handlers by configuration + metrics
* add the ability to disable the gRPC API handler
   (POLICIES_GRPC_DISABLED)

 * add the ability to disable the Events API handler
   (POLICIES_EVENTS_DISABLED)

 * add metrics

 * add support for specifying rego files via the environment varirable
   POLICIES_ENGINE_FILES

 * for file paths specified in yaml or in POLICIES_ENGINE_FILES, support
   'config:' and 'data:' path prefixes

 * fix typos in the documentation, and try to make it more clear

 * add metrics to the documentation

 * add a section for testing in the documentation

 * introduces a new top-level package pkg/metrics/ with utilities for
   metrics that are backported from the groupware branch, with unit
   tests
2026-08-31 11:54:32 +02:00

71 lines
1.5 KiB
Go

package opa_test
import (
"context"
"path/filepath"
"testing"
"time"
"github.com/opencloud-eu/opencloud/pkg/log"
"github.com/opencloud-eu/opencloud/services/policies/pkg/config"
"github.com/opencloud-eu/opencloud/services/policies/pkg/engine"
"github.com/opencloud-eu/opencloud/services/policies/pkg/engine/opa"
)
func benchEngine(tb testing.TB) engine.Engine {
tb.Helper()
e, err := opa.NewOPA(10*time.Second, log.NopLogger(), config.Engine{Policies: []string{
filepath.Join("testdata", "bench", "proxy.rego"),
filepath.Join("testdata", "bench", "utils.rego"),
}}, nil)
if err != nil {
tb.Fatal(err)
}
return e
}
func benchEnvironment(name string) engine.Environment {
return engine.Environment{
Stage: engine.StageHTTP,
Request: engine.Request{Method: "PUT", Path: "/remote.php/dav/files/alice/" + name},
Resource: engine.Resource{Name: name},
}
}
func BenchmarkEvaluate(b *testing.B) {
e := benchEngine(b)
env := benchEnvironment("report.txt")
ctx := context.Background()
b.ReportAllocs()
for b.Loop() {
granted, err := e.Evaluate(ctx, "data.proxy.granted", env)
if err != nil {
b.Fatal(err)
}
if !granted {
b.Fatal("expected .txt to be granted")
}
}
}
func BenchmarkEvaluateParallel(b *testing.B) {
e := benchEngine(b)
env := benchEnvironment("report.txt")
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err := e.Evaluate(ctx, "data.proxy.granted", env); err != nil {
b.Fatal(err)
}
}
})
}