Skip to content

Instantly share code, notes, and snippets.

@januszm
Last active January 17, 2018 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save januszm/67736b7a472c7cd3ddb393a7cb025e08 to your computer and use it in GitHub Desktop.
Save januszm/67736b7a472c7cd3ddb393a7cb025e08 to your computer and use it in GitHub Desktop.
# 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