From ad997093a31baedd299476b4f23bcf3d96fa8b29 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Wed, 2 Nov 2022 17:28:37 -0700 Subject: [PATCH] Collect C method stats on railsbench --- benchmarks/railsbench/benchmark.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/benchmarks/railsbench/benchmark.rb b/benchmarks/railsbench/benchmark.rb index 4ccbd159..b61b4531 100644 --- a/benchmarks/railsbench/benchmark.rb +++ b/benchmarks/railsbench/benchmark.rb @@ -21,6 +21,21 @@ rng = Random.new(0x1be52551fc152997) visiting_routes = Array.new(visit_count) { possible_routes.sample(random: rng) } +c_calls = Hash.new { 0 } +c_loops = Hash.new { 0 } + +TracePoint.new(:c_call) do |tp| + method_name = "#{tp.defined_class}##{tp.method_id}" + c_calls[method_name] += 1 + + case tp.method_id + when /(\A|_)each(_|\z)/, /(\A|_)map\!?\z/ + c_loops[method_name] += tp.self.size if tp.self.respond_to?(:size) + when 'times' + c_loops[method_name] += Integer(tp.self) + end +end.enable + run_benchmark(10) do visiting_routes.each do |path| # The app mutates `env`, so we need to create one every time. @@ -31,3 +46,14 @@ end end end + +puts "Top C loop method iterations:" +c_loops.sort_by(&:last).reverse_each do |method, count| + puts '%8d %s' % [count, method] +end +puts + +puts "Top 100 C method calls:" +c_calls.sort_by(&:last).reverse[0...100].each do |method, count| + puts '%8d %s' % [count, method] +end