Skip to content

Commit

Permalink
optional scenario setup/teardown (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
sha1n authored May 13, 2021
1 parent 599c62f commit 76b9603
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 18 deletions.
30 changes: 25 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,28 @@ 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:
- name: scenario A
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
Expand All @@ -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"
Expand Down
6 changes: 4 additions & 2 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
10 changes: 8 additions & 2 deletions internal/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
31 changes: 26 additions & 5 deletions internal/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,50 @@ 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)
}
}
}
}

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) {
Expand Down
16 changes: 14 additions & 2 deletions test_data/config_test_load.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 11 additions & 2 deletions test_data/config_test_load.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 76b9603

Please sign in to comment.