Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:

Expand Down
17 changes: 15 additions & 2 deletions harness-cstats/harness.rb → harness-stats/harness.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
}