Last active
January 17, 2018 15:20
-
-
Save januszm/67736b7a472c7cd3ddb393a7cb025e08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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