From 37134816159462386b8276cf74cb6212530e4047 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Fri, 6 Jan 2023 14:27:12 -0800 Subject: [PATCH] Count Ruby method calls as well in stats harness --- README.md | 2 +- {harness-cstats => harness-stats}/harness.rb | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) rename {harness-cstats => harness-stats}/harness.rb (76%) diff --git a/README.md b/README.md index b446d9b2..23c51693 100644 --- a/README.md +++ b/README.md @@ -146,7 +146,7 @@ You can find several test harnesses in this repository: * harness-perf - a simplified harness that runs for exactly the hinted number of iterations * harness-bips - a harness that measures iterations/second until stable * harness-continuous - a harness that adjusts the batch sizes of iterations to run in stable iteration size batches -* harness-cstats - count C method calls and C loop iterations +* harness-stats - count method calls and loop iterations To use it, run a benchmark script directly, specifying a harness directory with `-I`: diff --git a/harness-cstats/harness.rb b/harness-stats/harness.rb similarity index 76% rename from harness-cstats/harness.rb rename to harness-stats/harness.rb index f1a3b65b..cc0a3bdd 100644 --- a/harness-cstats/harness.rb +++ b/harness-stats/harness.rb @@ -7,6 +7,7 @@ def run_benchmark(*) frames = [] c_calls = Hash.new { 0 } c_loops = Hash.new { 0 } + rb_calls = Hash.new { 0 } method_trace = TracePoint.new(:call, :c_call, :return, :c_return) do |tp| # Keep track of call frames to get the caller of :b_call @@ -18,9 +19,12 @@ def run_benchmark(*) frames.pop end - # Count C method calls - if tp.event == :c_call + # Count method calls + case tp.event + when :c_call c_calls[method_name] += 1 + when :call + rb_calls[method_name] += 1 end end @@ -56,5 +60,14 @@ def run_benchmark(*) c_calls.sort_by(&:last).reverse.first(100).each do |method, count| puts '%8d (%4.1f%%) %s' % [count, 100.0 * count / c_calls_total, method] end + puts + + rb_calls_total = rb_calls.sum(&:last) + rb_calls = rb_calls.sort_by { |_method, count| -count }.first(100) + rb_calls_ratio = 100.0 * rb_calls.sum(&:last) / rb_calls_total + puts "Top #{rb_calls.size} Ruby method calls (#{'%.1f' % rb_calls_ratio}% of all #{rb_calls_total} calls):" + rb_calls.sort_by(&:last).reverse.first(100).each do |method, count| + puts '%8d (%4.1f%%) %s' % [count, 100.0 * count / rb_calls_total, method] + end end }