mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
cmd: Extend e2e benchmarking mode to include OPA config (#5335)
Currently the e2e bench mode doesn't support providing an OPA configuration to enable features like decision logging that can have an impact on the server overhead. This change adds a new flag to the bench cmd to specify the OPA configuration. Fixes: #4899 Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user