Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: add Regorus ACI benchmark tests #7298

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 83 additions & 7 deletions v1/rego/rego_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/open-policy-agent/opa/internal/runtime"
"github.com/open-policy-agent/opa/v1/ast"
"github.com/open-policy-agent/opa/v1/loader"
inmem "github.com/open-policy-agent/opa/v1/storage/inmem/test"
"github.com/open-policy-agent/opa/v1/util/test"
)
Expand Down Expand Up @@ -73,13 +74,7 @@ func BenchmarkPartialObjectRuleCrossModule(b *testing.B) {

func BenchmarkCustomFunctionInHotPath(b *testing.B) {
ctx := context.Background()

bs, err := os.ReadFile("testdata/ast.json")
if err != nil {
b.Fatal(err)
}

input := ast.MustParseTerm(string(bs))
input := ast.MustParseTerm(mustReadFileAsString(b, "testdata/ast.rego"))
module := ast.MustParseModule(`package test

import rego.v1
Expand Down Expand Up @@ -119,3 +114,84 @@ func BenchmarkCustomFunctionInHotPath(b *testing.B) {
}
}
}

// Benchmarks of the ACI test data from Regorus
// https://github.com/microsoft/regorus?tab=readme-ov-file#performance

// BenchmarkAciTestBuildAndEval-10 37 30700209 ns/op 16437935 B/op 384211 allocs/op
func BenchmarkAciTestBuildAndEval(b *testing.B) {
ctx := context.Background()

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
bundle, err := loader.NewFileLoader().
WithRegoVersion(ast.RegoV0).
AsBundle("testdata/aci")
if err != nil {
b.Fatal(err)
}

input := ast.MustParseTerm(mustReadFileAsString(b, "testdata/aci/input.json"))

r := New(Query("data.framework.mount_overlay = x"), ParsedBundle("", bundle))

pq, err := r.PrepareForEval(ctx)
if err != nil {
b.Fatal(err)
}

res, err := pq.Eval(ctx, EvalParsedInput(input.Value))
if err != nil {
b.Fatal(err)
}

_ = res
}
}

// BenchmarkAciTestOnlyEval-10 12752 92188 ns/op 50005 B/op 1062 allocs/op
func BenchmarkAciTestOnlyEval(b *testing.B) {
ctx := context.Background()

bundle, err := loader.NewFileLoader().
WithRegoVersion(ast.RegoV0).
AsBundle("testdata/aci")
if err != nil {
b.Fatal(err)
}

input := ast.MustParseTerm(mustReadFileAsString(b, "testdata/aci/input.json"))

r := New(Query("data.framework.mount_overlay = x"), ParsedBundle("", bundle))

pq, err := r.PrepareForEval(ctx)
if err != nil {
b.Fatal(err)
}

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {

res, err := pq.Eval(ctx, EvalParsedInput(input.Value))
if err != nil {
b.Fatal(err)
}

_ = res
}
}

func mustReadFileAsString(b *testing.B, path string) string {
b.Helper()

bs, err := os.ReadFile(path)
if err != nil {
b.Fatal(err)
}

return string(bs)
}
26 changes: 26 additions & 0 deletions v1/rego/testdata/aci/api.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

package api

version := "0.10.0"

enforcement_points := {
"mount_device": {"introducedVersion": "0.1.0", "default_results": {"allowed": false}},
"mount_overlay": {"introducedVersion": "0.1.0", "default_results": {"allowed": false}},
"create_container": {"introducedVersion": "0.1.0", "default_results": {"allowed": false, "env_list": null, "allow_stdio_access": false}},
"unmount_device": {"introducedVersion": "0.2.0", "default_results": {"allowed": true}},
"unmount_overlay": {"introducedVersion": "0.6.0", "default_results": {"allowed": true}},
"exec_in_container": {"introducedVersion": "0.2.0", "default_results": {"allowed": true, "env_list": null}},
"exec_external": {"introducedVersion": "0.3.0", "default_results": {"allowed": true, "env_list": null, "allow_stdio_access": false}},
"shutdown_container": {"introducedVersion": "0.4.0", "default_results": {"allowed": true}},
"signal_container_process": {"introducedVersion": "0.5.0", "default_results": {"allowed": true}},
"plan9_mount": {"introducedVersion": "0.6.0", "default_results": {"allowed": true}},
"plan9_unmount": {"introducedVersion": "0.6.0", "default_results": {"allowed": true}},
"get_properties": {"introducedVersion": "0.7.0", "default_results": {"allowed": true}},
"dump_stacks": {"introducedVersion": "0.7.0", "default_results": {"allowed": true}},
"runtime_logging": {"introducedVersion": "0.8.0", "default_results": {"allowed": true}},
"load_fragment": {"introducedVersion": "0.9.0", "default_results": {"allowed": false, "add_module": false}},
"scratch_mount": {"introducedVersion": "0.10.0", "default_results": {"allowed": true}},
"scratch_unmount": {"introducedVersion": "0.10.0", "default_results": {"allowed": true}},
}
12 changes: 12 additions & 0 deletions v1/rego/testdata/aci/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"metadata": {
"devices": {
"/run/layers/p0-layer0": "1b80f120dbd88e4355d6241b519c3e25290215c469516b49dece9cf07175a766",
"/run/layers/p0-layer1": "e769d7487cc314d3ee748a4440805317c19262c7acd2fdbdb0d47d2e4613a15c",
"/run/layers/p0-layer2": "eb36921e1f82af46dfe248ef8f1b3afb6a5230a64181d960d10237a08cd73c79",
"/run/layers/p0-layer3": "41d64cdeb347bf236b4c13b7403b633ff11f1cf94dbc7cf881a44d6da88c5156",
"/run/layers/p0-layer4": "4dedae42847c704da891a28c25d32201a1ae440bce2aecccfa8e6f03b97a6a6c",
"/run/layers/p0-layer5": "fe84c9d5bfddd07a2624d00333cf13c1a9c941f3a261f13ead44fc6a93bc0e7a"
}
}
}
Loading
Loading