Use Linux perf for some region of Ruby code easily.
Run as root to use perf. Otherwise it just executes a given block.
require 'erb'
require 'perf'
def bench
ERB.new('<%= 1 %>').result
end
10000.times { bench }
if RubyVM::MJIT.enabled?
RubyVM::MJIT.pause
end
Perf.record(count: 5000, call_graph: :dwarf) do
50000.times { bench }
end
Perf.stat(event: %w[cycles instructions branches branch-misses]) do
50000.times { bench }
end
The gem is available as open source under the terms of the MIT License.