|
1 | 1 | package exec |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + |
4 | 6 | "github.com/sha1n/bert/api" |
5 | 7 | ) |
6 | 8 |
|
7 | 9 | // Execute executes a benchmark and returns an object that provides access to collected stats. |
8 | | -func Execute(spec api.BenchmarkSpec, ctx api.ExecutionContext) { |
9 | | - ctx.Listener.OnBenchmarkStart() |
10 | | - defer ctx.Listener.OnBenchmarkEnd() |
| 10 | +func Execute(ctx context.Context, spec api.BenchmarkSpec, execCtx api.ExecutionContext) { |
| 11 | + execCtx.Listener.OnBenchmarkStart() |
| 12 | + defer execCtx.Listener.OnBenchmarkEnd() |
11 | 13 |
|
12 | 14 | if spec.Alternate { |
13 | | - executeAlternately(spec, ctx) |
| 15 | + executeAlternately(ctx, spec, execCtx) |
14 | 16 | } else { |
15 | | - executeSequentially(spec, ctx) |
| 17 | + executeSequentially(ctx, spec, execCtx) |
16 | 18 | } |
17 | 19 | } |
18 | 20 |
|
19 | | -func executeAlternately(spec api.BenchmarkSpec, ctx api.ExecutionContext) { |
| 21 | +func executeAlternately(ctx context.Context, spec api.BenchmarkSpec, execCtx api.ExecutionContext) { |
20 | 22 | for i := 1; i <= spec.Executions; i++ { |
21 | 23 | for si := range spec.Scenarios { |
| 24 | + if ctx.Err() != nil { |
| 25 | + return |
| 26 | + } |
| 27 | + |
22 | 28 | scenario := spec.Scenarios[si] |
23 | 29 |
|
24 | | - ctx.Listener.OnScenarioStart(scenario.ID()) |
| 30 | + execCtx.Listener.OnScenarioStart(scenario.ID()) |
25 | 31 | if i == 1 { |
26 | | - executeScenarioSetup(scenario, ctx) |
| 32 | + executeScenarioSetup(ctx, scenario, execCtx) |
27 | 33 | } |
28 | | - executeScenarioCommand(scenario, i, spec.Executions, ctx) |
| 34 | + executeScenarioCommand(ctx, scenario, i, spec.Executions, execCtx) |
29 | 35 | if i == spec.Executions { |
30 | | - executeScenarioTeardown(scenario, ctx) |
| 36 | + executeScenarioTeardown(ctx, scenario, execCtx) |
31 | 37 | } |
32 | 38 |
|
33 | | - ctx.Listener.OnScenarioEnd(scenario.ID()) |
| 39 | + execCtx.Listener.OnScenarioEnd(scenario.ID()) |
34 | 40 | } |
35 | 41 | } |
36 | 42 | } |
37 | 43 |
|
38 | | -func executeSequentially(spec api.BenchmarkSpec, ctx api.ExecutionContext) { |
| 44 | +func executeSequentially(ctx context.Context, spec api.BenchmarkSpec, execCtx api.ExecutionContext) { |
39 | 45 | for si := range spec.Scenarios { |
40 | 46 | scenario := spec.Scenarios[si] |
41 | 47 |
|
42 | 48 | for i := 1; i <= spec.Executions; i++ { |
43 | | - ctx.Listener.OnScenarioStart(scenario.ID()) |
| 49 | + if ctx.Err() != nil { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + execCtx.Listener.OnScenarioStart(scenario.ID()) |
44 | 54 | if i == 1 { |
45 | | - executeScenarioSetup(scenario, ctx) |
| 55 | + executeScenarioSetup(ctx, scenario, execCtx) |
46 | 56 | } |
47 | 57 |
|
48 | | - executeScenarioCommand(scenario, i, spec.Executions, ctx) |
| 58 | + executeScenarioCommand(ctx, scenario, i, spec.Executions, execCtx) |
49 | 59 |
|
50 | 60 | if i == spec.Executions { |
51 | | - executeScenarioTeardown(scenario, ctx) |
| 61 | + executeScenarioTeardown(ctx, scenario, execCtx) |
52 | 62 | } |
53 | | - ctx.Listener.OnScenarioEnd(scenario.ID()) |
| 63 | + execCtx.Listener.OnScenarioEnd(scenario.ID()) |
54 | 64 | } |
55 | 65 | } |
56 | 66 | } |
57 | 67 |
|
58 | | -func executeScenarioSetup(scenario api.ScenarioSpec, ctx api.ExecutionContext) { |
| 68 | +func executeScenarioSetup(ctx context.Context, scenario api.ScenarioSpec, execCtx api.ExecutionContext) { |
59 | 69 | if scenario.BeforeAll != nil { |
60 | | - ctx.Listener.OnMessagef(scenario.ID(), "running 'beforeAll' command %v...", scenario.BeforeAll.Cmd) |
61 | | - reportIfExecError(ctx.Executor.ExecuteFn(scenario.BeforeAll, scenario.WorkingDirectory, scenario.Env), scenario.ID(), ctx) |
| 70 | + execCtx.Listener.OnMessagef(scenario.ID(), "running 'beforeAll' command %v...", scenario.BeforeAll.Cmd) |
| 71 | + reportIfExecError(execCtx.Executor.ExecuteFn(ctx, scenario.BeforeAll, scenario.WorkingDirectory, scenario.Env), scenario.ID(), execCtx) |
62 | 72 | } |
63 | 73 | } |
64 | 74 |
|
65 | | -func executeScenarioTeardown(scenario api.ScenarioSpec, ctx api.ExecutionContext) { |
| 75 | +func executeScenarioTeardown(ctx context.Context, scenario api.ScenarioSpec, execCtx api.ExecutionContext) { |
66 | 76 | if scenario.AfterAll != nil { |
67 | | - ctx.Listener.OnMessagef(scenario.ID(), "running 'afterAll' command %v...", scenario.AfterAll.Cmd) |
68 | | - reportIfExecError(ctx.Executor.ExecuteFn(scenario.AfterAll, scenario.WorkingDirectory, scenario.Env), scenario.ID(), ctx) |
| 77 | + execCtx.Listener.OnMessagef(scenario.ID(), "running 'afterAll' command %v...", scenario.AfterAll.Cmd) |
| 78 | + reportIfExecError(execCtx.Executor.ExecuteFn(ctx, scenario.AfterAll, scenario.WorkingDirectory, scenario.Env), scenario.ID(), execCtx) |
69 | 79 | } |
70 | 80 | } |
71 | 81 |
|
72 | | -func executeScenarioCommand(scenario api.ScenarioSpec, execIndex int, totalExec int, ctx api.ExecutionContext) { |
73 | | - ctx.Listener.OnMessagef(scenario.ID(), "run %d of %d", execIndex, totalExec) |
| 82 | +func executeScenarioCommand(ctx context.Context, scenario api.ScenarioSpec, execIndex int, totalExec int, execCtx api.ExecutionContext) { |
| 83 | + execCtx.Listener.OnMessagef(scenario.ID(), "run %d of %d", execIndex, totalExec) |
74 | 84 | if scenario.BeforeEach != nil { |
75 | | - ctx.Listener.OnMessagef(scenario.ID(), "running 'beforeEach' command %v", scenario.BeforeEach.Cmd) |
76 | | - reportIfExecError(ctx.Executor.ExecuteFn(scenario.BeforeEach, scenario.WorkingDirectory, scenario.Env), scenario.ID(), ctx) |
| 85 | + execCtx.Listener.OnMessagef(scenario.ID(), "running 'beforeEach' command %v", scenario.BeforeEach.Cmd) |
| 86 | + reportIfExecError(execCtx.Executor.ExecuteFn(ctx, scenario.BeforeEach, scenario.WorkingDirectory, scenario.Env), scenario.ID(), execCtx) |
77 | 87 | } |
78 | 88 |
|
79 | | - ctx.Listener.OnMessagef(scenario.ID(), "running benchmark command %v", scenario.Command.Cmd) |
80 | | - executeFn := ctx.Executor.ExecuteFn(scenario.Command, scenario.WorkingDirectory, scenario.Env) |
| 89 | + execCtx.Listener.OnMessagef(scenario.ID(), "running benchmark command %v", scenario.Command.Cmd) |
| 90 | + executeFn := execCtx.Executor.ExecuteFn(ctx, scenario.Command, scenario.WorkingDirectory, scenario.Env) |
81 | 91 |
|
82 | | - endTrace := ctx.Tracer.Start(scenario) |
| 92 | + endTrace := execCtx.Tracer.Start(scenario) |
83 | 93 | info, err := executeFn() |
84 | 94 | endTrace(info, err) |
85 | 95 |
|
86 | | - reportIfError(err, scenario.ID(), ctx) |
| 96 | + reportIfError(err, scenario.ID(), execCtx) |
87 | 97 |
|
88 | 98 | if scenario.AfterEach != nil { |
89 | | - ctx.Listener.OnMessagef(scenario.ID(), "running 'afterEach' command %v", scenario.AfterEach.Cmd) |
90 | | - reportIfExecError(ctx.Executor.ExecuteFn(scenario.AfterEach, scenario.WorkingDirectory, scenario.Env), scenario.ID(), ctx) |
| 99 | + execCtx.Listener.OnMessagef(scenario.ID(), "running 'afterEach' command %v", scenario.AfterEach.Cmd) |
| 100 | + reportIfExecError(execCtx.Executor.ExecuteFn(ctx, scenario.AfterEach, scenario.WorkingDirectory, scenario.Env), scenario.ID(), execCtx) |
91 | 101 | } |
92 | 102 | } |
93 | 103 |
|
|
0 commit comments