-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
deps: Improving
deps
command performance (#6688)
Improving memory footprint and execution time of deps command for policies with high dependency connectivity. Fixes: #6685 Signed-off-by: Johan Fylling <johan.dev@fylling.se>
- Loading branch information
1 parent
a94e585
commit e7e5b6b
Showing
2 changed files
with
148 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package dependencies | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/open-policy-agent/opa/ast" | ||
) | ||
|
||
func BenchmarkBase(b *testing.B) { | ||
ruleCounts := []int{10, 20, 50} | ||
for _, ruleCount := range ruleCounts { | ||
b.Run(fmt.Sprint(ruleCount), func(b *testing.B) { | ||
policy := makePolicy(ruleCount) | ||
module := ast.MustParseModule(policy) | ||
compiler := ast.NewCompiler() | ||
if compiler.Compile(map[string]*ast.Module{"test": module}); compiler.Failed() { | ||
b.Fatalf("Failed to compile policy: %v", compiler.Errors) | ||
} | ||
|
||
ref := ast.MustParseRef("data.test.main") | ||
|
||
b.ResetTimer() | ||
|
||
_, err := Base(compiler, ref) | ||
if err != nil { | ||
b.Fatalf("Failed to compute base doc deps: %v", err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkVirtual(b *testing.B) { | ||
ruleCounts := []int{10, 20, 50} | ||
for _, ruleCount := range ruleCounts { | ||
b.Run(fmt.Sprint(ruleCount), func(b *testing.B) { | ||
policy := makePolicy(ruleCount) | ||
module := ast.MustParseModule(policy) | ||
compiler := ast.NewCompiler() | ||
if compiler.Compile(map[string]*ast.Module{"test": module}); compiler.Failed() { | ||
b.Fatalf("Failed to compile policy: %v", compiler.Errors) | ||
} | ||
|
||
ref := ast.MustParseRef("data.test.main") | ||
|
||
b.ResetTimer() | ||
|
||
_, err := Virtual(compiler, ref) | ||
if err != nil { | ||
b.Fatalf("Failed to compute virtual doc deps: %v", err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// makePolicy constructs a policy with ruleCount number of rules. | ||
// Each rule will depend on as many other rules as possible without creating circular dependencies. | ||
func makePolicy(ruleCount int) string { | ||
var b strings.Builder | ||
b.WriteString("package test\n\n") | ||
|
||
b.WriteString("main {\n") | ||
for i := 0; i < ruleCount; i++ { | ||
b.WriteString(fmt.Sprintf(" p_%d\n", i)) | ||
} | ||
b.WriteString("}\n\n") | ||
|
||
for i := 0; i < ruleCount; i++ { | ||
b.WriteString(fmt.Sprintf("p_%d {\n", i)) | ||
for j := i + 1; j < ruleCount; j++ { | ||
b.WriteString(fmt.Sprintf(" p_%d\n", j)) | ||
} | ||
b.WriteString(" input.x == 1\n") | ||
b.WriteString(" input.y == 2\n") | ||
b.WriteString(" input.z == 3\n") | ||
b.WriteString("}\n") | ||
} | ||
return b.String() | ||
} |