Skip to content

Commit

Permalink
Measure percentiles in benchmarks
Browse files Browse the repository at this point in the history
Signed-off-by: Farasath Ahamed <[email protected]>
  • Loading branch information
Farasath Ahamed committed Oct 20, 2024
1 parent f1da8d0 commit 7b660b0
Showing 1 changed file with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"testing"
"time"

"github.com/benburkert/pbench"

Check failure on line 16 in filters/openpolicyagent/opaauthorizerequest/opaauthorizerequest_test.go

View workflow job for this annotation

GitHub Actions / tests

no required module provides package github.com/benburkert/pbench; to add it:

Check failure on line 16 in filters/openpolicyagent/opaauthorizerequest/opaauthorizerequest_test.go

View workflow job for this annotation

GitHub Actions / check-race

no required module provides package github.com/benburkert/pbench; to add it:
"github.com/golang-jwt/jwt/v4"
opasdktest "github.com/open-policy-agent/opa/sdk/test"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -643,29 +644,35 @@ const (
//
// Note: Refer to the code for the latest available scenarios.
func BenchmarkAuthorizeRequest(b *testing.B) {

scenarios := []struct {
name string
benchmarkFunc func(b *testing.B, decisionLogging bool)
benchmarkFunc func(pb *pbench.B, decisionLogging bool)
}{
{"minimal", benchmarkMinimal},
{"with-body", benchmarkAllowWithReqBody},
{"jwt-validation", benchmarkJwtValidation},
}

for _, scenario := range scenarios {
b.Run(scenario.name, func(b *testing.B) {
b.Run("without-decision-logging", func(b *testing.B) {
scenario.benchmarkFunc(b, false)
b.Run(scenario.name, func(tb *testing.B) {
pb := pbench.New(tb)
pb.ReportPercentile(0.5)
pb.ReportPercentile(0.95)
pb.ReportPercentile(0.99)

pb.Run("no-decision-logs", func(pb *pbench.B) {
scenario.benchmarkFunc(pb, false)
})

b.Run("with-decision-logging", func(b *testing.B) {
scenario.benchmarkFunc(b, true)
pb.Run("with-decision-logs", func(pb *pbench.B) {
scenario.benchmarkFunc(pb, true)
})
})
}
}

func benchmarkMinimal(b *testing.B, decisionLogging bool) {
func benchmarkMinimal(b *pbench.B, decisionLogging bool) {
opaControlPlane := opasdktest.MustNewServer(
opasdktest.MockBundle("/bundles/somebundle.tar.gz", map[string]string{
"main.rego": `
Expand Down Expand Up @@ -702,16 +709,14 @@ func benchmarkMinimal(b *testing.B, decisionLogging bool) {
}

b.ResetTimer()
b.ReportAllocs()

b.RunParallel(func(pb *testing.PB) {
b.RunParallel(func(pb *pbench.PB) {
for pb.Next() {
f.Request(ctx)
}
})
}

func benchmarkAllowWithReqBody(b *testing.B, decisionLogging bool) {
func benchmarkAllowWithReqBody(b *pbench.B, decisionLogging bool) {
opaControlPlane := opasdktest.MustNewServer(
opasdktest.MockBundle("/bundles/somebundle.tar.gz", map[string]string{
"main.rego": `
Expand Down Expand Up @@ -755,16 +760,14 @@ func benchmarkAllowWithReqBody(b *testing.B, decisionLogging bool) {
}

b.ResetTimer()
b.ReportAllocs()

b.RunParallel(func(pb *testing.PB) {
b.RunParallel(func(pb *pbench.PB) {
for pb.Next() {
f.Request(ctx)
}
})
}

func benchmarkJwtValidation(b *testing.B, decisionLogging bool) {
func benchmarkJwtValidation(b *pbench.B, decisionLogging bool) {

publicKey, err := os.ReadFile(certPath)
if err != nil {
Expand Down Expand Up @@ -851,9 +854,7 @@ func benchmarkJwtValidation(b *testing.B, decisionLogging bool) {
}

b.ResetTimer()
b.ReportAllocs()

b.RunParallel(func(pb *testing.PB) {
b.RunParallel(func(pb *pbench.PB) {
for pb.Next() {
f.Request(ctx)
assert.False(b, ctx.FServed)
Expand Down

0 comments on commit 7b660b0

Please sign in to comment.