views:

149

answers:

3

Hi folks! 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?

+1  A: 

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

Chris McCauley
+1  A: 

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.

Jonas Elfström
+1  A: 

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.

Mike Dunlavey