|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'csv' |
| 4 | +require 'json' |
| 5 | +require 'rbconfig' |
| 6 | + |
| 7 | +# Extracted helper methods from run_benchmarks.rb for testing |
| 8 | +module BenchmarkRunner |
| 9 | + module_function |
| 10 | + |
| 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) |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + # Get benchmark categories from metadata |
| 20 | + def benchmark_categories(name, metadata) |
| 21 | + benchmark_metadata = metadata[name] || {} |
| 22 | + categories = [benchmark_metadata.fetch('category', 'other')] |
| 23 | + categories << 'ractor' if benchmark_metadata['ractor'] |
| 24 | + categories |
| 25 | + end |
| 26 | + |
| 27 | + # Check if the name matches any of the names in a list of filters |
| 28 | + def match_filter(entry, categories:, name_filters:, metadata:) |
| 29 | + name_filters = process_name_filters(name_filters) |
| 30 | + name = entry.sub(/\.rb\z/, '') |
| 31 | + (categories.empty? || benchmark_categories(name, metadata).any? { |cat| categories.include?(cat) }) && |
| 32 | + (name_filters.empty? || name_filters.any? { |filter| filter === name }) |
| 33 | + end |
| 34 | + |
| 35 | + # Process "/my_benchmark/i" into /my_benchmark/i |
| 36 | + def process_name_filters(name_filters) |
| 37 | + name_filters.map do |name_filter| |
| 38 | + if name_filter[0] == "/" |
| 39 | + regexp_str = name_filter[1..-1].reverse.sub(/\A(\w*)\//, "") |
| 40 | + regexp_opts = ::Regexp.last_match(1).to_s |
| 41 | + regexp_str.reverse! |
| 42 | + r = /#{regexp_str}/ |
| 43 | + if !regexp_opts.empty? |
| 44 | + # Convert option string to Regexp option flags |
| 45 | + flags = 0 |
| 46 | + flags |= Regexp::IGNORECASE if regexp_opts.include?('i') |
| 47 | + flags |= Regexp::MULTILINE if regexp_opts.include?('m') |
| 48 | + flags |= Regexp::EXTENDED if regexp_opts.include?('x') |
| 49 | + r = Regexp.new(regexp_str, flags) |
| 50 | + end |
| 51 | + r |
| 52 | + else |
| 53 | + name_filter |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + # Resolve the pre_init file path into a form that can be required |
| 59 | + def expand_pre_init(path) |
| 60 | + require 'pathname' |
| 61 | + |
| 62 | + path = Pathname.new(path) |
| 63 | + |
| 64 | + unless path.exist? |
| 65 | + puts "--with-pre-init called with non-existent file!" |
| 66 | + exit(-1) |
| 67 | + end |
| 68 | + |
| 69 | + if path.directory? |
| 70 | + puts "--with-pre-init called with a directory, please pass a .rb file" |
| 71 | + exit(-1) |
| 72 | + end |
| 73 | + |
| 74 | + library_name = path.basename(path.extname) |
| 75 | + load_path = path.parent.expand_path |
| 76 | + |
| 77 | + [ |
| 78 | + "-I", load_path, |
| 79 | + "-r", library_name |
| 80 | + ] |
| 81 | + end |
| 82 | + |
| 83 | + # Sort benchmarks with headlines first, then others, then micro |
| 84 | + def sort_benchmarks(bench_names, metadata) |
| 85 | + headline_benchmarks = metadata.select { |_, meta| meta['category'] == 'headline' }.keys |
| 86 | + micro_benchmarks = metadata.select { |_, meta| meta['category'] == 'micro' }.keys |
| 87 | + |
| 88 | + headline_names, bench_names = bench_names.partition { |name| headline_benchmarks.include?(name) } |
| 89 | + micro_names, other_names = bench_names.partition { |name| micro_benchmarks.include?(name) } |
| 90 | + headline_names.sort + other_names.sort + micro_names.sort |
| 91 | + end |
| 92 | + |
| 93 | + # Check which OS we are running |
| 94 | + def os |
| 95 | + @os ||= ( |
| 96 | + host_os = RbConfig::CONFIG['host_os'] |
| 97 | + case host_os |
| 98 | + when /mswin|msys|mingw|cygwin|bccwin|wince|emc/ |
| 99 | + :windows |
| 100 | + when /darwin|mac os/ |
| 101 | + :macosx |
| 102 | + when /linux/ |
| 103 | + :linux |
| 104 | + when /solaris|bsd/ |
| 105 | + :unix |
| 106 | + else |
| 107 | + raise "unknown os: #{host_os.inspect}" |
| 108 | + end |
| 109 | + ) |
| 110 | + end |
| 111 | + |
| 112 | + # Generate setarch prefix for Linux |
| 113 | + def setarch_prefix |
| 114 | + # Disable address space randomization (for determinism) |
| 115 | + prefix = ["setarch", `uname -m`.strip, "-R"] |
| 116 | + |
| 117 | + # Abort if we don't have permission (perhaps in a docker container). |
| 118 | + return [] unless system(*prefix, "true", out: File::NULL, err: File::NULL) |
| 119 | + |
| 120 | + prefix |
| 121 | + end |
| 122 | +end |
0 commit comments