Skip to content

Commit f8ae3ea

Browse files
committed
Move logic to build the output path to BenchmarkRunner
1 parent c43731e commit f8ae3ea

File tree

4 files changed

+102
-74
lines changed

4 files changed

+102
-74
lines changed

lib/benchmark_runner.rb

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,49 @@
66

77
# Extracted helper methods from run_benchmarks.rb for testing
88
module BenchmarkRunner
9-
module_function
9+
class << self
10+
# Determine output path - either use the override or find a free file number
11+
def output_path(out_path_dir, out_override: nil)
12+
if out_override
13+
out_override
14+
else
15+
# If no out path is specified, find a free file index for the output files
16+
file_no = free_file_no(out_path_dir)
17+
File.join(out_path_dir, "output_%03d" % file_no)
18+
end
19+
end
1020

11-
# Find the first available file number for output files
12-
def free_file_no(directory)
13-
(1..).each do |file_no|
14-
out_path = File.join(directory, "output_%03d.csv" % file_no)
15-
return file_no unless File.exist?(out_path)
21+
# Render a graph from JSON benchmark data
22+
def render_graph(json_path)
23+
png_path = json_path.sub(/\.json$/, '.png')
24+
require_relative 'graph_renderer'
25+
GraphRenderer.render(json_path, png_path)
1626
end
17-
end
1827

19-
# Render a graph from JSON benchmark data
20-
def render_graph(json_path)
21-
png_path = json_path.sub(/\.json$/, '.png')
22-
require_relative 'graph_renderer'
23-
GraphRenderer.render(json_path, png_path)
24-
end
28+
# Checked system - error or return info if the command fails
29+
def check_call(command, env: {}, raise_error: true, quiet: false)
30+
puts("+ #{command}") unless quiet
2531

26-
# Checked system - error or return info if the command fails
27-
def check_call(command, env: {}, raise_error: true, quiet: false)
28-
puts("+ #{command}") unless quiet
32+
result = {}
2933

30-
result = {}
34+
result[:success] = system(env, command)
35+
result[:status] = $?
3136

32-
result[:success] = system(env, command)
33-
result[:status] = $?
37+
unless result[:success]
38+
puts "Command #{command.inspect} failed with exit code #{result[:status].exitstatus} in directory #{Dir.pwd}"
39+
raise RuntimeError.new if raise_error
40+
end
3441

35-
unless result[:success]
36-
puts "Command #{command.inspect} failed with exit code #{result[:status].exitstatus} in directory #{Dir.pwd}"
37-
raise RuntimeError.new if raise_error
42+
result
3843
end
3944

40-
result
45+
private
46+
47+
def free_file_no(directory)
48+
(1..).each do |file_no|
49+
out_path = File.join(directory, "output_%03d.csv" % file_no)
50+
return file_no unless File.exist?(out_path)
51+
end
52+
end
4153
end
4254
end

run_benchmarks.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,7 @@
6666
)
6767
table, format = builder.build
6868

69-
output_path = nil
70-
if args.out_override
71-
output_path = args.out_override
72-
else
73-
# If no out path is specified, find a free file index for the output files
74-
file_no = BenchmarkRunner.free_file_no(args.out_path)
75-
output_path = File.join(args.out_path, "output_%03d" % file_no)
76-
end
69+
output_path = BenchmarkRunner.output_path(args.out_path, out_override: args.out_override)
7770

7871
# Save the raw data as JSON
7972
out_json_path = output_path + ".json"

test/benchmark_runner_test.rb

Lines changed: 62 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,6 @@
99
require 'yaml'
1010

1111
describe BenchmarkRunner do
12-
describe '.free_file_no' do
13-
it 'returns 1 when no files exist' do
14-
Dir.mktmpdir do |dir|
15-
file_no = BenchmarkRunner.free_file_no(dir)
16-
assert_equal 1, file_no
17-
end
18-
end
19-
20-
it 'returns next available number when files exist' do
21-
Dir.mktmpdir do |dir|
22-
FileUtils.touch(File.join(dir, 'output_001.csv'))
23-
FileUtils.touch(File.join(dir, 'output_002.csv'))
24-
25-
file_no = BenchmarkRunner.free_file_no(dir)
26-
assert_equal 3, file_no
27-
end
28-
end
29-
30-
it 'finds first gap in numbering' do
31-
Dir.mktmpdir do |dir|
32-
FileUtils.touch(File.join(dir, 'output_001.csv'))
33-
FileUtils.touch(File.join(dir, 'output_003.csv'))
34-
35-
file_no = BenchmarkRunner.free_file_no(dir)
36-
assert_equal 2, file_no
37-
end
38-
end
39-
40-
it 'handles triple digit numbers' do
41-
Dir.mktmpdir do |dir|
42-
(1..100).each do |i|
43-
FileUtils.touch(File.join(dir, 'output_%03d.csv' % i))
44-
end
45-
46-
file_no = BenchmarkRunner.free_file_no(dir)
47-
assert_equal 101, file_no
48-
end
49-
end
50-
end
51-
5212
describe '.check_call' do
5313
it 'runs a successful command and returns success status' do
5414
result = nil
@@ -158,6 +118,68 @@
158118
end
159119
end
160120

121+
describe '.output_path' do
122+
it 'returns the override path when provided' do
123+
Dir.mktmpdir do |dir|
124+
override = '/custom/path/output'
125+
result = BenchmarkRunner.output_path(dir, out_override: override)
126+
assert_equal override, result
127+
end
128+
end
129+
130+
it 'generates path with first available file number when no override' do
131+
Dir.mktmpdir do |dir|
132+
result = BenchmarkRunner.output_path(dir)
133+
expected = File.join(dir, 'output_001')
134+
assert_equal expected, result
135+
end
136+
end
137+
138+
it 'uses next available file number when files exist' do
139+
Dir.mktmpdir do |dir|
140+
FileUtils.touch(File.join(dir, 'output_001.csv'))
141+
FileUtils.touch(File.join(dir, 'output_002.csv'))
142+
143+
result = BenchmarkRunner.output_path(dir)
144+
expected = File.join(dir, 'output_003')
145+
assert_equal expected, result
146+
end
147+
end
148+
149+
it 'finds first gap in numbering when files are non-sequential' do
150+
Dir.mktmpdir do |dir|
151+
FileUtils.touch(File.join(dir, 'output_001.csv'))
152+
FileUtils.touch(File.join(dir, 'output_003.csv'))
153+
154+
result = BenchmarkRunner.output_path(dir)
155+
expected = File.join(dir, 'output_002')
156+
assert_equal expected, result
157+
end
158+
end
159+
160+
it 'prefers override even when files exist' do
161+
Dir.mktmpdir do |dir|
162+
FileUtils.touch(File.join(dir, 'output_001.csv'))
163+
164+
override = '/override/path'
165+
result = BenchmarkRunner.output_path(dir, out_override: override)
166+
assert_equal override, result
167+
end
168+
end
169+
170+
it 'handles triple digit file numbers' do
171+
Dir.mktmpdir do |dir|
172+
(1..100).each do |i|
173+
FileUtils.touch(File.join(dir, 'output_%03d.csv' % i))
174+
end
175+
176+
result = BenchmarkRunner.output_path(dir)
177+
expected = File.join(dir, 'output_101')
178+
assert_equal expected, result
179+
end
180+
end
181+
end
182+
161183
describe '.render_graph' do
162184
it 'delegates to GraphRenderer and returns calculated png_path' do
163185
Dir.mktmpdir do |dir|

test/run_benchmarks_integration_test.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@
3434
File.write(File.join(tmpdir, 'output_001.csv'), 'test')
3535
File.write(File.join(tmpdir, 'output_002.csv'), 'test')
3636

37-
# The free_file_no function should find the next number
37+
# The output_path function should find the next number
3838
require_relative '../lib/benchmark_runner'
39-
file_no = BenchmarkRunner.free_file_no(tmpdir)
40-
assert_equal 3, file_no
39+
output_path = BenchmarkRunner.output_path(tmpdir)
40+
expected_path = File.join(tmpdir, 'output_003')
41+
assert_equal expected_path, output_path
4142
end
4243
end
4344

0 commit comments

Comments
 (0)