|
| 1 | +# frozen_string_literal: true |
| 2 | +require_relative '../harness/harness-common' |
| 3 | + |
| 4 | +Warning[:experimental] = false |
| 5 | +ENV["YJIT_BENCH_RACTOR_HARNESS"] = "1" |
| 6 | + |
| 7 | +default_ractors = [ |
| 8 | + 0, # without ractor |
| 9 | + 1, 2, 4, 6, 8#, 12, 16, 32 |
| 10 | +] |
| 11 | +if rs = ENV["YJIT_BENCH_RACTORS"] |
| 12 | + rs = rs.split(",").map(&:to_i) # If you want to include 0, you have to specify |
| 13 | + rs = rs.sort.uniq |
| 14 | + if rs.any? |
| 15 | + ractors = rs |
| 16 | + end |
| 17 | +end |
| 18 | +RACTORS = (ractors || default_ractors).freeze |
| 19 | + |
| 20 | +unless Ractor.method_defined?(:join) |
| 21 | + class Ractor |
| 22 | + def join |
| 23 | + take |
| 24 | + self |
| 25 | + end |
| 26 | + alias value take |
| 27 | + end |
| 28 | +end |
| 29 | + |
| 30 | +def use_ractor_gemfile(filename) |
| 31 | + filename = File.expand_path("Gemfile_#{filename}.rb", "benchmarks/ractor/gemfiles") |
| 32 | + raise "Gemfile #{filename} doesn't exist" unless File.exist?(filename) |
| 33 | + use_inline_gemfile do |
| 34 | + gem "fiddle" # for maxrss |
| 35 | + instance_eval File.read(filename), filename, 1 |
| 36 | + end |
| 37 | +end |
| 38 | + |
| 39 | +MAX_ITERS = Integer(ENV.fetch("MAX_BENCH_ITRS", 5)) |
| 40 | + |
| 41 | +def run_benchmark(num_itrs_hint, ractor_args: [], &block) |
| 42 | + warmup_itrs = Integer(ENV.fetch('WARMUP_ITRS', 5)) |
| 43 | + bench_itrs = Integer(ENV.fetch('MIN_BENCH_ITRS', num_itrs_hint)) |
| 44 | + if bench_itrs > MAX_ITERS |
| 45 | + bench_itrs = MAX_ITERS |
| 46 | + end |
| 47 | + # { num_ractors => [itr_in_ms, ...] } |
| 48 | + stats = Hash.new { |h,k| h[k] = [] } |
| 49 | + |
| 50 | + header = "r: itr: time" |
| 51 | + puts header |
| 52 | + |
| 53 | + i = 0 |
| 54 | + while i < warmup_itrs |
| 55 | + args = if ractor_args.empty? |
| 56 | + [] |
| 57 | + else |
| 58 | + ractor_deep_dup(ractor_args) |
| 59 | + end |
| 60 | + block.call *([0] + args) |
| 61 | + i += 1 |
| 62 | + end |
| 63 | + |
| 64 | + blk = Ractor.make_shareable(block) |
| 65 | + RACTORS.each do |rs| |
| 66 | + num_itrs = 0 |
| 67 | + while num_itrs < bench_itrs |
| 68 | + before = Process.clock_gettime(Process::CLOCK_MONOTONIC) |
| 69 | + if rs.zero? |
| 70 | + block.call *([rs] + ractor_deep_dup(ractor_args)) |
| 71 | + else |
| 72 | + rs_list = [] |
| 73 | + rs.times do |
| 74 | + rs_list << Ractor.new(*([rs] + ractor_args), &block) # ractor_args are copied |
| 75 | + end |
| 76 | + while rs_list.any? |
| 77 | + r, _obj = Ractor.select(*rs_list) |
| 78 | + rs_list.delete(r) |
| 79 | + end |
| 80 | + end |
| 81 | + num_itrs += 1 |
| 82 | + time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - before |
| 83 | + time_ms = (1000 * time).to_i |
| 84 | + itr_str = "%-3s %4s %6s" % ["#{rs}", "##{num_itrs}:", "#{time_ms}ms"] |
| 85 | + stats[rs] << time_ms |
| 86 | + puts itr_str |
| 87 | + end |
| 88 | + end |
| 89 | + return_results([], stats.values.flatten) |
| 90 | +end |
| 91 | + |
| 92 | +# NOTE: we use `ractor_deep_dup` instead of `Ractor.make_shareable(copy: true)` for the case of |
| 93 | +# sending args to the block without a ractor because the arguments passed to `run_benchmark` are |
| 94 | +# sometimes modified, and we want to allow that because it improves compatibility. We don't want |
| 95 | +# it to be deeply frozen. |
| 96 | +def ractor_deep_dup(args) |
| 97 | + if Array === args |
| 98 | + ret = [] |
| 99 | + args.each do |el| |
| 100 | + ret << ractor_deep_dup(el) |
| 101 | + end |
| 102 | + ret |
| 103 | + elsif Hash === args |
| 104 | + ret = {} |
| 105 | + args.each do |k,v| |
| 106 | + ret[ractor_deep_dup(k)] = ractor_deep_dup(v) |
| 107 | + end |
| 108 | + ret |
| 109 | + else |
| 110 | + args.dup |
| 111 | + end |
| 112 | +end |
| 113 | + |
| 114 | +Ractor.make_shareable(self) # until we get Ractor.shareable_proc |
0 commit comments