Augusts Bautra
Posted on June 10, 2020
Include stackprof in gemfile
gem "stackprof", ">= 0.2.26", require: false
Require stackprof and run slow code in its block
require "stackprof"
def profile!
profile =
StackProf.run(ignore_gc: true, interval: 1000, mode: :wall, raw: true) do
@value = # slow code
end
output_file = Rails.root.join("log/stackprof.json")
File.write(output_file, JSON.generate(profile))
@value # return the original
end
profile!
DL the file to local computer
scp PROD:/path/to/log/stackprof.json ~/Desktop/profile.json
Upload the file to https://www.speedscope.app and study flamegraph
Addendum
You can simplify accessing the file somewhat by writing it into Rails' public/
directory to have access it via the web-server.
For example
output_file = Rails.root.join("public/stackprof.json")
Will be accessible at https://<your.site>/stackprof.json
. Be mindful of security risks.
💖 💪 🙅 🚩
Augusts Bautra
Posted on June 10, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
ruby Rails profiling story, or how I caught Faker trying to teach my app Australian Slang
February 20, 2019