Skip to content

Commit 0886cc6

Browse files
authored
refactor(benchmark): execute each prepared benchmark case once (#203)
1 parent f093c43 commit 0886cc6

1 file changed

Lines changed: 34 additions & 57 deletions

File tree

‎internal/runner/benchmark_registry.go‎

Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (openClawBenchmarkAdapter) Config() string {
151151
return openClawBenchmarkConfig
152152
}
153153

154-
func (adapter openClawBenchmarkAdapter) RunCases(opts Options, ctx RunContext, env map[string]string, now func() time.Time, client BenchmarkClient) ([]BenchmarkCase, error) {
154+
func (openClawBenchmarkAdapter) RunCases(opts Options, ctx RunContext, env map[string]string, now func() time.Time, client BenchmarkClient) ([]BenchmarkCase, error) {
155155
rows, err := client.FetchOpenClawRows(openClawBenchmarkDataset, opts.Benchmark.Split, opts.Benchmark.Offset, opts.Benchmark.Limit)
156156
if err != nil {
157157
return nil, err
@@ -160,32 +160,9 @@ func (adapter openClawBenchmarkAdapter) RunCases(opts Options, ctx RunContext, e
160160
for _, row := range rows {
161161
cases = append(cases, openClawBenchmarkCase(opts, env, row))
162162
}
163-
for index, row := range rows {
164-
if err := workContext(ctx.Context).Err(); err != nil {
165-
return cases, err
166-
}
167-
cases[index], err = adapter.runCase(opts, ctx, env, now, row)
168-
if err != nil {
169-
return cases, err
170-
}
171-
}
172-
return cases, nil
173-
}
174-
175-
func (openClawBenchmarkAdapter) runCase(opts Options, ctx RunContext, env map[string]string, now func() time.Time, row OpenClawBenchmarkRow) (benchmarkCase BenchmarkCase, runErr error) {
176-
benchmarkCase = openClawBenchmarkCase(opts, env, row)
177-
defer finishBenchmarkCase(&benchmarkCase, opts, ctx, now, &runErr)
178-
dir, err := os.MkdirTemp("", "clawscan-benchmark-*")
179-
if err != nil {
180-
return benchmarkCase, err
181-
}
182-
defer os.RemoveAll(dir)
183-
target, err := materializeOpenClawBenchmarkRowContext(workContext(ctx.Context), dir, row)
184-
if err != nil {
185-
return benchmarkCase, err
186-
}
187-
benchmarkCase.Run, err = runBenchmarkTarget(opts, ctx, env, now, target)
188-
return benchmarkCase, err
163+
return runPreparedBenchmarkCases(opts, ctx, env, now, cases, func(dir string, index int) (string, error) {
164+
return materializeOpenClawBenchmarkRowContext(workContext(ctx.Context), dir, rows[index])
165+
})
189166
}
190167

191168
func openClawBenchmarkCase(opts Options, env map[string]string, row OpenClawBenchmarkRow) BenchmarkCase {
@@ -211,6 +188,33 @@ func unstartedBenchmarkRun(opts Options, env map[string]string) Artifact {
211188
return artifact
212189
}
213190

191+
func runPreparedBenchmarkCases(opts Options, ctx RunContext, env map[string]string, now func() time.Time, cases []BenchmarkCase, materialize func(string, int) (string, error)) ([]BenchmarkCase, error) {
192+
runCase := func(index int) (err error) {
193+
benchmarkCase := &cases[index]
194+
defer finishBenchmarkCase(benchmarkCase, opts, ctx, now, &err)
195+
dir, err := os.MkdirTemp("", "clawscan-benchmark-*")
196+
if err != nil {
197+
return err
198+
}
199+
defer os.RemoveAll(dir)
200+
target, err := materialize(dir, index)
201+
if err != nil {
202+
return err
203+
}
204+
benchmarkCase.Run, err = runBenchmarkTarget(opts, ctx, env, now, target)
205+
return err
206+
}
207+
for index := range cases {
208+
if err := workContext(ctx.Context).Err(); err != nil {
209+
return cases, err
210+
}
211+
if err := runCase(index); err != nil {
212+
return cases, err
213+
}
214+
}
215+
return cases, nil
216+
}
217+
214218
func finishBenchmarkCase(benchmarkCase *BenchmarkCase, opts Options, ctx RunContext, now func() time.Time, runErr *error) {
215219
if *runErr == nil || benchmarkCase.Run.Status != "not_started" {
216220
return
@@ -273,16 +277,9 @@ func (adapter skillTrustBenchBenchmarkAdapter) RunCases(opts Options, ctx RunCon
273277
}
274278
cases = append(cases, BenchmarkCase{ID: row.ID, SkillSlug: row.ID, SkillVersion: skillTrustBenchVersion, Expected: expected, Run: unstartedBenchmarkRun(opts, env)})
275279
}
276-
for index, row := range rows {
277-
if err := workContext(ctx.Context).Err(); err != nil {
278-
return cases, err
279-
}
280-
cases[index], err = adapter.runCase(opts, ctx, env, now, client, row)
281-
if err != nil {
282-
return cases, err
283-
}
284-
}
285-
return cases, nil
280+
return runPreparedBenchmarkCases(opts, ctx, env, now, cases, func(dir string, index int) (string, error) {
281+
return client.MaterializeSkillTrustBenchRow(dir, rows[index])
282+
})
286283
}
287284

288285
func selectSkillTrustBenchRows(rows []SkillTrustBenchRow, ids []string, split string) ([]SkillTrustBenchRow, error) {
@@ -301,26 +298,6 @@ func selectSkillTrustBenchRows(rows []SkillTrustBenchRow, ids []string, split st
301298
return selected, nil
302299
}
303300

304-
func (adapter skillTrustBenchBenchmarkAdapter) runCase(opts Options, ctx RunContext, env map[string]string, now func() time.Time, client BenchmarkClient, row SkillTrustBenchRow) (benchmarkCase BenchmarkCase, runErr error) {
305-
expected, err := adapter.expected(row)
306-
benchmarkCase = BenchmarkCase{ID: row.ID, SkillSlug: row.ID, SkillVersion: skillTrustBenchVersion, Expected: expected, Run: unstartedBenchmarkRun(opts, env)}
307-
defer finishBenchmarkCase(&benchmarkCase, opts, ctx, now, &runErr)
308-
if err != nil {
309-
return benchmarkCase, err
310-
}
311-
dir, err := os.MkdirTemp("", "clawscan-benchmark-*")
312-
if err != nil {
313-
return benchmarkCase, err
314-
}
315-
defer os.RemoveAll(dir)
316-
target, err := client.MaterializeSkillTrustBenchRow(dir, row)
317-
if err != nil {
318-
return benchmarkCase, err
319-
}
320-
benchmarkCase.Run, err = runBenchmarkTarget(opts, ctx, env, now, target)
321-
return benchmarkCase, err
322-
}
323-
324301
func (adapter skillTrustBenchBenchmarkAdapter) expected(row SkillTrustBenchRow) (BenchmarkExpected, error) {
325302
context, err := json.Marshal(struct {
326303
RiskLabels []string `json:"risk_labels"`

0 commit comments

Comments
 (0)