forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cover: Disable local var plugging on trace
Similar treatment as what we do with the `Profiler`, the coverage tracer will now signal via configuration that it doesn't need local variable metadata. This improves the speed of evaluations with only coverage enabled by a pretty substantial amount, in particular with many/larger variables. Signed-off-by: Patrick East <east.patrick@gmail.com>
- Loading branch information
1 parent
837011c
commit dff78c4
Showing
3 changed files
with
100 additions
and
0 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,78 @@ | ||
// Copyright 2020 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 cover | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/open-policy-agent/opa/ast" | ||
"github.com/open-policy-agent/opa/rego" | ||
) | ||
|
||
func BenchmarkCoverBigLocalVar(b *testing.B) { | ||
iterations := []int{1, 100, 1000} | ||
vars := []int{1, 10} | ||
|
||
for _, iterationCount := range iterations { | ||
for _, varCount := range vars { | ||
name := fmt.Sprintf("%dVars%dIterations", varCount, iterationCount) | ||
b.Run(name, func(b *testing.B) { | ||
cover := New() | ||
module := generateModule(varCount, iterationCount) | ||
|
||
_, err := ast.ParseModule("test.rego", module) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
pq, err := rego.New( | ||
rego.Module("test.rego", module), | ||
rego.Query("data.test.p"), | ||
).PrepareForEval(ctx) | ||
|
||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
|
||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
b.StartTimer() | ||
_, err = pq.Eval(ctx, rego.EvalTracer(cover)) | ||
b.StopTimer() | ||
|
||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
|
||
func generateModule(numVars int, dataSize int) string { | ||
sb := strings.Builder{} | ||
sb.WriteString(`package test | ||
p { | ||
x := a | ||
v := x[i] | ||
`) | ||
for i := 0; i < numVars; i++ { | ||
sb.WriteString(fmt.Sprintf("\tv%d := x[i+%d]\n", i, i)) | ||
} | ||
sb.WriteString("\tfalse\n}\n") | ||
sb.WriteString("\na := [\n") | ||
for i := 0; i < dataSize; i++ { | ||
sb.WriteString(fmt.Sprintf("\t%d,\n", i)) | ||
} | ||
sb.WriteString("]\n") | ||
return sb.String() | ||
} |
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