Skip to content

Commit a2a9703

Browse files
committed
Move the check_call method to BenchmarkRunner module
And add tests to it.
1 parent 15ad5ec commit a2a9703

3 files changed

Lines changed: 100 additions & 24 deletions

File tree

lib/benchmark_runner.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,21 @@ def setarch_prefix
8080

8181
prefix
8282
end
83+
84+
# Checked system - error or return info if the command fails
85+
def check_call(command, env: {}, raise_error: true, quiet: false)
86+
puts("+ #{command}") unless quiet
87+
88+
result = {}
89+
90+
result[:success] = system(env, command)
91+
result[:status] = $?
92+
93+
unless result[:success]
94+
puts "Command #{command.inspect} failed with exit code #{result[:status].exitstatus} in directory #{Dir.pwd}"
95+
raise RuntimeError.new if raise_error
96+
end
97+
98+
result
99+
end
83100
end

run_benchmarks.rb

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,6 @@
1515
require_relative 'lib/table_formatter'
1616
require_relative 'lib/benchmark_filter'
1717

18-
# Checked system - error or return info if the command fails
19-
def check_call(command, env: {}, raise_error: true, quiet: false)
20-
puts("+ #{command}") unless quiet
21-
22-
result = {}
23-
24-
result[:success] = system(env, command)
25-
result[:status] = $?
26-
27-
unless result[:success]
28-
puts "Command #{command.inspect} failed with exit code #{result[:status].exitstatus} in directory #{Dir.pwd}"
29-
raise RuntimeError.new if raise_error
30-
end
31-
32-
result
33-
end
34-
3518
def check_output(*command)
3619
IO.popen(*command, &:read)
3720
end
@@ -46,17 +29,17 @@ def set_bench_config(turbo:)
4629
# sudo requires the flag '-S' in order to take input from stdin
4730
if File.exist?('/sys/devices/system/cpu/intel_pstate') # Intel
4831
unless intel_no_turbo? || turbo
49-
check_call("sudo -S sh -c 'echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo'")
50-
at_exit { check_call("sudo -S sh -c 'echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo'", quiet: true) }
32+
BenchmarkRunner.check_call("sudo -S sh -c 'echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo'")
33+
at_exit { BenchmarkRunner.check_call("sudo -S sh -c 'echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo'", quiet: true) }
5134
end
5235
# Disabling Turbo Boost reduces the CPU frequency, so this should be run after that.
53-
check_call("sudo -S sh -c 'echo 100 > /sys/devices/system/cpu/intel_pstate/min_perf_pct'") unless intel_perf_100pct?
36+
BenchmarkRunner.check_call("sudo -S sh -c 'echo 100 > /sys/devices/system/cpu/intel_pstate/min_perf_pct'") unless intel_perf_100pct?
5437
elsif File.exist?('/sys/devices/system/cpu/cpufreq/boost') # AMD
5538
unless amd_no_boost? || turbo
56-
check_call("sudo -S sh -c 'echo 0 > /sys/devices/system/cpu/cpufreq/boost'")
57-
at_exit { check_call("sudo -S sh -c 'echo 1 > /sys/devices/system/cpu/cpufreq/boost'", quiet: true) }
39+
BenchmarkRunner.check_call("sudo -S sh -c 'echo 0 > /sys/devices/system/cpu/cpufreq/boost'")
40+
at_exit { BenchmarkRunner.check_call("sudo -S sh -c 'echo 1 > /sys/devices/system/cpu/cpufreq/boost'", quiet: true) }
5841
end
59-
check_call("sudo -S cpupower frequency-set -g performance") unless performance_governor?
42+
BenchmarkRunner.check_call("sudo -S cpupower frequency-set -g performance") unless performance_governor?
6043
end
6144
end
6245

@@ -230,7 +213,7 @@ def run_benchmarks(ruby:, ruby_description:, categories:, name_filters:, out_pat
230213
end
231214

232215
# Do the benchmarking
233-
result = check_call(cmd.shelljoin, env: env, raise_error: false)
216+
result = BenchmarkRunner.check_call(cmd.shelljoin, env: env, raise_error: false)
234217

235218
if result[:success]
236219
bench_data[bench_name] = JSON.parse(File.read(result_json_path)).tap do |json|

test/benchmark_runner_test.rb

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,82 @@
170170
end
171171
end
172172

173+
describe '.check_call' do
174+
it 'runs a successful command and returns success status' do
175+
result = nil
176+
177+
capture_io do
178+
result = BenchmarkRunner.check_call('true')
179+
end
180+
181+
assert_equal true, result[:success]
182+
assert_equal 0, result[:status].exitstatus
183+
end
184+
185+
it 'prints the command by default' do
186+
output = capture_io do
187+
BenchmarkRunner.check_call('true')
188+
end
189+
190+
assert_includes output[0], '+ true'
191+
end
192+
193+
it 'suppresses output when quiet is true' do
194+
output = capture_io do
195+
BenchmarkRunner.check_call('true', quiet: true)
196+
end
197+
198+
assert_equal '', output[0]
199+
end
200+
201+
it 'raises error by default when command fails' do
202+
output = capture_io do
203+
assert_raises(RuntimeError) do
204+
BenchmarkRunner.check_call('false')
205+
end
206+
end
207+
208+
assert_includes output[0], 'Command "false" failed'
209+
end
210+
211+
it 'does not raise error when raise_error is false' do
212+
output = capture_io do
213+
result = BenchmarkRunner.check_call('false', raise_error: false)
214+
215+
assert_equal false, result[:success]
216+
assert_equal 1, result[:status].exitstatus
217+
end
218+
219+
assert_includes output[0], 'Command "false" failed'
220+
end
221+
222+
it 'passes environment variables to the command' do
223+
Dir.mktmpdir do |dir|
224+
output_file = File.join(dir, 'test_output.txt')
225+
output = capture_io do
226+
result = BenchmarkRunner.check_call("sh -c 'echo $TEST_VAR > #{output_file}'", env: { 'TEST_VAR' => 'hello' })
227+
assert_equal true, result[:success]
228+
end
229+
230+
# Command should be printed
231+
assert_includes output[0], "+ sh -c 'echo $TEST_VAR"
232+
# Environment variable should be written to file
233+
assert_equal "hello\n", File.read(output_file)
234+
end
235+
end
236+
237+
it 'includes exit code and directory in error message' do
238+
output = capture_io do
239+
result = BenchmarkRunner.check_call('sh -c "exit 42"', raise_error: false)
240+
assert_equal false, result[:success]
241+
assert_equal 42, result[:status].exitstatus
242+
end
243+
244+
assert_includes output[0], 'exit code 42'
245+
assert_includes output[0], "directory #{Dir.pwd}"
246+
end
247+
end
248+
173249
describe '.setarch_prefix' do
174250
it 'returns an array' do
175251
result = BenchmarkRunner.setarch_prefix

0 commit comments

Comments
 (0)