While investigating performance problems in a Rails app, a profiler is quite helpful. stackprof is such a tool which can be added to an existing Rails app quite easily.
As usual, add
Gemfile and run
To enable profiling, add this line to your configuration:
Important here is the
save_every: 5 parameter. It means that profile data is written after 5 HTTP request to your application.
Restart your server
And make 5 requests to your app (usually you would investigate performance issues for a single action, i.e. you would run the same request 5 times in a row).
Now, raw profile data should be created in files
Finally, you would generate profiling information by
This gives you something like
wall vs cpu
Depending on your use case, you might want to
- analyze time spent in your app together with other systems like a database or an external http service (
wallmode = total runtime)
– or –
- analyze time spent in your app itself (
cpumode = cpu time)
You might have noticed that in the listing above, most time is spent for
Puma.single. This is because we’ve used
mode: :wall in our configuration. Puma.single basically means i/o wait.
How it works
Note that stackprof is a sampling profiler:
Stackprof is a sampling call-stack profile for Ruby 2.1+. Instead of tracking all method calls, it will simply collect the current stack trace of a running program at fixed intervals. Methods that appear on top of the stack trace most often, are the methods your program spends most of its time in. (https://makandracards.com/makandra/38975-stackprof-sampling-call-stack-profiler-for-ruby)