Then do a baseline measurement that requests a
static file from the front end web server. You will never get Rails faster than this.
??? Run your tests from a machine as close as possible (in network terms) to the
server. You want to eliminate latency and jitter (variance in latency) from the
results.
??? Do not run performance tests from your web server. There are too many interactions
between the server and the analyzer that will be confounded with your
results. Even if CPU utilization is not a problem (such as on a multiprocessor
machine), you will not know if I/O contention has skewed the results.
Code Timing
The Ruby standard library includes Benchmark, which can be used to answer simple
questions about code performance. The key word here is simple: it is all too easy to
ignore confounding factors and take the numbers that Benchmark gives you as
gospel.
Suppose we want to compare conventional method dispatch to the idiom of using
method_missing and then examining the method name to decide what action to take.
Here is a simple code example that benchmarks the two options:
require 'benchmark'
class Test
def test_one
1 + 1
end
def method_missing(method_id)
case method_id
when :test_unknown: 1 + 1
152 | Chapter 6: Performance
else super
end
end
end
t = Test.new
Benchmark.bmbm do |b|
b.report("Direct method call") do
1_000_000.times { t.test_one }
end
b.
Pages:
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236