-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathbenchmark_runner_cli_test.rb
More file actions
374 lines (308 loc) · 11.5 KB
/
benchmark_runner_cli_test.rb
File metadata and controls
374 lines (308 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
require_relative 'test_helper'
require_relative '../lib/benchmark_runner/cli'
require_relative '../lib/argument_parser'
require 'tmpdir'
require 'fileutils'
require 'json'
require 'csv'
describe BenchmarkRunner::CLI do
before do
@original_env = {}
['WARMUP_ITRS', 'MIN_BENCH_ITRS', 'MIN_BENCH_TIME', 'BENCHMARK_QUIET'].each do |key|
@original_env[key] = ENV[key]
end
# Set fast iteration counts for tests
ENV['WARMUP_ITRS'] = '0'
ENV['MIN_BENCH_ITRS'] = '1'
ENV['MIN_BENCH_TIME'] = '0'
# Suppress benchmark output during tests
ENV['BENCHMARK_QUIET'] = '1'
end
after do
@original_env.each do |key, value|
if value.nil?
ENV.delete(key)
else
ENV[key] = value
end
end
end
# Helper method to create args directly without parsing
def create_args(overrides = {})
if RUBY_ENGINE == 'truffleruby'
executables = { 'truffleruby' => [RbConfig.ruby] }
else
executables = { 'interp' => [RbConfig.ruby], 'yjit' => [RbConfig.ruby, '--yjit'] }
end
defaults = {
executables: executables,
out_path: nil,
out_override: nil,
harness: 'harness',
yjit_opts: '',
categories: [],
name_filters: [],
excludes: [],
rss: false,
interleave: false,
graph: false,
no_pinning: true,
turbo: true,
skip_yjit: false,
with_pre_init: nil
}
ArgumentParser::Args.new(**defaults.merge(overrides))
end
describe '.run class method' do
it 'parses ARGV and runs the CLI end-to-end' do
Dir.mktmpdir do |tmpdir|
# Test the full integration: argv array -> parse -> initialize -> run
output = capture_io do
BenchmarkRunner::CLI.run([
'--name_filters=fib',
'--out_path=' + tmpdir,
'--once',
'--no-pinning',
'--turbo'
])
end.join
# Verify output contains expected information
assert_match(/fib/, output, "Output should mention the fib benchmark")
assert_match(/Total time spent benchmarking:/, output)
assert_match(/Output:/, output)
# Verify output files were created
json_files = Dir.glob(File.join(tmpdir, "output_*.json"))
assert_equal 1, json_files.size, "Should create exactly one JSON output file"
end
end
end
describe '#run integration test' do
it 'runs a simple benchmark end-to-end and produces all output files' do
Dir.mktmpdir do |tmpdir|
args = create_args(
name_filters: ['fib'],
out_path: tmpdir
)
# Run the CLI
cli = BenchmarkRunner::CLI.new(args)
# Capture output and run - should not raise errors
output = capture_io do
cli.run
end.join
# Verify output contains expected information
assert_match(/fib/, output, "Output should mention the fib benchmark")
assert_match(/Total time spent benchmarking:/, output)
assert_match(/Output:/, output)
# Verify JSON output file was created
json_files = Dir.glob(File.join(tmpdir, "output_*.json"))
assert_equal 1, json_files.size, "Should create exactly one JSON output file"
json_path = json_files.first
assert File.exist?(json_path), "JSON file should exist"
# Verify JSON content is valid and contains expected data
json_data = JSON.parse(File.read(json_path))
assert json_data.key?('metadata'), "JSON should contain metadata"
assert json_data.key?('raw_data'), "JSON should contain raw_data"
# Verify CSV output file was created
csv_path = json_path.sub('.json', '.csv')
assert File.exist?(csv_path), "CSV file should exist"
# Verify CSV content
csv_data = CSV.read(csv_path)
assert csv_data.size > 0, "CSV should have content"
# Verify TXT output file was created
txt_path = json_path.sub('.json', '.txt')
assert File.exist?(txt_path), "TXT file should exist"
# Verify TXT content
txt_content = File.read(txt_path)
assert_match(/fib/, txt_content, "TXT should contain benchmark results")
end
end
it 'handles multiple benchmarks with name filters' do
Dir.mktmpdir do |tmpdir|
args = create_args(
name_filters: ['fib', 'respond_to'],
out_path: tmpdir
)
cli = BenchmarkRunner::CLI.new(args)
output = capture_io { cli.run }.join
# Check both benchmarks ran
assert_match(/fib/, output)
assert_match(/respond_to/, output)
# Check output files were created
json_files = Dir.glob(File.join(tmpdir, "*.json"))
assert_equal 1, json_files.size
json_data = JSON.parse(File.read(json_files.first))
raw_data = json_data['raw_data']
# Verify data contains results for both benchmarks
assert raw_data.values.any? { |data| data.key?('fib') }
assert raw_data.values.any? { |data| data.key?('respond_to') }
end
end
it 'respects output path override' do
Dir.mktmpdir do |tmpdir|
custom_name = File.join(tmpdir, 'custom_output')
args = create_args(
name_filters: ['fib'],
out_path: tmpdir,
out_override: custom_name
)
cli = BenchmarkRunner::CLI.new(args)
capture_io { cli.run }
# Check that custom-named files were created
assert File.exist?(custom_name + '.json'), "Custom JSON file should exist"
assert File.exist?(custom_name + '.csv'), "Custom CSV file should exist"
assert File.exist?(custom_name + '.txt'), "Custom TXT file should exist"
end
end
it 'compares different ruby executables' do
skip "Requires actual ruby installations" unless ENV['RUN_INTEGRATION_TESTS']
Dir.mktmpdir do |tmpdir|
ruby_path = RbConfig.ruby
args = create_args(
executables: { 'test1' => [ruby_path], 'test2' => [ruby_path] },
name_filters: ['fib'],
out_path: tmpdir
)
cli = BenchmarkRunner::CLI.new(args)
output = capture_io { cli.run }.join
# Should show comparison between two executables
assert_match(/test1/, output)
assert_match(/test2/, output)
json_files = Dir.glob(File.join(tmpdir, "*.json"))
json_data = JSON.parse(File.read(json_files.first))
# Both executables should be in metadata
assert json_data['metadata'].key?('test1')
assert json_data['metadata'].key?('test2')
# Both should have raw data
assert json_data['raw_data'].key?('test1')
assert json_data['raw_data'].key?('test2')
end
end
it 'handles benchmark with category filter' do
Dir.mktmpdir do |tmpdir|
args = create_args(
categories: ['micro'],
name_filters: ['fib'],
out_path: tmpdir
)
cli = BenchmarkRunner::CLI.new(args)
output = capture_io { cli.run }.join
# Should run successfully
assert_match(/Total time spent benchmarking:/, output)
# Output files should exist
json_files = Dir.glob(File.join(tmpdir, "*.json"))
assert_equal 1, json_files.size
end
end
it 'creates sequential output files when no override specified' do
Dir.mktmpdir do |tmpdir|
# Run first benchmark
args1 = create_args(
name_filters: ['fib'],
out_path: tmpdir
)
cli1 = BenchmarkRunner::CLI.new(args1)
capture_io { cli1.run }
# Run second benchmark
args2 = create_args(
name_filters: ['respond_to'],
out_path: tmpdir
)
cli2 = BenchmarkRunner::CLI.new(args2)
capture_io { cli2.run }
# Should have two sets of output files
json_files = Dir.glob(File.join(tmpdir, "output_*.json")).sort
assert_equal 2, json_files.size
assert_match(/output_001\.json$/, json_files[0])
assert_match(/output_002\.json$/, json_files[1])
end
end
it 'includes RSS data when --rss flag is set' do
Dir.mktmpdir do |tmpdir|
args = create_args(
name_filters: ['fib'],
out_path: tmpdir,
rss: true
)
cli = BenchmarkRunner::CLI.new(args)
capture_io { cli.run }
# Output should reference RSS
txt_files = Dir.glob(File.join(tmpdir, "*.txt"))
txt_content = File.read(txt_files.first)
assert_match(/RSS/, txt_content, "Output should include RSS information")
end
end
it 'handles no matching benchmarks gracefully' do
Dir.mktmpdir do |tmpdir|
args = create_args(
name_filters: ['nonexistent_benchmark_xyz'],
out_path: tmpdir
)
cli = BenchmarkRunner::CLI.new(args)
# Should run without error but produce empty results
capture_io { cli.run }
# Should still create output files
json_files = Dir.glob(File.join(tmpdir, "*.json"))
assert_equal 1, json_files.size
end
end
it 'can be instantiated and have args accessed' do
args = create_args(name_filters: ['fib'])
cli = BenchmarkRunner::CLI.new(args)
assert_equal args, cli.args
assert_equal ['fib'], cli.args.name_filters
end
it 'prints benchmark timing information' do
Dir.mktmpdir do |tmpdir|
args = create_args(
name_filters: ['fib'],
out_path: tmpdir
)
cli = BenchmarkRunner::CLI.new(args)
output = capture_io { cli.run }.join
# Should show timing
assert_match(/Total time spent benchmarking: \d+s/, output)
end
end
it 'runs benchmarks interleaved when --interleave is set' do
Dir.mktmpdir do |tmpdir|
args = create_args(
name_filters: ['fib', 'respond_to'],
out_path: tmpdir,
interleave: true
)
cli = BenchmarkRunner::CLI.new(args)
output = capture_io { cli.run }.join
# Progress output should include executable names in brackets
assert_match(/\[.+\]/, output, "Interleaved output should include executable name in brackets")
assert_match(/Total time spent benchmarking:/, output)
# Verify output files were created with data from all executables
json_files = Dir.glob(File.join(tmpdir, "*.json"))
assert_equal 1, json_files.size
json_data = JSON.parse(File.read(json_files.first))
raw_data = json_data['raw_data']
# All executables should have results
args.executables.each_key do |name|
assert raw_data.key?(name), "Expected raw_data to contain '#{name}'"
assert raw_data[name].key?('fib'), "Expected '#{name}' to have 'fib' results"
assert raw_data[name].key?('respond_to'), "Expected '#{name}' to have 'respond_to' results"
end
end
end
it 'creates output directory if it does not exist' do
Dir.mktmpdir do |parent_tmpdir|
nested_dir = File.join(parent_tmpdir, 'nested', 'output', 'dir')
refute Dir.exist?(nested_dir), "Directory should not exist yet"
args = create_args(
name_filters: ['fib'],
out_path: nested_dir
)
cli = BenchmarkRunner::CLI.new(args)
capture_io { cli.run }
assert Dir.exist?(nested_dir), "Directory should be created"
# Verify files were created in the new directory
json_files = Dir.glob(File.join(nested_dir, "*.json"))
assert_equal 1, json_files.size
end
end
end
end