The two tools are actually used in pretty different ways! rbspy is a command line tool (
rbspy record --pid YOUR_PID),
and stackprof is a library that you can include in your Ruby program and use to profile a given
section of code.
rbspy profiles everything a given Ruby process is doing – you give it a PID of a Ruby process, and it profiles it. It’s useful when:
rbspy record ruby my-script.rb)
One common use case for rbspy is profiling slow unit test runs – instead of spending a bunch of
time adding instrumentation, you can run
rbspy record ruby my-test.rb and instantly get profiling
information about what’s going on.
Stackprof requires more work to set up, and gives you more control over which code gets profiled. It’s best when you want to profile a specific section of your code, or only want to profile certain HTTP requests.
Here’s what editing your code to use Stackprof looks like
StackProf.run(mode: :cpu, out: 'tmp/stackprof-cpu-myapp.dump', raw: true) do # code you want to profile here end
Here are the steps to using Stackprof:
stackprofgem to your Gemfile
stackprofto summarize the reported data from the
A more batteries-included way of doing profiling if you have a Rails/Rack program is to use rack-mini-profiler. It uses stackprof under the hood to do CPU profiling as well as supporting memory profiling. Here’s a blog post about rack-mini-profiler.