Skip to content

Commit

Permalink
Update CallTreeProfiler to use new RubyProf::Profiler format (#46)
Browse files Browse the repository at this point in the history
Plus:

- More accurate `pop`/`shift`/`slice!` reporting
- Only require `benchmark/ips` when necessary
- One-liner blocks
  • Loading branch information
gonzedge committed May 12, 2023
1 parent 91da344 commit 3eba897
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 39 deletions.
22 changes: 7 additions & 15 deletions tasks/ips.rb
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'benchmark/ips'

namespace :ips do
task :pop_shift_slice do
compare_pop_shift_slice
Expand Down Expand Up @@ -33,6 +31,7 @@
end

def compare
require 'benchmark/ips'
Benchmark.ips do |bm|
yield bm

Expand All @@ -43,21 +42,14 @@ def compare
def compare_pop_shift_slice
compare do |bm|
a = []
bm.report('push') { a.push 1 }
bm.report('pop') { a.pop }

bm.report 'push/pop' do
a.push 1
a.pop
end
bm.report('unshift') { a.unshift 1 }
bm.report('shift') { a.shift }

bm.report 'unshift/shift' do
a.unshift 1
a.shift
end

bm.report 'shovel(<<)/slice!(0)' do
a << 1
a.slice! 0
end
bm.report('shovel(<<)') { a << 1 }
bm.report('slice!(0)') { a.slice! 0 }
end
end

Expand Down
2 changes: 0 additions & 2 deletions tasks/performance.rb
Expand Up @@ -10,8 +10,6 @@
dependencies = %i(serialization:regenerate performance:directory)

task :performance, arg_names => dependencies do |_, args|
require 'benchmark/ips'

configuration = Performance::Configuration.new
task = Performance::Task.new configuration
task.run(**args)
Expand Down
4 changes: 1 addition & 3 deletions tasks/performance/reporters/benchmark.rb
Expand Up @@ -30,9 +30,7 @@ def measure iterations, param

require 'benchmark'
measure = ::Benchmark.measure do
iterations.times do
result = yield param
end
iterations.times { result = yield param }
end

output.puts result.to_s.ljust 10
Expand Down
14 changes: 5 additions & 9 deletions tasks/performance/reporters/call_tree_profile.rb
Expand Up @@ -14,15 +14,11 @@ def do_report iterations, params
FileUtils.mkdir_p dirpath

require 'ruby-prof'
result = RubyProf.profile merge_fibers: true do
params.each do |param|
iterations.times do
yield param
end
end
end

printer = RubyProf::CallTreePrinter.new result
profile = RubyProf::Profile.new
profile.profile { params.each { |p| iterations.times { yield p } } }
profile.merge!

printer = RubyProf::CallTreePrinter.new profile
printer.print path: dirpath
end

Expand Down
6 changes: 1 addition & 5 deletions tasks/performance/reporters/flamegraph.rb
Expand Up @@ -15,11 +15,7 @@ def do_report iterations, params

require 'flamegraph'
::Flamegraph.generate filepath do
params.each do |param|
iterations.times do
yield param
end
end
params.each { |p| iterations.times { yield p } }
end
end

Expand Down
6 changes: 1 addition & 5 deletions tasks/performance/reporters/memory_profile.rb
Expand Up @@ -19,11 +19,7 @@ def do_report iterations, params
ignore_files: 'lib/rambling/trie/tasks',
) do
with_gc_stats "performing #{filename}" do
params.each do |param|
iterations.times do
yield param
end
end
params.each { |p| iterations.times { yield p } }
end
end

Expand Down

0 comments on commit 3eba897

Please sign in to comment.