Rails Optimization Example | 155
The IOTail library mixes in atail_linesmethod toIOandStringIO. This method
yields once for each line that is added to an open file, as it is added. This is used in
some of the other utility methods from the Rails Analyzer Tools.
The real gem in this library, however, israils_stat. This simple command-line util-
ity takes as arguments the path to a Rails production log and an optional refresh
interval, which defaults to 10 seconds. It sits on the logfile, watching for new lines,
and summarizes the traffic to the site, refreshing at the given interval until it receives
SIGINT (Ctrl-C):
$ rails_stat log/production.log
~ 1.4 req/sec, 0.0 queries/sec, 7.9 lines/sec
The implementation ofrails_statis very simple (it is based on IOTail). This code
could be used as the basis for a flexible real-time log analytics system.
Rails Optimization Example
To tie these concepts together, we will look at the process of benchmarking, profil-
ing, and optimizing a Rails action. This example comes from a real application, one
that is fairly large and complicated. We have seen pieces of this application before; it
is a map-based real estate search application. The application deals heavily with
geospatial data, and is based on the PostGIS spatial extensions to PostgreSQL.
We have identified an action to profile: the action that performs the search itself
(POST /searches). This action is not particularly slow in absolute terms, but it is our
most commonly used feature, and any more performance we can get reduces overall
latency and makes our application feel snappier.
Profiling an Action
Once we have decided on an action whose performance we want to improve, we can
profile it to see where our time is being spent. Jeremy Kemper recently added a new
request profiler to Rails, which will be released with the final version of Rails 2.0. Its
library is located at actionpack/lib/action_controller/request_profiler.rb, and it is
accessible throughscript/performance/request. It is a fairly simple wrapper around the
ruby-prof library, adding some commonly needed Rails functionality:
- Rather than running a single action, the request profiler runs a specified integra-
tion test script, so the test procedure can be arbitrarily complex. This also means
that we can profile non-GET requests, which is a must for the action we wish to
profile. - The request profiler can run a script multiple times, while only profiling time
actually spent in those actions (not the overhead of starting up the profiler). - The profiler script opens up both the flat and HTML graph profiles for us; we
will see later how both of these are useful.