diff --git a/cmd/bench.go b/cmd/bench.go index c13e3a4e65..210597ce17 100644 --- a/cmd/bench.go +++ b/cmd/bench.go @@ -46,6 +46,7 @@ type benchmarkCommandParams struct { e2e bool gracefulShutdownPeriod int shutdownWaitPeriod int + configFile string } const ( @@ -125,6 +126,7 @@ The optional "gobench" output format conforms to the Go Benchmark Data Format. addBenchmemFlag(benchCommand.Flags(), ¶ms.benchMem, true) addE2EFlag(benchCommand.Flags(), ¶ms.e2e, false) + addConfigFileFlag(benchCommand.Flags(), ¶ms.configFile) benchCommand.Flags().IntVar(¶ms.gracefulShutdownPeriod, "shutdown-grace-period", 10, "set the time (in seconds) that the server will wait to gracefully shut down. This flag is valid in 'e2e' mode only.") benchCommand.Flags().IntVar(¶ms.shutdownWaitPeriod, "shutdown-wait-period", 0, "set the time (in seconds) that the server will wait before initiating shutdown. This flag is valid in 'e2e' mode only.") @@ -296,6 +298,7 @@ func benchE2E(ctx context.Context, args []string, params benchmarkCommandParams, EnableVersionCheck: false, GracefulShutdownPeriod: params.gracefulShutdownPeriod, ShutdownWaitPeriod: params.shutdownWaitPeriod, + ConfigFile: params.configFile, } rt, err := runtime.NewRuntime(ctx, rtParams) diff --git a/cmd/bench_test.go b/cmd/bench_test.go index 1d8083f448..fcd80a7359 100644 --- a/cmd/bench_test.go +++ b/cmd/bench_test.go @@ -102,6 +102,59 @@ func TestRunBenchmarkE2E(t *testing.T) { } } +func TestRunBenchmarkE2EWithOPAConfigFile(t *testing.T) { + + fs := map[string]string{ + "/config.yaml": `{"decision_logs": {"console": true}}`, + } + + test.WithTempFS(fs, func(testDirRoot string) { + + params := testBenchParams() + params.e2e = true + params.configFile = filepath.Join(testDirRoot, "/config.yaml") + + args := []string{"1 + 1"} + var buf bytes.Buffer + + rc, err := benchMain(args, params, &buf, &goBenchRunner{}) + if err != nil { + t.Fatalf("Unexpected error: %s", err) + } + + if rc != 0 { + t.Fatalf("Unexpected return code %d, expected 0", rc) + } + + // Expect a json serialized benchmark result with histogram fields + var br testing.BenchmarkResult + err = util.UnmarshalJSON(buf.Bytes(), &br) + if err != nil { + t.Fatalf("Unexpected error unmarshalling output: %s", err) + } + + if br.N == 0 || br.T == 0 || br.MemAllocs == 0 || br.MemBytes == 0 { + t.Fatalf("Expected benchmark results to be non-zero, got: %+v", br) + } + + if _, ok := br.Extra["histogram_timer_rego_query_eval_ns_count"]; !ok { + t.Fatalf("Expected benchmark results to contain 'histogram_timer_rego_query_eval_ns_count', got: %+v", br) + } + + if float64(br.N) != br.Extra["histogram_timer_rego_query_eval_ns_count"] { + t.Fatalf("Expected 'histogram_timer_rego_query_eval_ns_count' to be equal to N") + } + + if _, ok := br.Extra["histogram_timer_server_handler_ns_count"]; !ok { + t.Fatalf("Expected benchmark results to contain 'histogram_timer_server_handler_ns_count', got: %+v", br) + } + + if float64(br.N) != br.Extra["histogram_timer_server_handler_ns_count"] { + t.Fatalf("Expected 'histogram_timer_server_handler_ns_count' to be equal to N") + } + }) +} + func TestRunBenchmarkFailFastE2E(t *testing.T) { params := testBenchParams() params.fail = true // configured to fail on undefined results