Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Gemfile
# gem "ruby-prof"
# gem "ruby-prof-flamegraph"
# gem "stackprof"
require "ruby-prof"
require "stackprof"
module ProfilerWrapper
def self.stackprof(name)
GC.disable
::StackProf.run(
mode: :cpu,
raw: true, # for the flamegraph
out: "tmp/#{Time.now.to_i}_#{name}-stackprof.dump", # or Thread.current.object_id
interval: 1000 # microseconds
) do
yield
end
ensure
GC.enable
end
# DUMP=PATH_TO_FILE.dump
# bin/flamegraph.pl --countname=ms tmp/$DUMP > tmp/$DUMP.svg
# https://github.com/brendangregg/FlameGraph/blob/master/flamegraph.pl
def self.rubyprof(name)
GC.disable
result = ::RubyProf.profile do
yield
end
printer = ::RubyProf::FlameGraphPrinter.new(result)
printer.setup_options(print_file: true)
# or Thread.current.object_id
File.open("tmp/#{Time.now.to_i}_#{name}-rubyprof.dump", "w") do |file|
printer.print(file, {})
end
ensure
GC.enable
end
# DUMP=PATH_TO_FILE.dump
# stackprof --flamegraph tmp/$DUMP > tmp/$DUMP.flame
# stackprof --flamegraph-viewer=tmp/$DUMP.flame
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment