Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1
Measurement Tools | 153

Don’tjust go poking around looking for code to benchmark, though. Profilers are
much more powerful utilities when you don’t know what needs optimization. We
will consider them next.


Rails Analyzer Tools


The Rails Analyzer Tools (http://rails-analyzer.rubyforge.org/) are a set of utilities that
can help profile your Rails application. While Benchmark is Ruby-specific, and
httperf will benchmark any web site, the Rails Analyzer Tools were written with
Rails in mind.


Production log analyzer


The first tool, the Production Log Analyzer, scans your production logfiles to find
the slowest actions in your deployed application. This is very useful for problem
solving, but it has a number of downsides. It requires the logs to go through the
SyslogLogger(provided with the Rails Analyzer Tools), so you must set this up
before the requests come in. The tool also requires a syslogd that supports filtering
on program names, which usually means either running BSD or installing syslog-ng.
For these reasons, we will not go into detail on its use here. Complete setup instruc-
tions are available athttp://rails-analyzer.rubyforge.org/pl_analyze/.


Action profiler


Once you know which actions are hurting for performance, you need a way to dig
deeper. Action Profiler is a library (and a corresponding executable,action_profiler)
that glues Rails to a profiler. It profiles a single Rails action through the entire Rails
stack and application code so that you can examine where the most time is being
spent during that action.


Action Profiler can be used with one of several profiling engines: ZenProfiler, ruby-
prof, and Ruby’s built-inProfilerclass. They will be tried in that order unless one is
specified on the command line with the-Poption. Here is a sample run of Ruby’s
Profiler, showing a flat call profile:


$ action_profiler -P Profiler ListingController#map
Warmup...
Profiling...
% cumulative self self total
time seconds seconds calls ms/call ms/call name
13.73 0.07 0.07 204 0.34 0.34 ERB::Util.html_escape
11.76 0.13 0.06 1462 0.04 0.08 String#gsub
11.76 0.19 0.06 6 10.00 10.00 AssetTagHelper.
compute_public_path
9.80 0.24 0.05 89 0.56 0.67 FormOptionsHelper.
option_value_selected?
9.80 0.29 0.05 178 0.28 0.28 Array#<<
3.92 0.31 0.02 44 0.45 6.36 Array#each
3.92 0.33 0.02 287 0.07 0.07 Kernel.respond_to?
Free download pdf