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

(Tuis.) #1

148 | Chapter 6: Performance


Measurement Tools


Of course, in order to properly measure performance, we need tools. This section is
concerned with analysis of Ruby and Rails code, as well as web applications in gen-
eral. There are a series of tools that can be used to analyze the full Rails stack, from
HTTP down to Ruby’s internals.


Black-Box Analysis


The most basic high-level measurement you will be interested in is:in the ideal case,
how fast can this server serve requests?While the answer is a somewhat nebulous
value that often bears no relation to actual performance under typical load, it is still
useful to compare against itself—for example, when testing caching or deploying a
new feature set.


This technique is calledblack-box analysis: we measure how much traffic the server
can handle, while treating it as a “black box.” For now, we don’t really care what’s
inside the box, only about how fast it can serve requests. We will leave the minutiae
of profiling and tweaking until later.


For this stage, we will need a benchmarking utility—but first, a brief diversion into
the world of mathematics.


Statistics: The least you need to know


It doesn’t take much knowledge of statistics to properly interpret the results of black-
box analysis, but there are some things you need to know.


Statistical analysis deals with the results of multiple samples, which in this case cor-
respond to HTT Presponse times. In Ruby fashion, we will illustrate this with a Ruby
array:


samples = %w(10 11 12 10 10).map{|x|x.to_f}

The average, ormean, of these samples is their sum divided by the number of sam-
ples. This is straightforward to translate into Ruby—adding a few methods to
Enumerable:


module Enumerable
def sum(identity = 0)
inject(identity) {|sum, x| sum + x}
end

def mean
sum.to_f / length
end
end
Free download pdf