Tracing a ruby process as it is running

I am running a ruby code that has a significant number of http (web and api) requests, as well as some pretty intensive data processing. As you might expect it is pretty slow, and i want to find out where the main bottle necks are. I do not think i need a full blown profiler (yet), but want some way of tracing the process as it runs (without writing puts statement all over the file)

I imagine that this would be pretty common thing to want to have, any thoughts on how this can be accomplished, perhaps a gem/plugin that does this?

13.10.2009 17:40:16
ruby-prof is very easy to install, and will give you the info you want. Even though you mention you don't think you need a profiler, any reason not to try it out? ruby-prof.rubyforge.org
hgmnz 13.10.2009 18:44:05
Is there any downside to using a profiler? Are you worried about it giving you more information than you can handle?
Andrew Grimm 13.10.2009 22:19:22
4 ОТВЕТА
РЕШЕНИЕ

You could try Hijack, it gives you an irb session to an existing ruby process. While in context you could benchmark the suspected requests something like:

require 'benchmark'
puts Benchmark.measure { the_slow_Http_Request }

Joe Damato's ltrace hack also seems very nice but I haven't tried it myself.

3
14.10.2009 08:00:29
From the author: github.com/ileitch/hijack/issues/17: "Hijack is has not worked reliably since Ruby 1.8."
mpelzsherman 3.08.2016 15:50:56

Benchmark it!

I've found that a few well placed calls to benchmark usually narrow down the problems pretty quickly - profilers are fine but a simple roll-your-own solution often works well.

One major bonus using this approach for me was finding just how big a difference there is in batching up database inserts.

In fact I've left the benchmarks in place in a few programs just to constantly check that performance keeps within acceptable areas

1
14.10.2009 07:28:37

Can you run it under a debugger and pause it manually while it is being slow? If you do that several times, you will find out what the biggest bottlenecks are.

For example, if it is in the process of waiting for a web or api request, that will be at the top of the stack. Then look at the line of code one level down the stack, which is a function call. The I/O request is being done because of that function call. Then look at the next line - same thing.

The same is true if it is not doing I/O, like if it is computing. Every line of code on the call stack bears full responsibility for that slice of time being used.

So, if you pause it several times, any line of code that appears on the call stack on more than one pause, is telling you it is a bottleneck, if there is any way you could remove it or execute it less often.

Here's a more complete explanation.

1
23.05.2017 10:24:19

https://rbspy.github.io is pretty impressive and stupid easy to use. just send it your pid and you can get profiling and a nice flame graph.

0
20.04.2018 19:11:11