Skip to content

Commit d35f19d

Browse files
committed
refactor runner to step through test one by one
1 parent 251bb1d commit d35f19d

File tree

5 files changed

+302
-180
lines changed

5 files changed

+302
-180
lines changed

benchmarks/bench-kafka-kafka/bench-kafka-kafka.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ tests:
4040
conduit:
4141
docker-compose: ./conduit/docker-compose-conduit.override.yml
4242

43-
steps:
43+
steps: # TODO rename to hooks?
4444
pre-infrastructure:
4545
post-infrastructure:
4646
- name: "Create topics"

cmd/benchi-bubble/main.go

+48-13
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ func mainE() error {
6161
return fmt.Errorf("failed to create output directory: %w", err)
6262
}
6363

64-
logPath := filepath.Join(*outPath, fmt.Sprintf("%s_benchi.log", time.Now().Format("20060102150405")))
64+
now := time.Now()
65+
logPath := filepath.Join(*outPath, fmt.Sprintf("%s_benchi.log", now.Format("20060102150405")))
6566
logFile, err := os.Create(logPath)
6667
if err != nil {
6768
return fmt.Errorf("failed to create log file: %w", err)
@@ -88,14 +89,28 @@ func mainE() error {
8889
slog.Info("Using network", "network", net.Name, "network-id", net.ID)
8990
defer dockerutil.RemoveNetwork(ctx, dockerClient, net.Name)
9091

92+
// Resolve absolute path before changing working directory
93+
*outPath, err = filepath.Abs(*outPath)
94+
if err != nil {
95+
return fmt.Errorf("failed to get absolute path for output directory: %w", err)
96+
}
97+
9198
// Change working directory to config path, all relative paths are relative to the config file.
9299
err = os.Chdir(filepath.Dir(*configPath))
93100
if err != nil {
94101
return fmt.Errorf("could not change working directory: %w", err)
95102
}
96103

104+
tr := benchi.BuildTestRunners(cfg, benchi.TestRunnerOptions{
105+
ResultsDir: *outPath,
106+
StartedAt: now,
107+
FilterTests: nil,
108+
FilterTools: nil,
109+
DockerClient: dockerClient,
110+
})
111+
97112
// TODO run all tests
98-
model, err := newTestModel(cfg, dockerClient)
113+
model, err := newTestModel(dockerClient, tr[0])
99114
if err != nil {
100115
return fmt.Errorf("failed to create test model: %w", err)
101116
}
@@ -118,25 +133,30 @@ func parseConfig() (config.Config, error) {
118133
}
119134

120135
type testModel struct {
121-
config config.Config
122-
tool string
136+
runner *benchi.TestRunner
137+
errors []error
138+
currentStep benchi.Step
123139

124140
infrastructureModel *composeProjectModel
125141
toolsModel *composeProjectModel
126142
}
127143

128-
func newTestModel(cfg config.Config, client client.APIClient) (tea.Model, error) {
129-
infraFiles := make([]string, 0, len(cfg.Infrastructure))
130-
for _, f := range cfg.Infrastructure {
144+
type stepResult struct {
145+
err error
146+
}
147+
148+
func newTestModel(client client.APIClient, runner *benchi.TestRunner) (tea.Model, error) {
149+
infraFiles := make([]string, 0, len(runner.Infrastructure()))
150+
for _, f := range runner.Infrastructure() {
131151
infraFiles = append(infraFiles, f.DockerCompose)
132152
}
133153
infraComposeProject, err := benchi.NewComposeProject(infraFiles, client)
134154
if err != nil {
135155
return nil, err
136156
}
137157

138-
toolFiles := make([]string, 0, len(cfg.Tools))
139-
for _, f := range cfg.Tools {
158+
toolFiles := make([]string, 0, len(runner.Tools()))
159+
for _, f := range runner.Tools() {
140160
toolFiles = append(toolFiles, f.DockerCompose)
141161
}
142162
toolComposeProject, err := benchi.NewComposeProject(toolFiles, client)
@@ -145,8 +165,7 @@ func newTestModel(cfg config.Config, client client.APIClient) (tea.Model, error)
145165
}
146166

147167
return &testModel{
148-
config: cfg,
149-
tool: "conduit",
168+
runner: runner,
150169

151170
infrastructureModel: &composeProjectModel{
152171
name: "Infrastructure",
@@ -160,7 +179,14 @@ func newTestModel(cfg config.Config, client client.APIClient) (tea.Model, error)
160179
}
161180

162181
func (m *testModel) Init() tea.Cmd {
163-
return tea.Batch(m.infrastructureModel.Init(), m.toolsModel.Init())
182+
return tea.Batch(m.infrastructureModel.Init(), m.toolsModel.Init(), m.step())
183+
}
184+
185+
func (m *testModel) step() tea.Cmd {
186+
return func() tea.Msg {
187+
err := m.runner.RunStep(context.Background())
188+
return stepResult{err: err}
189+
}
164190
}
165191

166192
func (m *testModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
@@ -179,13 +205,22 @@ func (m *testModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
179205
}
180206

181207
return m, tea.Batch(cmds...)
208+
case stepResult:
209+
if msg.err != nil {
210+
m.errors = append(m.errors, msg.err)
211+
}
212+
m.currentStep = m.runner.Step()
213+
if m.currentStep != benchi.StepDone {
214+
return m, m.step()
215+
}
182216
}
183217

184218
return m, nil
185219
}
186220

187221
func (m *testModel) View() string {
188-
s := "Running test Foo (1/3)"
222+
s := fmt.Sprintf("Running test %s (1/3)", m.runner.Name())
223+
s = fmt.Sprintf("Step: %s", m.currentStep)
189224
s += "\n\n"
190225
s += m.infrastructureModel.View()
191226
s += "\n"

cmd/benchi/main.go

+28-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"path/filepath"
2424
"strings"
25+
"time"
2526

2627
"github.com/conduitio/benchi"
2728
"github.com/conduitio/benchi/config"
@@ -58,9 +59,15 @@ func mainE() error {
5859
return fmt.Errorf("config path is required")
5960
}
6061

61-
err := os.Chdir(filepath.Dir(*configPath))
62+
cfg, err := parseConfig()
6263
if err != nil {
63-
return fmt.Errorf("could not change working directory: %w", err)
64+
return err
65+
}
66+
67+
// Create output directory if it does not exist.
68+
err = os.MkdirAll(*outPath, 0o755)
69+
if err != nil {
70+
return fmt.Errorf("failed to create output directory: %w", err)
6471
}
6572

6673
dockerClient, err := client.NewClientWithOpts(client.FromEnv)
@@ -76,17 +83,32 @@ func mainE() error {
7683
slog.Info("Using network", "network", net.Name, "network-id", net.ID)
7784
defer dockerutil.RemoveNetwork(ctx, dockerClient, net.Name)
7885

79-
cfg, err := parseConfig()
86+
// Resolve absolute path before changing working directory
87+
*outPath, err = filepath.Abs(*outPath)
8088
if err != nil {
81-
return err
89+
return fmt.Errorf("failed to get absolute path for output directory: %w", err)
8290
}
8391

84-
err = benchi.Run(ctx, cfg, benchi.RunOptions{
85-
OutPath: *outPath,
92+
err = os.Chdir(filepath.Dir(*configPath))
93+
if err != nil {
94+
return fmt.Errorf("could not change working directory: %w", err)
95+
}
96+
97+
tests := benchi.BuildTestRunners(cfg, benchi.TestRunnerOptions{
98+
ResultsDir: *outPath,
99+
StartedAt: time.Now(),
86100
FilterTests: nil, // TODO: implement filter
101+
FilterTools: nil, // TODO: implement filter
87102
DockerClient: dockerClient,
88103
})
89104

105+
for _, t := range tests {
106+
err := t.Run(ctx)
107+
if err != nil {
108+
return fmt.Errorf("failed to run test: %w", err)
109+
}
110+
}
111+
90112
return nil
91113
}
92114

0 commit comments

Comments
 (0)