From 76b9603a1a9c5c95f85d4baff9723e08fc7f12cd Mon Sep 17 00:00:00 2001 From: Shai Nagar Date: Thu, 13 May 2021 10:55:51 +0300 Subject: [PATCH] optional scenario setup/teardown (#6) --- README.md | 30 +++++++++++++++++++++++++----- internal/config.go | 6 ++++-- internal/config_test.go | 10 ++++++++-- internal/exec.go | 31 ++++++++++++++++++++++++++----- test_data/config_test_load.json | 16 ++++++++++++++-- test_data/config_test_load.yaml | 13 +++++++++++-- 6 files changed, 88 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 9262188..2ac4488 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,6 @@ The config file can be either in JSON format or YAML. `benchy` assumes a file wi **Example YAML config:** ```yaml --- ---- alternate: true executions: 10 scenarios: @@ -63,12 +62,21 @@ scenarios: workingDir: "/tmp" env: KEY: value - before: + setup: + cmd: + - echo + - setupA + teardown: + cmd: + - echo + - teardownA + + beforeCommand: workingDir: "/another-path" cmd: - echo - beforeA - after: + afterCommand: cmd: - echo - afterA @@ -95,14 +103,26 @@ scenarios: "env": { "KEY": "value" }, - "before": { + "setup": { + "cmd": [ + "echo", + "setupA" + ] + }, + "teardown": { + "cmd": [ + "echo", + "teardownA" + ] + }, + "beforeCommand": { "workingDir": "/another-path", "cmd": [ "echo", "beforeA" ] }, - "after": { + "afterCommand": { "cmd": [ "echo", "afterA" diff --git a/internal/config.go b/internal/config.go index b5487ed..d447d83 100644 --- a/internal/config.go +++ b/internal/config.go @@ -17,8 +17,10 @@ type Scenario struct { Name string `json:"name" yaml:"name" binding:"required"` WorkingDirectory string `json:"workingDir" yaml:"workingDir"` Env map[string]string - Before *Command - After *Command + Setup *Command + Teardown *Command + BeforeCommand *Command `json:"beforeCommand" yaml:"beforeCommand" binding:"required"` + AfterCommand *Command `json:"aftercommand" yaml:"afterCommand" binding:"required"` Command *Command `json:"command" yaml:"command" binding:"required"` } diff --git a/internal/config_test.go b/internal/config_test.go index 1be34b0..1c8632e 100644 --- a/internal/config_test.go +++ b/internal/config_test.go @@ -31,11 +31,17 @@ func expectedBenchmarkConfig() *Benchmark { Name: "scenario A", WorkingDirectory: "/tmp", Env: map[string]string{"KEY": "value"}, - Before: &Command{ + Setup: &Command{ + Cmd: []string{"echo", "setupA"}, + }, + Teardown: &Command{ + Cmd: []string{"echo", "teardownA"}, + }, + BeforeCommand: &Command{ WorkingDirectory: "/another-path", Cmd: []string{"echo", "beforeA"}, }, - After: &Command{ + AfterCommand: &Command{ Cmd: []string{"echo", "afterA"}, }, Command: &Command{ diff --git a/internal/exec.go b/internal/exec.go index 5c26afd..e50b007 100644 --- a/internal/exec.go +++ b/internal/exec.go @@ -32,7 +32,15 @@ func Execute(b *Benchmark, ctx *Context) pkg.TracerSummary { func executeAlternately(b *Benchmark, ctx *Context) { for i := 1; i <= b.Executions; i++ { for si := range b.Scenarios { - executeScenario(b.Scenarios[si], ctx) + scenario := b.Scenarios[si] + + if i == 1 { + executeScenarioSetup(scenario, ctx) + } + executeScenarioCommand(scenario, ctx) + if i == b.Executions { + executeScenarioTeardown(scenario, ctx) + } } } } @@ -40,21 +48,34 @@ func executeAlternately(b *Benchmark, ctx *Context) { func executeSequencially(b *Benchmark, ctx *Context) { for si := range b.Scenarios { scenario := b.Scenarios[si] + + executeScenarioSetup(scenario, ctx) for i := 1; i <= b.Executions; i++ { - executeScenario(scenario, ctx) + executeScenarioCommand(scenario, ctx) } + executeScenarioTeardown(scenario, ctx) } } -func executeScenario(scenario *Scenario, ctx *Context) { +func executeScenarioSetup(scenario *Scenario, ctx *Context) { + log.Printf("Running setup for scenario '%s'...\r\n", scenario.Name) + executeCommand(scenario.Setup, scenario.WorkingDirectory, scenario.Env, ctx) +} + +func executeScenarioTeardown(scenario *Scenario, ctx *Context) { + log.Printf("Running teardown for scenario '%s'...\r\n", scenario.Name) + executeCommand(scenario.Teardown, scenario.WorkingDirectory, scenario.Env, ctx) +} + +func executeScenarioCommand(scenario *Scenario, ctx *Context) { log.Printf("Executing scenario '%s'...\r\n", scenario.Name) - executeCommand(scenario.Before, scenario.WorkingDirectory, scenario.Env, ctx) + executeCommand(scenario.BeforeCommand, scenario.WorkingDirectory, scenario.Env, ctx) ctx.tracer.Start(scenario)( executeCommand(scenario.Command, scenario.WorkingDirectory, scenario.Env, ctx), ) - executeCommand(scenario.After, scenario.WorkingDirectory, scenario.Env, ctx) + executeCommand(scenario.AfterCommand, scenario.WorkingDirectory, scenario.Env, ctx) } func executeCommand(cmd *Command, defaultWorkingDir string, env map[string]string, ctx *Context) (exitError error) { diff --git a/test_data/config_test_load.json b/test_data/config_test_load.json index 0c07599..bca816c 100644 --- a/test_data/config_test_load.json +++ b/test_data/config_test_load.json @@ -8,14 +8,26 @@ "env": { "KEY": "value" }, - "before": { + "setup": { + "cmd": [ + "echo", + "setupA" + ] + }, + "teardown": { + "cmd": [ + "echo", + "teardownA" + ] + }, + "beforeCommand": { "workingDir": "/another-path", "cmd": [ "echo", "beforeA" ] }, - "after": { + "afterCommand": { "cmd": [ "echo", "afterA" diff --git a/test_data/config_test_load.yaml b/test_data/config_test_load.yaml index 40ee066..775e19f 100644 --- a/test_data/config_test_load.yaml +++ b/test_data/config_test_load.yaml @@ -6,12 +6,21 @@ scenarios: workingDir: "/tmp" env: KEY: value - before: + setup: + cmd: + - echo + - setupA + teardown: + cmd: + - echo + - teardownA + + beforeCommand: workingDir: "/another-path" cmd: - echo - beforeA - after: + afterCommand: cmd: - echo - afterA