1111require 'etc'
1212require 'yaml'
1313require_relative 'misc/stats'
14+ require_relative 'lib/benchmark_runner'
1415
1516# Check which OS we are running
1617def os
17- @os ||= (
18- host_os = RbConfig ::CONFIG [ 'host_os' ]
19- case host_os
20- when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
21- :windows
22- when /darwin|mac os/
23- :macosx
24- when /linux/
25- :linux
26- when /solaris|bsd/
27- :unix
28- else
29- raise "unknown os: #{ host_os . inspect } "
30- end
31- )
18+ BenchmarkRunner . os
3219end
3320
3421# Checked system - error or return info if the command fails
@@ -123,38 +110,7 @@ def performance_governor?
123110end
124111
125112def table_to_str ( table_data , format , failures )
126- # Trim numbers to one decimal for console display
127- # Keep two decimals for the speedup ratios
128-
129- failure_rows = failures . map { |_exe , data | data . keys } . flatten . uniq
130- . map { |name | [ name ] + ( [ 'N/A' ] * ( table_data . first . size - 1 ) ) }
131-
132- table_data = table_data . first ( 1 ) + failure_rows + table_data . drop ( 1 ) . map { |row |
133- format . zip ( row ) . map { |fmt , data | fmt % data }
134- }
135-
136- num_rows = table_data . length
137- num_cols = table_data [ 0 ] . length
138-
139- # Pad each column to the maximum width in the column
140- ( 0 ...num_cols ) . each do |c |
141- cell_lens = ( 0 ...num_rows ) . map { |r | table_data [ r ] [ c ] . length }
142- max_width = cell_lens . max
143- ( 0 ...num_rows ) . each { |r | table_data [ r ] [ c ] = table_data [ r ] [ c ] . ljust ( max_width ) }
144- end
145-
146- # Row of separator dashes
147- sep_row = ( 0 ...num_cols ) . map { |i | '-' * table_data [ 0 ] [ i ] . length } . join ( ' ' )
148-
149- out = sep_row + "\n "
150-
151- table_data . each do |row |
152- out += row . join ( ' ' ) + "\n "
153- end
154-
155- out += sep_row
156-
157- return out
113+ BenchmarkRunner . table_to_str ( table_data , format , failures )
158114end
159115
160116def mean ( values )
@@ -165,92 +121,21 @@ def stddev(values)
165121 Stats . new ( values ) . stddev
166122end
167123
168- def free_file_no ( prefix )
169- ( 1 ..) . each do |file_no |
170- out_path = File . join ( prefix , "output_%03d.csv" % file_no )
171- if !File . exist? ( out_path )
172- return file_no
173- end
174- end
175- end
176-
177- def benchmark_categories ( name )
178- metadata = benchmarks_metadata . find { |benchmark , _metadata | benchmark == name } &.last || { }
179- categories = [ metadata . fetch ( 'category' , 'other' ) ]
180- categories << 'ractor' if metadata [ 'ractor' ]
181- categories
182- end
183-
184124# Check if the name matches any of the names in a list of filters
185125def match_filter ( entry , categories :, name_filters :)
186- name_filters = process_name_filters ( name_filters )
187- name = entry . sub ( /\. rb\z / , '' )
188- ( categories . empty? || benchmark_categories ( name ) . any? { |cat | categories . include? ( cat ) } ) &&
189- ( name_filters . empty? || name_filters . any? { |filter | filter === name } )
190- end
191-
192- # process "/my_benchmark/i" into /my_benchmark/i
193- def process_name_filters ( name_filters )
194- name_filters . map do |name_filter |
195- if name_filter [ 0 ] == "/"
196- regexp_str = name_filter [ 1 ..-1 ] . reverse . sub ( /\A (\w *)\/ / , "" )
197- regexp_opts = $1. to_s
198- regexp_str . reverse!
199- r = /#{ regexp_str } /
200- if !regexp_opts . empty?
201- r = Regexp . compile ( r . to_s , regexp_opts )
202- end
203- r
204- else
205- name_filter
206- end
207- end
208- end
209-
210- # Resolve the pre_init file path into a form that can be required
211- def expand_pre_init ( path )
212- path = Pathname . new ( path )
213-
214- unless path . exist?
215- puts "--with-pre-init called with non-existent file!"
216- exit ( -1 )
217- end
218-
219- if path . directory?
220- puts "--with-pre-init called with a directory, please pass a .rb file"
221- exit ( -1 )
222- end
223-
224- library_name = path . basename ( path . extname )
225- load_path = path . parent . expand_path
226-
227- [
228- "-I" , load_path ,
229- "-r" , library_name
230- ]
126+ BenchmarkRunner . match_filter ( entry , categories : categories , name_filters : name_filters , metadata : benchmarks_metadata )
231127end
232128
233129def benchmarks_metadata
234130 @benchmarks_metadata ||= YAML . load_file ( 'benchmarks.yml' )
235131end
236132
237133def sort_benchmarks ( bench_names )
238- headline_benchmarks = benchmarks_metadata . select { |_ , metadata | metadata [ 'category' ] == 'headline' } . keys
239- micro_benchmarks = benchmarks_metadata . select { |_ , metadata | metadata [ 'category' ] == 'micro' } . keys
240-
241- headline_names , bench_names = bench_names . partition { |name | headline_benchmarks . include? ( name ) }
242- micro_names , other_names = bench_names . partition { |name | micro_benchmarks . include? ( name ) }
243- headline_names . sort + other_names . sort + micro_names . sort
134+ BenchmarkRunner . sort_benchmarks ( bench_names , benchmarks_metadata )
244135end
245136
246137def setarch_prefix
247- # Disable address space randomization (for determinism)
248- prefix = [ "setarch" , `uname -m` . strip , "-R" ]
249-
250- # Abort if we don't have permission (perhaps in a docker container).
251- return [ ] unless system ( *prefix , "true" )
252-
253- prefix
138+ BenchmarkRunner . setarch_prefix
254139end
255140
256141# Run all the benchmarks and record execution times
@@ -284,7 +169,7 @@ def run_benchmarks(ruby:, ruby_description:, categories:, name_filters:, out_pat
284169 end
285170
286171 if pre_init
287- pre_init = expand_pre_init ( pre_init )
172+ pre_init = BenchmarkRunner . expand_pre_init ( pre_init )
288173 end
289174
290175
@@ -603,7 +488,7 @@ def run_benchmarks(ruby:, ruby_description:, categories:, name_filters:, out_pat
603488 output_path = args . out_override
604489else
605490 # If no out path is specified, find a free file index for the output files
606- file_no = free_file_no ( args . out_path )
491+ file_no = BenchmarkRunner . free_file_no ( args . out_path )
607492 output_path = File . join ( args . out_path , "output_%03d" % file_no )
608493end
609494
0 commit comments