Skip to content

Commit 97cf092

Browse files
committed
Simplify the logic in run by determining benchmark directories during initialization
1 parent 7550537 commit 97cf092

2 files changed

Lines changed: 83 additions & 28 deletions

File tree

lib/benchmark_suite.rb

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
require 'yaml'
99
require 'rbconfig'
1010
require_relative 'benchmark_filter'
11+
require_relative 'benchmark_runner'
1112

1213
# BenchmarkSuite runs a collection of benchmarks and collects their results
1314
class BenchmarkSuite
@@ -17,7 +18,7 @@ class BenchmarkSuite
1718
RACTOR_CATEGORY = "ractor"
1819
RACTOR_HARNESS = "harness-ractor"
1920

20-
attr_reader :ruby, :ruby_description, :categories, :name_filters, :out_path, :harness, :pre_init, :no_pinning
21+
attr_reader :ruby, :ruby_description, :categories, :name_filters, :out_path, :harness, :pre_init, :no_pinning, :bench_dir, :ractor_bench_dir
2122

2223
def initialize(ruby:, ruby_description:, categories:, name_filters:, out_path:, harness:, pre_init: nil, no_pinning: false)
2324
@ruby = ruby
@@ -29,6 +30,15 @@ def initialize(ruby:, ruby_description:, categories:, name_filters:, out_path:,
2930
@pre_init = pre_init ? expand_pre_init(pre_init) : nil
3031
@no_pinning = no_pinning
3132
@ractor_only = (categories == RACTOR_ONLY_CATEGORY)
33+
34+
@bench_dir = BENCHMARKS_DIR
35+
@ractor_bench_dir = RACTOR_BENCHMARKS_DIR
36+
37+
if @ractor_only
38+
@bench_dir = @ractor_bench_dir
39+
@harness = RACTOR_HARNESS
40+
@categories = []
41+
end
3242
end
3343

3444
# Run all the benchmarks and record execution times
@@ -37,33 +47,6 @@ def run
3747
bench_data = {}
3848
bench_failures = {}
3949

40-
bench_dir = BENCHMARKS_DIR
41-
ractor_bench_dir = RACTOR_BENCHMARKS_DIR
42-
43-
if @racktor_only
44-
bench_dir = ractor_bench_dir
45-
@harness = RACTOR_HARNESS
46-
@categories = []
47-
end
48-
49-
bench_file_grouping = {}
50-
51-
# Get the list of benchmark files/directories matching name filters
52-
filter = benchmark_filter(categories: categories, name_filters: name_filters)
53-
bench_file_grouping[bench_dir] = Dir.children(bench_dir).sort.filter do |entry|
54-
filter.match?(entry)
55-
end
56-
57-
if categories == [RACTOR_CATEGORY]
58-
# We ignore the category filter here because everything in the
59-
# benchmarks-ractor directory should be included when we're benchmarking the
60-
# Ractor category
61-
ractor_filter = benchmark_filter(categories: [], name_filters: name_filters)
62-
bench_file_grouping[ractor_bench_dir] = Dir.children(ractor_bench_dir).sort.filter do |entry|
63-
ractor_filter.match?(entry)
64-
end
65-
end
66-
6750
bench_file_grouping.each do |bench_dir, bench_files|
6851
bench_files.each_with_index do |entry, idx|
6952
bench_name = entry.gsub('.rb', '')
@@ -140,6 +123,30 @@ def run
140123

141124
private
142125

126+
def bench_file_grouping
127+
bench_file_grouping = {}
128+
129+
# Get the list of benchmark files/directories matching name filters
130+
filter = benchmark_filter(categories: categories, name_filters: name_filters)
131+
bench_file_grouping[bench_dir] = filtered_bench_entries(bench_dir, filter)
132+
133+
if categories == [RACTOR_CATEGORY]
134+
# We ignore the category filter here because everything in the
135+
# benchmarks-ractor directory should be included when we're benchmarking the
136+
# Ractor category
137+
ractor_filter = benchmark_filter(categories: [], name_filters: name_filters)
138+
bench_file_grouping[ractor_bench_dir] = filtered_bench_entries(ractor_bench_dir, ractor_filter)
139+
end
140+
141+
bench_file_grouping
142+
end
143+
144+
def filtered_bench_entries(dir, filter)
145+
Dir.children(dir).sort.filter do |entry|
146+
filter.match?(entry)
147+
end
148+
end
149+
143150
def benchmark_filter(categories:, name_filters:)
144151
@benchmark_filter ||= {}
145152
key = [categories, name_filters]

test/benchmark_suite_test.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,54 @@
7878

7979
assert_equal true, suite.no_pinning
8080
end
81+
82+
it 'sets bench_dir to BENCHMARKS_DIR by default' do
83+
suite = BenchmarkSuite.new(
84+
ruby: ['ruby'],
85+
ruby_description: 'ruby 3.2.0',
86+
categories: ['micro'],
87+
name_filters: [],
88+
out_path: @out_path,
89+
harness: 'harness'
90+
)
91+
92+
assert_equal 'benchmarks', suite.bench_dir
93+
assert_equal 'benchmarks-ractor', suite.ractor_bench_dir
94+
assert_equal 'harness', suite.harness
95+
assert_equal ['micro'], suite.categories
96+
end
97+
98+
it 'sets bench_dir to ractor directory and updates harness when ractor-only category is used' do
99+
suite = BenchmarkSuite.new(
100+
ruby: ['ruby'],
101+
ruby_description: 'ruby 3.2.0',
102+
categories: ['ractor-only'],
103+
name_filters: [],
104+
out_path: @out_path,
105+
harness: 'harness'
106+
)
107+
108+
assert_equal 'benchmarks-ractor', suite.bench_dir
109+
assert_equal 'benchmarks-ractor', suite.ractor_bench_dir
110+
assert_equal 'harness-ractor', suite.harness
111+
assert_equal [], suite.categories
112+
end
113+
114+
it 'keeps bench_dir as BENCHMARKS_DIR when ractor category is used' do
115+
suite = BenchmarkSuite.new(
116+
ruby: ['ruby'],
117+
ruby_description: 'ruby 3.2.0',
118+
categories: ['ractor'],
119+
name_filters: [],
120+
out_path: @out_path,
121+
harness: 'harness'
122+
)
123+
124+
assert_equal 'benchmarks', suite.bench_dir
125+
assert_equal 'benchmarks-ractor', suite.ractor_bench_dir
126+
assert_equal 'harness', suite.harness
127+
assert_equal ['ractor'], suite.categories
128+
end
81129
end
82130

83131
describe '#run' do

0 commit comments

Comments
 (0)