Skip to content

Commit dfe53c5

Browse files
authored
Reduce dependency requirements to enable running some benchmarks with miniruby (#308)
* Embed realtime method to remove dependency on benchmark * Warn and skip max rss if fiddle fails to load * Warn and skip writing file if json fails to load
1 parent 51a26df commit dfe53c5

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

harness/harness-common.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ def get_maxrss
7171
raise unless result.zero?
7272
maxrss_kb = buffer[offset, Fiddle::SIZEOF_LONG].unpack1('q')
7373
1024 * maxrss_kb
74+
rescue LoadError
75+
warn "Failed to get max RSS: #{$!.message}"
76+
nil
7477
end
7578

7679
# Do expand_path at require-time, not when returning results, before the benchmark is likely to chdir
@@ -87,22 +90,31 @@ def return_results(warmup_iterations, bench_iterations)
8790

8891
# Collect our own peak mem usage as soon as reasonable after finishing the last iteration.
8992
rss = get_rss
90-
maxrss = get_maxrss
91-
puts "RSS: %.1fMiB" % (rss / 1024.0 / 1024.0)
92-
puts "MAXRSS: %.1fMiB" % (maxrss / 1024.0 / 1024.0)
9393
yjit_bench_results["rss"] = rss
94-
yjit_bench_results["maxrss"] = maxrss
94+
puts "RSS: %.1fMiB" % (rss / 1024.0 / 1024.0)
95+
96+
if maxrss = get_maxrss
97+
puts "MAXRSS: %.1fMiB" % (maxrss / 1024.0 / 1024.0)
98+
yjit_bench_results["maxrss"] = maxrss
99+
end
95100

96101
if defined?(RubyVM::YJIT) && RubyVM::YJIT.stats_enabled?
97102
yjit_bench_results["yjit_stats"] = RubyVM::YJIT.runtime_stats
98103
end
99104

105+
write_json_file(yjit_bench_results)
106+
end
107+
108+
def write_json_file(yjit_bench_results)
100109
require "json"
110+
101111
out_path = YB_OUTPUT_FILE
102112
system('mkdir', '-p', File.dirname(out_path))
103113

104114
# Using default path? Print where we put it.
105115
puts "Writing file #{out_path}" unless ENV["RESULT_JSON_PATH"]
106116

107117
File.write(out_path, JSON.pretty_generate(yjit_bench_results))
118+
rescue LoadError
119+
warn "Failed to write JSON file: #{$!.message}"
108120
end

harness/harness.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
require 'benchmark'
21
require_relative "./harness-common"
32

43
# Warmup iterations
@@ -19,14 +18,28 @@
1918

2019
puts RUBY_DESCRIPTION
2120

21+
if defined?(Process.clock_gettime) && defined?(Process::CLOCK_MONOTONIC)
22+
def realtime
23+
r0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
24+
yield
25+
Process.clock_gettime(Process::CLOCK_MONOTONIC) - r0
26+
end
27+
else
28+
require "benchmark"
29+
30+
def realtime(&block)
31+
Benchmark.realtime(&block)
32+
end
33+
end
34+
2235
# Takes a block as input
23-
def run_benchmark(_num_itrs_hint)
36+
def run_benchmark(_num_itrs_hint, &block)
2437
times = []
2538
total_time = 0
2639
num_itrs = 0
2740

2841
begin
29-
time = Benchmark.realtime { yield }
42+
time = realtime(&block)
3043
num_itrs += 1
3144

3245
# NOTE: we may want to avoid this as it could trigger GC?

0 commit comments

Comments
 (0)