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

(Tuis.) #1

162 | Chapter 6: Performance


The benchmarking tool we will be using is Railsbench (http://railsbench.rubyforge.org/)
by Stefan Kaes. This provides some very convenient tools for benchmarking Rails
applications and visualizing the results. Railsbench has a slightly complicated instal-
lation procedure. First, install the gem:


$ sudo gem install railsbench

Next, you should add the directory containing the Railsbench scripts to your PATH,
and make them executable. This can be done as follows:


$ eval `railsbench path`
$ sudo railsbench postinstall

Railsbench looks for some environment variables to know where the Rails applica-
tion is and where its data should go, so we need to export those:


$ export RAILS_ROOT=.
$ export RAILS_PERF_DATA=.

Now Railsbench is installed and ready to run. But first, there are some changes we
need to make to the application. We install the Railsbench code into our application:


$ railsbench install

That command provides us with a few files that are used for benchmarking:


config/benchmarks.rb
Railsbench configuration. Use this file to provide Rails with custom session data,
if needed.


config/benchmarks.yml
Defines the set of benchmarks that can be run; each one has a name, URI, HTTP
method, and optional POST data. Most of this file can be generated automati-
cally from the application’s routes; we will do this as our next step.


config/environments/benchmarking.rb
This is a separate Rails environment used for benchmarking. When created, it is
a copy of theproduction.rbfile, but it can be customized to meet the bench-
mark’s needs.


Railsbench has a command to generate theconfig/benchmarks.ymlfile based on the
application’s routes. We will run this to create the basic version of the file.


$ railsbench generate_benchmarks

We delete actions from this file that we do not need to benchmark (such as named
routes generated automatically bymap.resources, some of which are unused). And
we modify thesearches_createbenchmark, changing the method topostand add-
ing the POST data we need:


searches_create:
uri: /searches/
action: create
controller: searches
method: post
post_data: "search[pclass]=1&search[city_id]=149&search[subtype]=HOUSE"
Free download pdf