This repository has been archived by the owner on Jan 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathperformance_testing.html
682 lines (626 loc) · 31.8 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
<!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" />
</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="http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/overview">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>
<p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
<ul class="nav">
<li><a href="index.html">홈</a></li>
<li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">목차</a>
<div id="guides" class="clearfix" style="display: none;">
<hr />
<dl class="L">
<dt>시작</dt>
<dd><a href="getting_started.html">레일즈 시작하기</a></dd>
<dt>모델(Models)</dt>
<dd><a href="migrations.html">레일즈 데이터베이스 마이그레이션</a></dd>
<dd><a href="active_record_validations_callbacks.html">액티브 레코드 데이터 검증(Validation)과 Callback(콜백)</a></dd>
<dd><a href="association_basics.html">액티브 레코드 Association(관계)</a></dd>
<dd><a href="active_record_querying.html">액티브 레코드 쿼리 인터페이스</a></dd>
<dt>뷰(Views)</dt>
<dd><a href="layouts_and_rendering.html">레이아웃(Layouts)과 렌더링(Rendering)</a></dd>
<dd><a href="form_helpers.html">액션 뷰 폼 핼퍼(Action View Form Helpers)</a></dd>
<dt>컨트롤러(Controllers)</dt>
<dd><a href="action_controller_overview.html">액션 컨트롤러 둘러보기</a></dd>
<dd><a href="routing.html">외부 요청에 대한 레일즈 라우팅</a></dd>
</dl>
<dl class="R">
<dt>심화내용</dt>
<dd><a href="active_support_core_extensions.html">액티브 서포트(Active Support) 확장(Core Extensions)</a></dd>
<dd><a href="i18n.html">레일즈 국제화I(nternationalization) API</a></dd>
<dd><a href="action_mailer_basics.html">액션 메일러의 기본</a></dd>
<dd><a href="testing.html">레일즈 어플리케이션 테스트하기</a></dd>
<dd><a href="security.html">레일즈 어플리케이션의 보안</a></dd>
<dd><a href="debugging_rails_applications.html">레일즈 어플리케이션 디버깅</a></dd>
<dd><a href="performance_testing.html">레일즈 어플리케이션 성능 테스트하기</a></dd>
<dd><a href="configuring.html">레일즈 어플리케이션 설정</a></dd>
<dd><a href="command_line.html">레일즈 커멘드라인 도구와 Rake 테스크</a></dd>
<dd><a href="caching_with_rails.html">레일즈를 이용한 캐싱</a></dd>
<dt>레일즈 확장하기(Extending Rails)</dt>
<dd><a href="plugins.html">레일즈 플러그인 작성의 기본</a></dd>
<dd><a href="rails_on_rack.html">렉 위의 레일즈(Rails on Rack)</a></dd>
<dd><a href="generators.html">레일즈 제너레이터(Generator) 제작과 수정</a></dd>
<dt>루비 온 레이즈에 기여하기</dt>
<dd><a href="contributing_to_ruby_on_rails.html">루비 온 레이즈에 기여하기</a></dd>
<dd><a href="api_documentation_guidelines.html">API 문서 가이드라인</a></dd>
<dd><a href="ruby_on_rails_guides_guidelines.html">루비 온 레일즈 가이드에 대한 가이드라인</a></dd>
<dt>Release Notes</dt>
<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="contribute.html">기여하기</a></li>
<li><a href="credits.html">수고하신 분들</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>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-ruby">Installing GC-Patched Ruby</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><li><a href="#changelog">Changelog</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>
<notextile>
<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 < ActionController::PerformanceTest
def test_homepage
get '/'
end
end
</pre>
</div>
</notextile>
<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>test_unit:performance</tt> for creating new performance tests:</p>
<notextile>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
rails generate test_unit:performance homepage
</pre>
</div>
</notextile>
<p>This generates <tt>homepage_test.rb</tt> in the <tt>test/performance</tt> directory:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
require 'test_helper'
require 'rails/performance_test_help'
class HomepageTest < ActionController::PerformanceTest
# Replace this with your real tests.
def test_homepage
get '/'
end
end
</pre>
</div>
</notextile>
<h4 id="examples">1.2 Examples</h4>
<p>Let’s assume your application has the following controller and model:</p>
<notextile>
<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(:include => :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>
</notextile>
<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>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
require 'test_helper'
require 'rails/performance_test_help'
class PostPerformanceTest < ActionController::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>
</notextile>
<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>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
require 'test_helper'
require 'rails/performance_test_help'
class PostModelTest < ActionController::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>
</notextile>
<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 helps find out how fast each performance test runs. Each test case is run <tt>4 times</tt> in benchmarking mode.</p>
<p>To run performance tests in benchmarking mode:</p>
<notextile>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rake test:benchmark
</pre>
</div>
</notextile>
<h5 id="profiling">1.3.2 Profiling</h5>
<p>Profiling helps you see the details of a performance test and provide an in-depth picture of the slow and memory hungry parts. Each test case is run <tt>1 time</tt> in profiling mode.</p>
<p>To run performance tests in profiling mode:</p>
<notextile>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rake test:profile
</pre>
</div>
</notextile>
<h4 id="metrics">1.4 Metrics</h4>
<p>Benchmarking and profiling run performance tests in various modes described 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>
<p>Mode: Benchmarking</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>
<p>Mode: Profiling</p>
<h5 id="memory">1.4.3 Memory</h5>
<p>Memory measures the amount of memory used for the performance test case.</p>
<p>Mode: Benchmarking, Profiling <a href="#installing-gc-patched-ruby">Requires GC Patched Ruby</a></p>
<h5 id="objects">1.4.4 Objects</h5>
<p>Objects measures the number of objects allocated for the performance test case.</p>
<p>Mode: Benchmarking, Profiling <a href="#installing-gc-patched-ruby">Requires GC Patched Ruby</a></p>
<h5 id="gc-runs">1.4.5 GC Runs</h5>
<p>GC Runs measures the number of times GC was invoked for the performance test case.</p>
<p>Mode: Benchmarking <a href="#installing-gc-patched-ruby">Requires GC Patched Ruby</a></p>
<h5 id="gc-time">1.4.6 GC Time</h5>
<p>GC Time measures the amount of time spent in GC for the performance test case.</p>
<p>Mode: Benchmarking <a href="#installing-gc-patched-ruby">Requires GC Patched Ruby</a></p>
<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>
<notextile>
<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: 5514
gc_runs: 0
gc_time: 19 ms
</pre>
</div>
</notextile>
<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>
<notextile>
<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>
</notextile>
<h5 id="output-profiling">1.5.2 Profiling</h5>
<p>In profiling mode, you can choose from four types of output.</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>
<notextile>
<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: 7882
</pre>
</div>
</notextile>
<h6 id="flat">1.5.2.2 Flat</h6>
<p>Flat output shows the total amount of time spent 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 how long each method takes to run, 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>
<h4 id="tuning-test-runs">1.6 Tuning Test Runs</h4>
<p>By default, each performance test is run <tt>4 times</tt> in benchmarking mode and <tt>1 time</tt> in profiling. However, test runs can easily be configured.</p>
<div class='warning'><p>Performance test configurability is not yet enabled in Rails. But it will be soon.</p></div>
<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>
<notextile>
<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>
</notextile>
<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-ruby">1.8 Installing GC-Patched Ruby</h4>
<p>To get the best from Rails performance tests, you need to build a special Ruby binary with some super powers – <a href="http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch">GC patch</a> for measuring GC Runs/Time and memory/object allocation.</p>
<p>The process is fairly straightforward. If you’ve never compiled a Ruby binary before, follow these steps to build a ruby binary inside your home directory:</p>
<h5 id="installation">1.8.1 Installation</h5>
<p>Compile Ruby and apply this <a href="http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch">GC Patch</a>.</p>
<h5 id="download-and-extract">1.8.2 Download and Extract</h5>
<notextile>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
[lifo@null ~]$ mkdir rubygc
[lifo@null ~]$ wget <download the latest stable ruby from ftp://ftp.ruby-lang.org/pub/ruby>
[lifo@null ~]$ tar -xzvf <ruby-version.tar.gz>
[lifo@null ~]$ cd <ruby-version>
</pre>
</div>
</notextile>
<h5 id="apply-the-patch">1.8.3 Apply the Patch</h5>
<notextile>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
[lifo@null ruby-version]$ curl http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch | patch -p0
</pre>
</div>
</notextile>
<h5 id="configure-and-install">1.8.4 Configure and Install</h5>
<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>
<notextile>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
[lifo@null ruby-version]$ ./configure --prefix=/<homedir>/rubygc
[lifo@null ruby-version]$ make && make install
</pre>
</div>
</notextile>
<h5 id="prepare-aliases">1.8.5 Prepare Aliases</h5>
<p>For convenience, add the following lines in your <tt>~/.profile</tt>:</p>
<notextile>
<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>
</notextile>
<h5 id="install-rubygems-and-dependency-gems">1.8.6 Install Rubygems and Dependency Gems</h5>
<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.</p>
<p>Additionally, install the following gems:</p>
<ul>
<li><tt>rake</tt></li>
<li><tt>rails</tt></li>
<li><tt>ruby-prof</tt></li>
<li><tt>rack</tt></li>
<li><tt>mysql</tt></li>
</ul>
<p>If installing <tt>mysql</tt> fails, you can try to install it manually:</p>
<notextile>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
[lifo@null mysql]$ gcruby extconf.rb --with-mysql-config
[lifo@null mysql]$ make && make install
</pre>
</div>
</notextile>
<p>And you’re ready to go. Don’t forget to use <tt>gcruby</tt> and <tt>gcrake</tt> aliases when running the performance tests.</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><tt>benchmarker</tt> is a wrapper around Ruby’s <a href="http://ruby-doc.org/core/classes/Benchmark.html">Benchmark</a> standard library.</p>
<p>Usage:</p>
<notextile>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rails benchmarker [times] 'Person.expensive_way' 'Person.another_expensive_way' ...
</pre>
</div>
</notextile>
<p>Examples:</p>
<notextile>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rails benchmarker 10 'Item.all' 'CouchItem.all'
</pre>
</div>
</notextile>
<p>If the <tt>[times]</tt> argument is omitted, supplied methods are run just once:</p>
<notextile>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rails benchmarker 'Item.first' 'Item.last'
</pre>
</div>
</notextile>
<h4 id="profiler">2.2 <tt>profiler</tt></h4>
<p><tt>profiler</tt> is a wrapper around the <a href="http://ruby-prof.rubyforge.org">ruby-prof</a> gem.</p>
<p>Usage:</p>
<notextile>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rails profiler 'Person.expensive_method(10)' [times] [flat|graph|graph_html]
</pre>
</div>
</notextile>
<p>Examples:</p>
<notextile>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rails profiler 'Item.all'
</pre>
</div>
</notextile>
<p>This will profile <tt>Item.all</tt> in <tt>RubyProf::WALL_TIME</tt> measure mode. By default, it prints flat output to the shell.</p>
<notextile>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rails profiler 'Item.all' 10 graph
</pre>
</div>
</notextile>
<p>This will profile <tt>10.times { Item.all }</tt> with <tt>RubyProf::WALL_TIME</tt> measure mode and print graph output to the shell.</p>
<p>If you want to store the output in a file:</p>
<notextile>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
$ rails profiler 'Item.all' 10 graph 2> graph.txt
</pre>
</div>
</notextile>
<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>
<notextile>
<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>
</notextile>
<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>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
Creating project (185.3ms)
</pre>
</div>
</notextile>
<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>
<notextile>
<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>
</notextile>
<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>
<notextile>
<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>
</notextile>
<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>
<notextile>
<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>
</notextile>
<p>For this section, we’re only interested in the last line:</p>
<notextile>
<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>
</notextile>
<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/projects/">Palmist</a></li>
<li><a href="http://github.com/josevalim/rails-footnotes/tree/master">Rails Footnotes</a></li>
<li><a href="http://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 two startups dedicated to Rails specific performance tools:</p>
<ul>
<li><a href="http://www.newrelic.com">New Relic</a></li>
<li><a href="http://scoutapp.com">Scout</a></li>
</ul>
<h3 id="changelog">7 Changelog</h3>
<ul>
<li>January 9, 2009: Complete rewrite by <a href="credits.html#lifo">Pratik</a></li>
<li>September 6, 2008: Initial version by Matthew Bergman</li>
</ul>
<h3>Feedback</h3>
<p>
You're encouraged to help in keeping 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>
Issues may also be reported in <a href="https://github.com/lifo/docrails/issues">Github</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>