Specifications

If you prefer to have the results sent to you
via e-mail, rather than stored to a disk file, use
the -e option:
pl_analyze /var/log/production_log -e
¯
reuven@lerner.co.il
This option is particularly useful when you
invoke pl_analyze from a cron job, for example.
The output file from pl_analyze is divided
into three parts:
I Time spent in each request.
I Time spent in the database for each request.
I Time spent rendering the output from
each request.
For each controller action, pl_request lists how
many times it was invoked, as well as the average
time it took to execute. It also gives the min, max
and standard deviation, allowing you to see how
much the execution time varied over time.
Thus, the production log analyzer shows which
actions take the greatest amount of time overall,
which take the greatest amount of time in the
database (or to render) and how many times
each was invoked.
I have found pl_analyzer to be an indispensable
tool when trying to determine whether a site is fast
enough and where I should focus my attention to
improve its speed.
Request Profiler
The production log profiler shows which actions
require attention, but it doesn’t tell why a particular
action might be giving you trouble. For that, you
need to dive into the application a bit more, profil-
ing not a set of actions, but one particular action.
This is possible thanks to a built-in script that
comes with Rails, script/performance/request. This
script follows a set of instructions written in a (pre-
sumably short) Ruby program, using a similar set of
commands and subroutines that are available for
integration tests.
In other words, you use integration-test syntax to
describe a short sequence of one or more actions and
run this program via the request profiler. Then, the
request profiler produces two output files that describe
what was going on behind the scenes as those
requests were serviced. This information can help you
improve the performance of this particular action.
In order for this script to work, first install the
ruby-prof gem:
gem install --remote ruby-prof
Once that is installed, you need to create a simple
integration test script. This script doesn’t need to be
wrapped in the same object that the integration tests
themselves use. Instead, simply create a file named
test.rb, and put it somewhere on the filesystem. I
created a directory named test/performance and put
it there, with the one-line contents as follows:
get('/')
Notice that I’m using URLs here, rather than
names of controllers and actions. Finally, with this in
place, invoke the profiler:
script/performance/request -n 10 test/performance/test.rb
Now you should see the program telling you
that it’s warming up and then reporting as it goes
through each of the iterations you specified. In the
above example, the -n 10 option indicates the
number of times the script should be invoked;
by default, it’s 100.
Note that the output files are put in the test
directory (to which you might not have write access
by default). And, indeed, the output files are quite
useful, but they can be confusing the first time you
look at them.
The first output file, profile-output.txt, is (as the
suffix implies) a text file that shows how much time
was spent in each method, both as a time measure
and as a percentage of the total run time. Consider
the following:
%self total self wait child calls name
13.74 58.35 38.13 0.00 20.22 608720 Buffer#read
24 | august 2008 www.linuxjournal.com
AT THE FORGE
COLUMNS
Resources
The Rails Way, by Obie Fernandez, has become
my favorite, because it includes so much useful
information, as well as code examples. It doesn’t
try to teach you Rails, but it does provide a great
deal of information that is useful for advanced
users as well as newcomers.
Advanced Rails, by Brad Ediger, gives some greater
depth to several topics, such as performance opti-
mization, ActiveRecord features, RESTful sites and
internationalization, among others.
Rails Analyzer Tools: this is a collection of tools that
can help you better understand your Rails-based site.
The production log profiler is part of the Rails Analysis
Tools set; see
rails-analyzer.rubyforge.org
for
more information.