-
Notifications
You must be signed in to change notification settings - Fork 1
/
performance_testing.html
852 lines (825 loc) · 38.3 KB
/
performance_testing.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Ruby on Rails Guides: Performance Testing Rails Applications</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
</head>
<body class="guide">
<div id="topNav">
<div class="wrapper">
<strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
<a href="http://rubyonrails.org/">Overview</a> |
<a href="http://rubyonrails.org/download">Download</a> |
<a href="http://rubyonrails.org/deploy">Deploy</a> |
<a href="https://github.com/rails/rails">Code</a> |
<a href="http://rubyonrails.org/screencasts">Screencasts</a> |
<a href="http://rubyonrails.org/documentation">Documentation</a> |
<a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
<a href="http://rubyonrails.org/community">Community</a> |
<a href="http://weblog.rubyonrails.org/">Blog</a>
</div>
</div>
<div id="header">
<div class="wrapper clearfix">
<h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
<ul class="nav">
<li><a href="index.html">Home</a></li>
<li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
<div id="guides" class="clearfix" style="display: none;">
<hr />
<dl class="L">
<dt>Start Here</dt>
<dd><a href="getting_started.html">Getting Started with Rails</a></dd>
<dt>Models</dt>
<dd><a href="migrations.html">Rails Database Migrations</a></dd>
<dd><a href="active_record_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
<dd><a href="association_basics.html">Active Record Associations</a></dd>
<dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
<dt>Views</dt>
<dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
<dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
<dt>Controllers</dt>
<dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
<dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
</dl>
<dl class="R">
<dt>Digging Deeper</dt>
<dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
<dd><a href="i18n.html">Rails Internationalization API</a></dd>
<dd><a href="security.html">Securing Rails Applications</a></dd>
<dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
<dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
<dd><a href="configuring.html">Configuring Rails Applications</a></dd>
<dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
<dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
<dt>Extending Rails</dt>
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
<dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
<dt>Contributing to Ruby on Rails</dt>
<dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
<dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
<dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
<dt>Release Notes</dt>
<dd><a href="3_2_release_notes.html">Ruby on Rails 3.2 Release Notes</a></dd>
<dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
<dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
<dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
<dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
</dl>
</div>
</li>
<li><a href="contributing_to_ruby_on_rails.html">Contribute</a></li>
<li><a href="credits.html">Credits</a></li>
</ul>
</div>
</div>
<hr class="hide" />
<div id="feature">
<div class="wrapper">
<h2>Performance Testing Rails Applications</h2>
<p>This guide covers the various ways of performance testing a Ruby on Rails application. By referring to this guide, you will be able to:</p>
<ul>
<li>Understand the various types of benchmarking and profiling metrics</li>
<li>Generate performance and benchmarking tests</li>
<li>Install and use a GC-patched Ruby binary to measure memory usage and object allocation</li>
<li>Understand the benchmarking information provided by Rails inside the log files</li>
<li>Learn about various tools facilitating benchmarking and profiling</li>
</ul>
<p>Performance testing is an integral part of the development cycle. It is very important that you don’t make your end users wait for too long before the page is completely loaded. Ensuring a pleasant browsing experience for end users and cutting the cost of unnecessary hardware is important for any non-trivial web application.</p>
<div id="subCol">
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
<ol class="chapters">
<li><a href="#performance-test-cases">Performance Test Cases</a><ul><li><a href="#generating-performance-tests">Generating Performance Tests</a></li> <li><a href="#examples">Examples</a></li> <li><a href="#modes">Modes</a></li> <li><a href="#metrics">Metrics</a></li> <li><a href="#understanding-the-output">Understanding the Output</a></li> <li><a href="#tuning-test-runs">Tuning Test Runs</a></li> <li><a href="#performance-test-environment">Performance Test Environment</a></li> <li><a href="#installing-gc-patched-mri">Installing GC-Patched <span class="caps">MRI</span></a></li> <li><a href="#using-ruby-prof-on-mri-and-ree">Using Ruby-Prof on <span class="caps">MRI</span> and <span class="caps">REE</span></a></li></ul></li><li><a href="#command-line-tools">Command Line Tools</a><ul><li><a href="#benchmarker"><tt>benchmarker</tt></a></li> <li><a href="#profiler"><tt>profiler</tt></a></li></ul></li><li><a href="#helper-methods">Helper Methods</a><ul><li><a href="#model">Model</a></li> <li><a href="#controller">Controller</a></li> <li><a href="#view">View</a></li></ul></li><li><a href="#request-logging">Request Logging</a></li><li><a href="#useful-links">Useful Links</a><ul><li><a href="#rails-plugins-and-gems">Rails Plugins and Gems</a></li> <li><a href="#generic-tools">Generic Tools</a></li> <li><a href="#tutorials-and-documentation">Tutorials and Documentation</a></li></ul></li><li><a href="#commercial-products">Commercial Products</a></li></ol></div>
</div>
</div>
<div id="container">
<div class="wrapper">
<div id="mainCol">
<h3 id="performance-test-cases">1 Performance Test Cases</h3>
<p>Rails performance tests are a special type of integration tests, designed for benchmarking and profiling the test code. With performance tests, you can determine where your application’s memory or speed problems are coming from, and get a more in-depth picture of those problems.</p>
<p>In a freshly generated Rails application, <tt>test/performance/browsing_test.rb</tt> contains an example of a performance test:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
require 'test_helper'
require 'rails/performance_test_help'
# Profiling results for each test method are written to tmp/performance.
class BrowsingTest < ActionDispatch::PerformanceTest
def test_homepage
get '/'
end
end
</pre>
</div>
<p>This example is a simple performance test case for profiling a <span class="caps">GET</span> request to the application’s homepage.</p>
<h4 id="generating-performance-tests">1.1 Generating Performance Tests</h4>
<p>Rails provides a generator called <tt>performance_test</tt> for creating new performance tests:</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rails generate performance_test homepage
</pre>
</div>
<p>This generates <tt>homepage_test.rb</tt> in the <tt>test/performance</tt> directory:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
require 'test_helper'
require 'rails/performance_test_help'
class HomepageTest < ActionDispatch::PerformanceTest
# Replace this with your real tests.
def test_homepage
get '/'
end
end
</pre>
</div>
<h4 id="examples">1.2 Examples</h4>
<p>Let’s assume your application has the following controller and model:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# routes.rb
root :to => 'home#index'
resources :posts
# home_controller.rb
class HomeController < ApplicationController
def dashboard
@users = User.last_ten.includes(:avatars)
@posts = Post.all_today
end
end
# posts_controller.rb
class PostsController < ApplicationController
def create
@post = Post.create(params[:post])
redirect_to(@post)
end
end
# post.rb
class Post < ActiveRecord::Base
before_save :recalculate_costly_stats
def slow_method
# I fire gallzilion queries sleeping all around
end
private
def recalculate_costly_stats
# CPU heavy calculations
end
end
</pre>
</div>
<h5 id="controller-example">1.2.1 Controller Example</h5>
<p>Because performance tests are a special kind of integration test, you can use the <tt>get</tt> and <tt>post</tt> methods in them.</p>
<p>Here’s the performance test for <tt>HomeController#dashboard</tt> and <tt>PostsController#create</tt>:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
require 'test_helper'
require 'rails/performance_test_help'
class PostPerformanceTest < ActionDispatch::PerformanceTest
def setup
# Application requires logged-in user
login_as(:lifo)
end
def test_homepage
get '/dashboard'
end
def test_creating_new_post
post '/posts', :post => { :body => 'lifo is fooling you' }
end
end
</pre>
</div>
<p>You can find more details about the <tt>get</tt> and <tt>post</tt> methods in the <a href="testing.html">Testing Rails Applications</a> guide.</p>
<h5 id="model-example">1.2.2 Model Example</h5>
<p>Even though the performance tests are integration tests and hence closer to the request/response cycle by nature, you can still performance test pure model code.</p>
<p>Performance test for <tt>Post</tt> model:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
require 'test_helper'
require 'rails/performance_test_help'
class PostModelTest < ActionDispatch::PerformanceTest
def test_creation
Post.create :body => 'still fooling you', :cost => '100'
end
def test_slow_method
# Using posts(:awesome) fixture
posts(:awesome).slow_method
end
end
</pre>
</div>
<h4 id="modes">1.3 Modes</h4>
<p>Performance tests can be run in two modes: Benchmarking and Profiling.</p>
<h5 id="benchmarking">1.3.1 Benchmarking</h5>
<p>Benchmarking makes it easy to quickly gather a few metrics about each test run. By default, each test case is run <strong>4 times</strong> in benchmarking mode.</p>
<p>To run performance tests in benchmarking mode:</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rake test:benchmark
</pre>
</div>
<h5 id="profiling">1.3.2 Profiling</h5>
<p>Profiling allows you to make an in-depth analysis of each of your tests by using an external profiler. Depending on your Ruby interpreter, this profiler can be native (Rubinius, JRuby) or not (<span class="caps">MRI</span>, which uses RubyProf). By default, each test case is run <strong>once</strong> in profiling mode.</p>
<p>To run performance tests in profiling mode:</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rake test:profile
</pre>
</div>
<h4 id="metrics">1.4 Metrics</h4>
<p>Benchmarking and profiling run performance tests and give you multiple metrics. The availability of each metric is determined by the interpreter being used—none of them support all metrics—and by the mode in use. A brief description of each metric and their availability across interpreters/modes is given below.</p>
<h5 id="wall-time">1.4.1 Wall Time</h5>
<p>Wall time measures the real world time elapsed during the test run. It is affected by any other processes concurrently running on the system.</p>
<h5 id="process-time">1.4.2 Process Time</h5>
<p>Process time measures the time taken by the process. It is unaffected by any other processes running concurrently on the same system. Hence, process time is likely to be constant for any given performance test, irrespective of the machine load.</p>
<h5 id="cpu-time">1.4.3 <span class="caps">CPU</span> Time</h5>
<p>Similar to process time, but leverages the more accurate <span class="caps">CPU</span> clock counter available on the Pentium and PowerPC platforms.</p>
<h5 id="user-time">1.4.4 User Time</h5>
<p>User time measures the amount of time the <span class="caps">CPU</span> spent in user-mode, i.e. within the process. This is not affected by other processes and by the time it possibly spends blocked.</p>
<h5 id="memory">1.4.5 Memory</h5>
<p>Memory measures the amount of memory used for the performance test case.</p>
<h5 id="objects">1.4.6 Objects</h5>
<p>Objects measures the number of objects allocated for the performance test case.</p>
<h5 id="gc-runs">1.4.7 GC Runs</h5>
<p>GC Runs measures the number of times GC was invoked for the performance test case.</p>
<h5 id="gc-time">1.4.8 GC Time</h5>
<p>GC Time measures the amount of time spent in GC for the performance test case.</p>
<h5 id="metric-availability">1.4.9 Metric Availability</h5>
<h6 id="benchmarking_1">1.4.9.1 Benchmarking</h6>
<table>
<tr>
<th>Interpreter</th>
<th>Wall Time</th>
<th>Process Time</th>
<th><span class="caps">CPU</span> Time</th>
<th>User Time</th>
<th>Memory</th>
<th>Objects</th>
<th>GC Runs</th>
<th>GC Time</th>
</tr>
<tr>
<th><span class="caps">MRI</span> </th>
<td> yes </td>
<td> yes </td>
<td> yes </td>
<td> no </td>
<td> yes </td>
<td> yes </td>
<td> yes </td>
<td> yes </td>
</tr>
<tr>
<th><span class="caps">REE</span> </th>
<td> yes </td>
<td> yes </td>
<td> yes </td>
<td> no </td>
<td> yes </td>
<td> yes </td>
<td> yes </td>
<td> yes </td>
</tr>
<tr>
<th>Rubinius </th>
<td> yes </td>
<td> no </td>
<td> no </td>
<td> no </td>
<td> yes </td>
<td> yes </td>
<td> yes </td>
<td> yes </td>
</tr>
<tr>
<th>JRuby </th>
<td> yes </td>
<td> no </td>
<td> no </td>
<td> yes </td>
<td> yes </td>
<td> yes </td>
<td> yes </td>
<td> yes </td>
</tr>
</table>
<h6 id="profiling_1">1.4.9.2 Profiling</h6>
<table>
<tr>
<th>Interpreter</th>
<th>Wall Time</th>
<th>Process Time</th>
<th><span class="caps">CPU</span> Time</th>
<th>User Time</th>
<th>Memory</th>
<th>Objects</th>
<th>GC Runs</th>
<th>GC Time</th>
</tr>
<tr>
<th><span class="caps">MRI</span> </th>
<td> yes </td>
<td> yes </td>
<td> no </td>
<td> no </td>
<td> yes </td>
<td> yes </td>
<td> yes </td>
<td> yes </td>
</tr>
<tr>
<th><span class="caps">REE</span> </th>
<td> yes </td>
<td> yes </td>
<td> no </td>
<td> no </td>
<td> yes </td>
<td> yes </td>
<td> yes </td>
<td> yes </td>
</tr>
<tr>
<th>Rubinius </th>
<td> yes </td>
<td> no </td>
<td> no </td>
<td> no </td>
<td> no </td>
<td> no </td>
<td> no </td>
<td> no </td>
</tr>
<tr>
<th>JRuby </th>
<td> yes </td>
<td> no </td>
<td> no </td>
<td> no </td>
<td> no </td>
<td> no </td>
<td> no </td>
<td> no </td>
</tr>
</table>
<div class="note"><p>To profile under JRuby you’ll need to run <tt>export JRUBY_OPTS="-Xlaunch.inproc=false --profile.api"</tt> <strong>before</strong> the performance tests.</p></div>
<h4 id="understanding-the-output">1.5 Understanding the Output</h4>
<p>Performance tests generate different outputs inside <tt>tmp/performance</tt> directory depending on their mode and metric.</p>
<h5 id="output-benchmarking">1.5.1 Benchmarking</h5>
<p>In benchmarking mode, performance tests generate two types of outputs.</p>
<h6 id="output-command-line">1.5.1.1 Command Line</h6>
<p>This is the primary form of output in benchmarking mode. Example:</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
BrowsingTest#test_homepage (31 ms warmup)
wall_time: 6 ms
memory: 437.27 KB
objects: 5,514
gc_runs: 0
gc_time: 19 ms
</pre>
</div>
<h6 id="csv-files">1.5.1.2 <span class="caps">CSV</span> Files</h6>
<p>Performance test results are also appended to <tt>.csv</tt> files inside <tt>tmp/performance</tt>. For example, running the default <tt>BrowsingTest#test_homepage</tt> will generate following five files:</p>
<ul>
<li>BrowsingTest#test_homepage_gc_runs.csv</li>
<li>BrowsingTest#test_homepage_gc_time.csv</li>
<li>BrowsingTest#test_homepage_memory.csv</li>
<li>BrowsingTest#test_homepage_objects.csv</li>
<li>BrowsingTest#test_homepage_wall_time.csv</li>
</ul>
<p>As the results are appended to these files each time the performance tests are run in benchmarking mode, you can collect data over a period of time. This can be very helpful in analyzing the effects of code changes.</p>
<p>Sample output of <tt>BrowsingTest#test_homepage_wall_time.csv</tt>:</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
measurement,created_at,app,rails,ruby,platform
0.00738224999999992,2009-01-08T03:40:29Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
0.00755874999999984,2009-01-08T03:46:18Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
0.00762099999999993,2009-01-08T03:49:25Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
0.00603075000000008,2009-01-08T04:03:29Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
0.00619899999999995,2009-01-08T04:03:53Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
0.00755449999999991,2009-01-08T04:04:55Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
0.00595999999999997,2009-01-08T04:05:06Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
0.00740450000000004,2009-01-09T03:54:47Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
0.00603150000000008,2009-01-09T03:54:57Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
0.00771250000000012,2009-01-09T15:46:03Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
</pre>
</div>
<h5 id="output-profiling">1.5.2 Profiling</h5>
<p>In profiling mode, performance tests can generate multiple types of outputs. The command line output is always presented but support for the others is dependent on the interpreter in use. A brief description of each type and their availability across interpreters is given below.</p>
<h6 id="command-line">1.5.2.1 Command Line</h6>
<p>This is a very basic form of output in profiling mode:</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
BrowsingTest#test_homepage (58 ms warmup)
process_time: 63 ms
memory: 832.13 KB
objects: 7,882
</pre>
</div>
<h6 id="flat">1.5.2.2 Flat</h6>
<p>Flat output shows the metric—time, memory, etc—measure in each method. <a href="http://ruby-prof.rubyforge.org/files/examples/flat_txt.html">Check Ruby-Prof documentation for a better explanation</a>.</p>
<h6 id="graph">1.5.2.3 Graph</h6>
<p>Graph output shows the metric measure in each method, which methods call it and which methods it calls. <a href="http://ruby-prof.rubyforge.org/files/examples/graph_txt.html">Check Ruby-Prof documentation for a better explanation</a>.</p>
<h6 id="tree">1.5.2.4 Tree</h6>
<p>Tree output is profiling information in calltree format for use by <a href="http://kcachegrind.sourceforge.net/html/Home.html">kcachegrind</a> and similar tools.</p>
<h6 id="output-availability">1.5.2.5 Output Availability</h6>
<table>
<tr>
<th></th>
<th>Flat</th>
<th>Graph</th>
<th>Tree</th>
</tr>
<tr>
<th><span class="caps">MRI</span> </th>
<td> yes </td>
<td> yes </td>
<td> yes </td>
</tr>
<tr>
<th><span class="caps">REE</span> </th>
<td> yes </td>
<td> yes </td>
<td> yes </td>
</tr>
<tr>
<th>Rubinius </th>
<td> yes </td>
<td> yes </td>
<td> no </td>
</tr>
<tr>
<th>JRuby </th>
<td> yes </td>
<td> yes </td>
<td> no </td>
</tr>
</table>
<h4 id="tuning-test-runs">1.6 Tuning Test Runs</h4>
<p>Test runs can be tuned by setting the <tt>profile_options</tt> class variable on your test class.</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
require 'test_helper'
require 'rails/performance_test_help'
# Profiling results for each test method are written to tmp/performance.
class BrowsingTest < ActionDispatch::PerformanceTest
self.profile_options = { :runs => 5,
:metrics => [:wall_time, :memory] }
def test_homepage
get '/'
end
end
</pre>
</div>
<p>In this example, the test would run 5 times and measure wall time and memory. There are a few configurable options:</p>
<table>
<tr>
<th>Option </th>
<th>Description</th>
<th>Default</th>
<th>Mode</th>
</tr>
<tr>
<td><tt>:runs</tt> </td>
<td>Number of runs.</td>
<td>Benchmarking: 4, Profiling: 1</td>
<td>Both</td>
</tr>
<tr>
<td><tt>:output</tt> </td>
<td>Directory to use when writing the results.</td>
<td><tt>tmp/performance</tt></td>
<td>Both</td>
</tr>
<tr>
<td><tt>:metrics</tt> </td>
<td>Metrics to use.</td>
<td>See below.</td>
<td>Both</td>
</tr>
<tr>
<td><tt>:formats</tt> </td>
<td>Formats to output to.</td>
<td>See below.</td>
<td>Profiling</td>
</tr>
</table>
<p>Metrics and formats have different defaults depending on the interpreter in use.</p>
<table>
<tr>
<th>Interpreter</th>
<th>Mode</th>
<th>Default metrics</th>
<th>Default formats</th>
</tr>
<tr>
<td rowspan="2"><span class="caps">MRI</span>/<span class="caps">REE</span> </td>
<td>Benchmarking</td>
<td><tt>[:wall_time, :memory, :objects, :gc_runs, :gc_time]</tt></td>
<td>N/A</td>
</tr>
<tr>
<td>Profiling </td>
<td><tt>[:process_time, :memory, :objects]</tt></td>
<td><tt>[:flat, :graph_html, :call_tree, :call_stack]</tt></td>
</tr>
<tr>
<td rowspan="2">Rubinius</td>
<td>Benchmarking</td>
<td><tt>[:wall_time, :memory, :objects, :gc_runs, :gc_time]</tt></td>
<td>N/A</td>
</tr>
<tr>
<td>Profiling </td>
<td><tt>[:wall_time]</tt></td>
<td><tt>[:flat, :graph]</tt></td>
</tr>
<tr>
<td rowspan="2">JRuby </td>
<td>Benchmarking</td>
<td><tt>[:wall_time, :user_time, :memory, :gc_runs, :gc_time]</tt></td>
<td>N/A</td>
</tr>
<tr>
<td>Profiling </td>
<td><tt>[:wall_time]</tt></td>
<td><tt>[:flat, :graph]</tt></td>
</tr>
</table>
<p>As you’ve probably noticed by now, metrics and formats are specified using a symbol array with each name <a href="http://api.rubyonrails.org/classes/String.html#method-i-underscore">underscored.</a></p>
<h4 id="performance-test-environment">1.7 Performance Test Environment</h4>
<p>Performance tests are run in the <tt>test</tt> environment. But running performance tests will set the following configuration parameters:</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
ActionController::Base.perform_caching = true
ActiveSupport::Dependencies.mechanism = :require
Rails.logger.level = ActiveSupport::BufferedLogger::INFO
</pre>
</div>
<p>As <tt>ActionController::Base.perform_caching</tt> is set to <tt>true</tt>, performance tests will behave much as they do in the <tt>production</tt> environment.</p>
<h4 id="installing-gc-patched-mri">1.8 Installing GC-Patched <span class="caps">MRI</span></h4>
<p>To get the best from Rails’ performance tests under <span class="caps">MRI</span>, you’ll need to build a special Ruby binary with some super powers.</p>
<p>The recommended patches for each <span class="caps">MRI</span> version are:</p>
<table>
<tr>
<th>Version</th>
<th>Patch</th>
</tr>
<tr>
<td>1.8.6</td>
<td>ruby186gc</td>
</tr>
<tr>
<td>1.8.7</td>
<td>ruby187gc</td>
</tr>
<tr>
<td>1.9.2 and above</td>
<td>gcdata</td>
</tr>
</table>
<p>All of these can be found on <a href="https://github.com/wayneeseguin/rvm/tree/master/patches/ruby">RVM’s <em>patches</em> directory</a> under each specific interpreter version.</p>
<p>Concerning the installation itself, you can either do this easily by using <a href="http://rvm.beginrescueend.com"><span class="caps">RVM</span></a> or you can build everything from source, which is a little bit harder.</p>
<h5 id="install-using-rvm">1.8.1 Install Using <span class="caps">RVM</span></h5>
<p>The process of installing a patched Ruby interpreter is very easy if you let <span class="caps">RVM</span> do the hard work. All of the following <span class="caps">RVM</span> commands will provide you with a patched Ruby interpreter:</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rvm install 1.9.2-p180 --patch gcdata
$ rvm install 1.8.7 --patch ruby187gc
$ rvm install 1.9.2-p180 --patch ~/Downloads/downloaded_gcdata_patch.patch
</pre>
</div>
<p>You can even keep your regular interpreter by assigning a name to the patched one:</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rvm install 1.9.2-p180 --patch gcdata --name gcdata
$ rvm use 1.9.2-p180 # your regular ruby
$ rvm use 1.9.2-p180-gcdata # your patched ruby
</pre>
</div>
<p>And it’s done! You have installed a patched Ruby interpreter.</p>
<h5 id="install-from-source">1.8.2 Install From Source</h5>
<p>This process is a bit more complicated, but straightforward nonetheless. If you’ve never compiled a Ruby binary before, follow these steps to build a Ruby binary inside your home directory.</p>
<h6 id="download-and-extract">1.8.2.1 Download and Extract</h6>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ mkdir rubygc
$ wget <the version you want from ftp://ftp.ruby-lang.org/pub/ruby>
$ tar -xzvf <ruby-version.tar.gz>
$ cd <ruby-version>
</pre>
</div>
<h6 id="apply-the-patch">1.8.2.2 Apply the Patch</h6>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ curl http://github.com/wayneeseguin/rvm/raw/master/patches/ruby/1.9.2/p180/gcdata.patch | patch -p0 # if you're on 1.9.2!
$ curl http://github.com/wayneeseguin/rvm/raw/master/patches/ruby/1.8.7/ruby187gc.patch | patch -p0 # if you're on 1.8.7!
</pre>
</div>
<h6 id="configure-and-install">1.8.2.3 Configure and Install</h6>
<p>The following will install Ruby in your home directory’s <tt>/rubygc</tt> directory. Make sure to replace <tt><homedir></tt> with a full patch to your actual home directory.</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ ./configure --prefix=/<homedir>/rubygc
$ make && make install
</pre>
</div>
<h6 id="prepare-aliases">1.8.2.4 Prepare Aliases</h6>
<p>For convenience, add the following lines in your <tt>~/.profile</tt>:</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
alias gcruby='~/rubygc/bin/ruby'
alias gcrake='~/rubygc/bin/rake'
alias gcgem='~/rubygc/bin/gem'
alias gcirb='~/rubygc/bin/irb'
alias gcrails='~/rubygc/bin/rails'
</pre>
</div>
<p>Don’t forget to use your aliases from now on.</p>
<h6 id="install-rubygems-1-8-only">1.8.2.5 Install RubyGems (1.8 only!)</h6>
<p>Download <a href="http://rubyforge.org/projects/rubygems">RubyGems</a> and install it from source. Rubygem’s <span class="caps">README</span> file should have necessary installation instructions. Please note that this step isn’t necessary if you’ve installed Ruby 1.9 and above.</p>
<h4 id="using-ruby-prof-on-mri-and-ree">1.9 Using Ruby-Prof on <span class="caps">MRI</span> and <span class="caps">REE</span></h4>
<p>Add Ruby-Prof to your applications’ Gemfile if you want to benchmark/profile under <span class="caps">MRI</span> or <span class="caps">REE</span>:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
gem 'ruby-prof', :git => 'git://github.com/wycats/ruby-prof.git'
</pre>
</div>
<p>Now run <tt>bundle install</tt> and you’re ready to go.</p>
<h3 id="command-line-tools">2 Command Line Tools</h3>
<p>Writing performance test cases could be an overkill when you are looking for one time tests. Rails ships with two command line tools that enable quick and dirty performance testing:</p>
<h4 id="benchmarker">2.1 <tt>benchmarker</tt></h4>
<p>Usage:</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
Usage: rails benchmarker 'Ruby.code' 'Ruby.more_code' ... [OPTS]
-r, --runs N Number of runs.
Default: 4
-o, --output PATH Directory to use when writing the results.
Default: tmp/performance
-m, --metrics a,b,c Metrics to use.
Default: wall_time,memory,objects,gc_runs,gc_time
</pre>
</div>
<p>Example:</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rails benchmarker 'Item.all' 'CouchItem.all' --runs 3 --metrics wall_time,memory
</pre>
</div>
<h4 id="profiler">2.2 <tt>profiler</tt></h4>
<p>Usage:</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
Usage: rails profiler 'Ruby.code' 'Ruby.more_code' ... [OPTS]
-r, --runs N Number of runs.
Default: 1
-o, --output PATH Directory to use when writing the results.
Default: tmp/performance
--metrics a,b,c Metrics to use.
Default: process_time,memory,objects
-m, --formats x,y,z Formats to output to.
Default: flat,graph_html,call_tree
</pre>
</div>
<p>Example:</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rails profiler 'Item.all' 'CouchItem.all' --runs 2 --metrics process_time --formats flat
</pre>
</div>
<div class="note"><p>Metrics and formats vary from interpreter to interpreter. Pass <tt>--help</tt> to each tool to see the defaults for your interpreter.</p></div>
<h3 id="helper-methods">3 Helper Methods</h3>
<p>Rails provides various helper methods inside Active Record, Action Controller and Action View to measure the time taken by a given piece of code. The method is called <tt>benchmark()</tt> in all the three components.</p>
<h4 id="model">3.1 Model</h4>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
Project.benchmark("Creating project") do
project = Project.create("name" => "stuff")
project.create_manager("name" => "David")
project.milestones << Milestone.all
end
</pre>
</div>
<p>This benchmarks the code enclosed in the <tt>Project.benchmark("Creating project") do...end</tt> block and prints the result to the log file:</p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
Creating project (185.3ms)
</pre>
</div>
<p>Please refer to the <a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001336"><span class="caps">API</span> docs</a> for additional options to <tt>benchmark()</tt></p>
<h4 id="controller">3.2 Controller</h4>
<p>Similarly, you could use this helper method inside <a href="http://api.rubyonrails.org/classes/ActionController/Benchmarking/ClassMethods.html#M000715">controllers</a></p>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
def process_projects
self.class.benchmark("Processing projects") do
Project.process(params[:project_ids])
Project.update_cached_projects
end
end
</pre>
</div>
<div class="note"><p><tt>benchmark</tt> is a class method inside controllers</p></div>
<h4 id="view">3.3 View</h4>
<p>And in <a href="http://api.rubyonrails.org/classes/ActionController/Benchmarking/ClassMethods.html#M000715">views</a>:</p>
<div class="code_container">
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
<% benchmark("Showing projects partial") do %>
<%= render @projects %>
<% end %>
</pre>
</div>
<h3 id="request-logging">4 Request Logging</h3>
<p>Rails log files contain very useful information about the time taken to serve each request. Here’s a typical log file entry:</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
Processing ItemsController#index (for 127.0.0.1 at 2009-01-08 03:06:39) [GET]
Rendering template within layouts/items
Rendering items/index
Completed in 5ms (View: 2, DB: 0) | 200 OK [http://0.0.0.0/items]
</pre>
</div>
<p>For this section, we’re only interested in the last line:</p>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
Completed in 5ms (View: 2, DB: 0) | 200 OK [http://0.0.0.0/items]
</pre>
</div>
<p>This data is fairly straightforward to understand. Rails uses millisecond(ms) as the metric to measure the time taken. The complete request spent 5 ms inside Rails, out of which 2 ms were spent rendering views and none was spent communication with the database. It’s safe to assume that the remaining 3 ms were spent inside the controller.</p>
<p>Michael Koziarski has an <a href="http://www.therailsway.com/2009/1/6/requests-per-second">interesting blog post</a> explaining the importance of using milliseconds as the metric.</p>
<h3 id="useful-links">5 Useful Links</h3>
<h4 id="rails-plugins-and-gems">5.1 Rails Plugins and Gems</h4>
<ul>
<li><a href="http://rails-analyzer.rubyforge.org">Rails Analyzer</a></li>
<li><a href="http://www.flyingmachinestudios.com/programming/announcing-palmist">Palmist</a></li>
<li><a href="https://github.com/josevalim/rails-footnotes/tree/master">Rails Footnotes</a></li>
<li><a href="https://github.com/dsboulder/query_reviewer/tree/master">Query Reviewer</a></li>
</ul>
<h4 id="generic-tools">5.2 Generic Tools</h4>
<ul>
<li><a href="http://www.hpl.hp.com/research/linux/httperf/">httperf</a></li>
<li><a href="http://httpd.apache.org/docs/2.2/programs/ab.html">ab</a></li>
<li><a href="http://jakarta.apache.org/jmeter/">JMeter</a></li>
<li><a href="http://kcachegrind.sourceforge.net/html/Home.html">kcachegrind</a></li>
</ul>
<h4 id="tutorials-and-documentation">5.3 Tutorials and Documentation</h4>
<ul>
<li><a href="http://ruby-prof.rubyforge.org">ruby-prof <span class="caps">API</span> Documentation</a></li>
<li><a href="http://railscasts.com/episodes/98-request-profiling">Request Profiling Railscast</a> – Outdated, but useful for understanding call graphs</li>
</ul>
<h3 id="commercial-products">6 Commercial Products</h3>
<p>Rails has been lucky to have a few companies dedicated to Rails-specific performance tools. A couple of those are:</p>
<ul>
<li><a href="http://www.newrelic.com">New Relic</a></li>
<li><a href="http://scoutapp.com">Scout</a></li>
</ul>
<h3>Feedback</h3>
<p>
You're encouraged to help improve the quality of this guide.
</p>
<p>
If you see any typos or factual errors you are confident to
patch, please clone <a href="https://github.com/lifo/docrails">docrails</a>
and push the change yourself. That branch of Rails has public write access.
Commits are still reviewed, but that happens after you've submitted your
contribution. <a href="https://github.com/lifo/docrails">docrails</a> is
cross-merged with master periodically.
</p>
<p>
You may also find incomplete content, or stuff that is not up to date.
Please do add any missing documentation for master. Check the
<a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
for style and conventions.
</p>
<p>
If for whatever reason you spot something to fix but cannot patch it yourself, please
<a href="https://github.com/rails/rails/issues">open an issue</a>.
</p>
<p>And last but not least, any kind of discussion regarding Ruby on Rails
documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
</p>
</div>
</div>
</div>
<hr class="hide" />
<div id="footer">
<div class="wrapper">
<p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0</a> License</p>
<p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
</div>
</div>
<script type="text/javascript" src="javascripts/guides.js"></script>
<script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
<script type="text/javascript">
SyntaxHighlighter.all()
</script>
</body>
</html>