loading...
Cover image for Profile arbitrary code in prod console

Profile arbitrary code in prod console

epigene profile image Augusts Bautra ・1 min read

Include stackprof in gemfile

gem "stackprof", ">= 0.2.13", 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
      # slow code
    end

  output_file = Rails.root.join("log/stackprof.json")  
  File.write(output_file, JSON.generate(profile))
  output_file
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

Discussion

markdown guide