railties 3.1.1 → 3.1.2.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +2382 -0
- data/guides/output/2_2_release_notes.html +565 -0
- data/guides/output/2_3_release_notes.html +713 -0
- data/guides/output/3_0_release_notes.html +652 -0
- data/guides/output/3_1_release_notes.html +670 -0
- data/guides/output/action_controller_overview.html +925 -0
- data/guides/output/action_mailer_basics.html +658 -0
- data/guides/output/action_view_overview.html +1471 -0
- data/guides/output/active_model_basics.html +349 -0
- data/guides/output/active_record_basics.html +364 -0
- data/guides/output/active_record_querying.html +1272 -0
- data/guides/output/active_record_validations_callbacks.html +1292 -0
- data/guides/output/active_resource_basics.html +252 -0
- data/guides/output/active_support_core_extensions.html +3374 -0
- data/guides/output/ajax_on_rails.html +412 -0
- data/guides/output/api_documentation_guidelines.html +317 -0
- data/guides/output/asset_pipeline.html +691 -0
- data/guides/output/association_basics.html +1742 -0
- data/guides/output/caching_with_rails.html +533 -0
- data/guides/output/command_line.html +662 -0
- data/guides/output/configuring.html +811 -0
- data/guides/output/contribute.html +216 -0
- data/guides/output/contributing_to_ruby_on_rails.html +465 -0
- data/guides/output/credits.html +210 -0
- data/guides/output/debugging_rails_applications.html +791 -0
- data/guides/output/engines.html +673 -0
- data/guides/output/form_helpers.html +850 -0
- data/guides/output/generators.html +725 -0
- data/guides/output/getting_started.html +1980 -0
- data/guides/output/i18n.html +1054 -0
- data/guides/output/images/belongs_to.png +0 -0
- data/guides/output/images/book_icon.gif +0 -0
- data/guides/output/images/bullet.gif +0 -0
- data/guides/output/images/challenge.png +0 -0
- data/guides/output/images/chapters_icon.gif +0 -0
- data/guides/output/images/check_bullet.gif +0 -0
- data/guides/output/images/credits_pic_blank.gif +0 -0
- data/guides/output/images/csrf.png +0 -0
- data/guides/output/images/customized_error_messages.png +0 -0
- data/guides/output/images/edge_badge.png +0 -0
- data/guides/output/images/error_messages.png +0 -0
- data/guides/output/images/feature_tile.gif +0 -0
- data/guides/output/images/footer_tile.gif +0 -0
- data/guides/output/images/fxn.png +0 -0
- data/guides/output/images/grey_bullet.gif +0 -0
- data/guides/output/images/habtm.png +0 -0
- data/guides/output/images/has_many.png +0 -0
- data/guides/output/images/has_many_through.png +0 -0
- data/guides/output/images/has_one.png +0 -0
- data/guides/output/images/has_one_through.png +0 -0
- data/guides/output/images/header_backdrop.png +0 -0
- data/guides/output/images/header_tile.gif +0 -0
- data/guides/output/images/i18n/demo_html_safe.png +0 -0
- data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
- data/guides/output/images/i18n/demo_translated_en.png +0 -0
- data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
- data/guides/output/images/i18n/demo_translation_missing.png +0 -0
- data/guides/output/images/i18n/demo_untranslated.png +0 -0
- data/guides/output/images/icons/README +5 -0
- data/guides/output/images/icons/callouts/1.png +0 -0
- data/guides/output/images/icons/callouts/10.png +0 -0
- data/guides/output/images/icons/callouts/11.png +0 -0
- data/guides/output/images/icons/callouts/12.png +0 -0
- data/guides/output/images/icons/callouts/13.png +0 -0
- data/guides/output/images/icons/callouts/14.png +0 -0
- data/guides/output/images/icons/callouts/15.png +0 -0
- data/guides/output/images/icons/callouts/2.png +0 -0
- data/guides/output/images/icons/callouts/3.png +0 -0
- data/guides/output/images/icons/callouts/4.png +0 -0
- data/guides/output/images/icons/callouts/5.png +0 -0
- data/guides/output/images/icons/callouts/6.png +0 -0
- data/guides/output/images/icons/callouts/7.png +0 -0
- data/guides/output/images/icons/callouts/8.png +0 -0
- data/guides/output/images/icons/callouts/9.png +0 -0
- data/guides/output/images/icons/caution.png +0 -0
- data/guides/output/images/icons/example.png +0 -0
- data/guides/output/images/icons/home.png +0 -0
- data/guides/output/images/icons/important.png +0 -0
- data/guides/output/images/icons/next.png +0 -0
- data/guides/output/images/icons/note.png +0 -0
- data/guides/output/images/icons/prev.png +0 -0
- data/guides/output/images/icons/tip.png +0 -0
- data/guides/output/images/icons/up.png +0 -0
- data/guides/output/images/icons/warning.png +0 -0
- data/guides/output/images/jaimeiniesta.jpg +0 -0
- data/guides/output/images/nav_arrow.gif +0 -0
- data/guides/output/images/polymorphic.png +0 -0
- data/guides/output/images/posts_index.png +0 -0
- data/guides/output/images/radar.png +0 -0
- data/guides/output/images/rails_guides_logo.gif +0 -0
- data/guides/output/images/rails_logo_remix.gif +0 -0
- data/guides/output/images/rails_welcome.png +0 -0
- data/guides/output/images/session_fixation.png +0 -0
- data/guides/output/images/tab_grey.gif +0 -0
- data/guides/output/images/tab_info.gif +0 -0
- data/guides/output/images/tab_note.gif +0 -0
- data/guides/output/images/tab_red.gif +0 -0
- data/guides/output/images/tab_yellow.gif +0 -0
- data/guides/output/images/tab_yellow.png +0 -0
- data/guides/output/images/validation_error_messages.png +0 -0
- data/guides/output/images/vijaydev.jpg +0 -0
- data/guides/output/index.html +300 -0
- data/guides/output/initialization.html +1087 -0
- data/guides/output/javascripts/guides.js +7 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
- data/guides/output/javascripts/syntaxhighlighter/shCore.js +17 -0
- data/guides/output/layout.html +312 -0
- data/guides/output/layouts_and_rendering.html +1257 -0
- data/guides/output/migrations.html +751 -0
- data/guides/output/nested_model_forms.html +350 -0
- data/guides/output/performance_testing.html +858 -0
- data/guides/output/plugins.html +590 -0
- data/guides/output/rails_application_templates.html +368 -0
- data/guides/output/rails_on_rack.html +408 -0
- data/guides/output/routing.html +1246 -0
- data/guides/output/ruby_on_rails_guides_guidelines.html +218 -0
- data/guides/output/security.html +968 -0
- data/guides/output/stylesheets/fixes.css +16 -0
- data/guides/output/stylesheets/main.css +445 -0
- data/guides/output/stylesheets/print.css +52 -0
- data/guides/output/stylesheets/reset.css +43 -0
- data/guides/output/stylesheets/style.css +13 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCore.css +226 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
- data/guides/output/testing.html +1182 -0
- data/guides/source/active_record_validations_callbacks.textile +3 -3
- data/guides/source/active_support_core_extensions.textile +2 -2
- data/guides/source/asset_pipeline.textile +7 -5
- data/guides/source/association_basics.textile +2 -4
- data/guides/source/command_line.textile +1 -1
- data/guides/source/configuring.textile +4 -2
- data/guides/source/contributing_to_ruby_on_rails.textile +6 -6
- data/guides/source/initialization.textile +2 -2
- data/guides/source/performance_testing.textile +1 -1
- data/guides/source/routing.textile +1 -1
- data/guides/source/security.textile +2 -2
- data/lib/rails/commands/server.rb +1 -1
- data/lib/rails/engine.rb +1 -1
- data/lib/rails/generators/app_base.rb +2 -1
- data/lib/rails/generators/base.rb +3 -3
- data/lib/rails/generators/rails/app/templates/gitignore +15 -5
- data/lib/rails/rack.rb +0 -1
- data/lib/rails/tasks/documentation.rake +7 -7
- data/lib/rails/tasks/misc.rake +0 -2
- data/lib/rails/test_unit/testing.rake +2 -0
- data/lib/rails/version.rb +2 -2
- metadata +495 -326
- data/CHANGELOG +0 -2371
- data/lib/rails/rack/content_length.rb +0 -38
@@ -0,0 +1,350 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
2
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
3
|
+
|
4
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
5
|
+
<head>
|
6
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
7
|
+
|
8
|
+
<title>Ruby on Rails Guides: Rails nested model forms</title>
|
9
|
+
|
10
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
|
11
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
|
12
|
+
|
13
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
|
14
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
|
15
|
+
|
16
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
|
17
|
+
</head>
|
18
|
+
<body class="guide">
|
19
|
+
<div id="topNav">
|
20
|
+
<div class="wrapper">
|
21
|
+
<strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
|
22
|
+
<a href="http://rubyonrails.org/">Overview</a> |
|
23
|
+
<a href="http://rubyonrails.org/download">Download</a> |
|
24
|
+
<a href="http://rubyonrails.org/deploy">Deploy</a> |
|
25
|
+
<a href="https://github.com/rails/rails">Code</a> |
|
26
|
+
<a href="http://rubyonrails.org/screencasts">Screencasts</a> |
|
27
|
+
<a href="http://rubyonrails.org/documentation">Documentation</a> |
|
28
|
+
<a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
|
29
|
+
<a href="http://rubyonrails.org/community">Community</a> |
|
30
|
+
<a href="http://weblog.rubyonrails.org/">Blog</a>
|
31
|
+
</div>
|
32
|
+
</div>
|
33
|
+
<div id="header">
|
34
|
+
<div class="wrapper clearfix">
|
35
|
+
<h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
|
36
|
+
<p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
|
37
|
+
<ul class="nav">
|
38
|
+
<li><a href="index.html">Home</a></li>
|
39
|
+
<li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
|
40
|
+
<div id="guides" class="clearfix" style="display: none;">
|
41
|
+
<hr />
|
42
|
+
<dl class="L">
|
43
|
+
<dt>Start Here</dt>
|
44
|
+
<dd><a href="getting_started.html">Getting Started with Rails</a></dd>
|
45
|
+
<dt>Models</dt>
|
46
|
+
<dd><a href="migrations.html">Rails Database Migrations</a></dd>
|
47
|
+
<dd><a href="active_record_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
|
48
|
+
<dd><a href="association_basics.html">Active Record Associations</a></dd>
|
49
|
+
<dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
|
50
|
+
<dt>Views</dt>
|
51
|
+
<dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
|
52
|
+
<dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
|
53
|
+
<dt>Controllers</dt>
|
54
|
+
<dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
|
55
|
+
<dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
|
56
|
+
</dl>
|
57
|
+
<dl class="R">
|
58
|
+
<dt>Digging Deeper</dt>
|
59
|
+
<dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
|
60
|
+
<dd><a href="i18n.html">Rails Internationalization API</a></dd>
|
61
|
+
<dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
|
62
|
+
<dd><a href="testing.html">Testing Rails Applications</a></dd>
|
63
|
+
<dd><a href="security.html">Securing Rails Applications</a></dd>
|
64
|
+
<dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
|
65
|
+
<dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
|
66
|
+
<dd><a href="configuring.html">Configuring Rails Applications</a></dd>
|
67
|
+
<dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
|
68
|
+
<dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
|
69
|
+
<dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
|
70
|
+
|
71
|
+
<dt>Extending Rails</dt>
|
72
|
+
<dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
|
73
|
+
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
|
74
|
+
<dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
|
75
|
+
|
76
|
+
<dt>Contributing to Ruby on Rails</dt>
|
77
|
+
<dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
|
78
|
+
<dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
|
79
|
+
<dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
|
80
|
+
|
81
|
+
<dt>Release Notes</dt>
|
82
|
+
<dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
|
83
|
+
<dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
|
84
|
+
<dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
|
85
|
+
<dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
|
86
|
+
</dl>
|
87
|
+
</div>
|
88
|
+
</li>
|
89
|
+
<li><a href="contributing_to_ruby_on_rails.html">Contribute</a></li>
|
90
|
+
<li><a href="credits.html">Credits</a></li>
|
91
|
+
</ul>
|
92
|
+
</div>
|
93
|
+
</div>
|
94
|
+
<hr class="hide" />
|
95
|
+
|
96
|
+
<div id="feature">
|
97
|
+
<div class="wrapper">
|
98
|
+
<h2>Rails nested model forms</h2>
|
99
|
+
<p>Creating a form for a model <em>and</em> its associations can become quite tedious. Therefore Rails provides helpers to assist in dealing with the complexities of generating these forms <em>and</em> the required <span class="caps">CRUD</span> operations to create, update, and destroy associations.</p>
|
100
|
+
<p>In this guide you will:</p>
|
101
|
+
<ul>
|
102
|
+
<li>do stuff</li>
|
103
|
+
</ul>
|
104
|
+
|
105
|
+
<div id="subCol">
|
106
|
+
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
|
107
|
+
<ol class="chapters">
|
108
|
+
<li><a href="#model-setup">Model setup</a><ul><li><a href="#activerecord-base-model">ActiveRecord::Base model</a></li> <li><a href="#custom-model">Custom model</a></li></ul></li><li><a href="#views">Views</a><ul><li><a href="#controller-code">Controller code</a></li> <li><a href="#form-code">Form code</a></li></ul></li></ol></div>
|
109
|
+
</div>
|
110
|
+
</div>
|
111
|
+
|
112
|
+
<div id="container">
|
113
|
+
<div class="wrapper">
|
114
|
+
<div id="mainCol">
|
115
|
+
<div class="note"><p>This guide assumes the user knows how to use the <a href="form_helpers.html">Rails form helpers</a> in general. Also, it’s <strong>not</strong> an <span class="caps">API</span> reference. For a complete reference please visit <a href="http://api.rubyonrails.org/">the Rails <span class="caps">API</span> documentation</a>.</p></div>
|
116
|
+
<h3 id="model-setup">1 Model setup</h3>
|
117
|
+
<p>To be able to use the nested model functionality in your forms, the model will need to support some basic operations.</p>
|
118
|
+
<p>First of all, it needs to define a writer method for the attribute that corresponds to the association you are building a nested model form for. The <tt>fields_for</tt> form helper will look for this method to decide whether or not a nested model form should be build.</p>
|
119
|
+
<p>If the associated object is an array a form builder will be yielded for each object, else only a single form builder will be yielded.</p>
|
120
|
+
<p>Consider a Person model with an associated Address. When asked to yield a nested FormBuilder for the <tt>:address</tt> attribute, the <tt>fields_for</tt> form helper will look for a method on the Person instance named <tt>address_attributes=</tt>.</p>
|
121
|
+
<h4 id="activerecord-base-model">1.1 ActiveRecord::Base model</h4>
|
122
|
+
<p>For an ActiveRecord::Base model and association this writer method is commonly defined with the <tt>accepts_nested_attributes_for</tt> class method:</p>
|
123
|
+
<h5 id="has_one">1.1.1 has_one</h5>
|
124
|
+
<div class="code_container">
|
125
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
126
|
+
class Person < ActiveRecord::Base
|
127
|
+
has_one :address
|
128
|
+
accepts_nested_attributes_for :address
|
129
|
+
end
|
130
|
+
</pre>
|
131
|
+
</div>
|
132
|
+
<h5 id="belongs_to">1.1.2 belongs_to</h5>
|
133
|
+
<div class="code_container">
|
134
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
135
|
+
class Person < ActiveRecord::Base
|
136
|
+
belongs_to :firm
|
137
|
+
accepts_nested_attributes_for :firm
|
138
|
+
end
|
139
|
+
</pre>
|
140
|
+
</div>
|
141
|
+
<h5 id="has_many-has_and_belongs_to_many">1.1.3 has_many / has_and_belongs_to_many</h5>
|
142
|
+
<div class="code_container">
|
143
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
144
|
+
class Person < ActiveRecord::Base
|
145
|
+
has_many :projects
|
146
|
+
accepts_nested_attributes_for :projects
|
147
|
+
end
|
148
|
+
</pre>
|
149
|
+
</div>
|
150
|
+
<h4 id="custom-model">1.2 Custom model</h4>
|
151
|
+
<p>As you might have inflected from this explanation, you <em>don’t</em> necessarily need an ActiveRecord::Base model to use this functionality. The following examples are sufficient to enable the nested model form behaviour:</p>
|
152
|
+
<h5 id="single-associated-object">1.2.1 Single associated object</h5>
|
153
|
+
<div class="code_container">
|
154
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
155
|
+
class Person
|
156
|
+
def address
|
157
|
+
Address.new
|
158
|
+
end
|
159
|
+
|
160
|
+
def address_attributes=(attributes)
|
161
|
+
# ...
|
162
|
+
end
|
163
|
+
end
|
164
|
+
</pre>
|
165
|
+
</div>
|
166
|
+
<h5 id="association-collection">1.2.2 Association collection</h5>
|
167
|
+
<div class="code_container">
|
168
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
169
|
+
class Person
|
170
|
+
def projects
|
171
|
+
[Project.new, Project.new]
|
172
|
+
end
|
173
|
+
|
174
|
+
def projects_attributes=(attributes)
|
175
|
+
# ...
|
176
|
+
end
|
177
|
+
end
|
178
|
+
</pre>
|
179
|
+
</div>
|
180
|
+
<div class="note"><p>See (<span class="caps">TODO</span>) in the advanced section for more information on how to deal with the <span class="caps">CRUD</span> operations in your custom model.</p></div>
|
181
|
+
<h3 id="views">2 Views</h3>
|
182
|
+
<h4 id="controller-code">2.1 Controller code</h4>
|
183
|
+
<p>A nested model form will <em>only</em> be built if the associated object(s) exist. This means that for a new model instance you would probably want to build the associated object(s) first.</p>
|
184
|
+
<p>Consider the following typical RESTful controller which will prepare a new Person instance and its <tt>address</tt> and <tt>projects</tt> associations before rendering the <tt>new</tt> template:</p>
|
185
|
+
<div class="code_container">
|
186
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
187
|
+
class PeopleController < ActionController:Base
|
188
|
+
def new
|
189
|
+
@person = Person.new
|
190
|
+
@person.built_address
|
191
|
+
2.times { @person.projects.build }
|
192
|
+
end
|
193
|
+
|
194
|
+
def create
|
195
|
+
@person = Person.new(params[:person])
|
196
|
+
if @person.save
|
197
|
+
# ...
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
</pre>
|
202
|
+
</div>
|
203
|
+
<div class="note"><p>Obviously the instantiation of the associated object(s) can become tedious and not <span class="caps">DRY</span>, so you might want to move that into the model itself. ActiveRecord::Base provides an <tt>after_initialize</tt> callback which is a good way to refactor this.</p></div>
|
204
|
+
<h4 id="form-code">2.2 Form code</h4>
|
205
|
+
<p>Now that you have a model instance, with the appropriate methods and associated object(s), you can start building the nested model form.</p>
|
206
|
+
<h5 id="standard-form">2.2.1 Standard form</h5>
|
207
|
+
<p>Start out with a regular RESTful form:</p>
|
208
|
+
<div class="code_container">
|
209
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
210
|
+
<%= form_for @person do |f| %>
|
211
|
+
<%= f.text_field :name %>
|
212
|
+
<% end %>
|
213
|
+
</pre>
|
214
|
+
</div>
|
215
|
+
<p>This will generate the following html:</p>
|
216
|
+
<div class="code_container">
|
217
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
218
|
+
<form action="/people" class="new_person" id="new_person" method="post">
|
219
|
+
<input id="person_name" name="person[name]" size="30" type="text" />
|
220
|
+
</form>
|
221
|
+
</pre>
|
222
|
+
</div>
|
223
|
+
<h5 id="nested-form-for-a-single-associated-object">2.2.2 Nested form for a single associated object</h5>
|
224
|
+
<p>Now add a nested form for the <tt>address</tt> association:</p>
|
225
|
+
<div class="code_container">
|
226
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
227
|
+
<%= form_for @person do |f| %>
|
228
|
+
<%= f.text_field :name %>
|
229
|
+
|
230
|
+
<%= f.fields_for :address do |af| %>
|
231
|
+
<%= af.text_field :street %>
|
232
|
+
<% end %>
|
233
|
+
<% end %>
|
234
|
+
</pre>
|
235
|
+
</div>
|
236
|
+
<p>This generates:</p>
|
237
|
+
<div class="code_container">
|
238
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
239
|
+
<form action="/people" class="new_person" id="new_person" method="post">
|
240
|
+
<input id="person_name" name="person[name]" size="30" type="text" />
|
241
|
+
|
242
|
+
<input id="person_address_attributes_street" name="person[address_attributes][street]" size="30" type="text" />
|
243
|
+
</form>
|
244
|
+
</pre>
|
245
|
+
</div>
|
246
|
+
<p>Notice that <tt>fields_for</tt> recognized the <tt>address</tt> as an association for which a nested model form should be built by the way it has namespaced the <tt>name</tt> attribute.</p>
|
247
|
+
<p>When this form is posted the Rails parameter parser will construct a hash like the following:</p>
|
248
|
+
<div class="code_container">
|
249
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
250
|
+
{
|
251
|
+
"person" => {
|
252
|
+
"name" => "Eloy Duran",
|
253
|
+
"address_attributes" => {
|
254
|
+
"street" => "Nieuwe Prinsengracht"
|
255
|
+
}
|
256
|
+
}
|
257
|
+
}
|
258
|
+
</pre>
|
259
|
+
</div>
|
260
|
+
<p>That’s it. The controller will simply pass this hash on to the model from the <tt>create</tt> action. The model will then handle building the <tt>address</tt> association for you and automatically save it when the parent (<tt>person</tt>) is saved.</p>
|
261
|
+
<h5 id="nested-form-for-a-collection-of-associated-objects">2.2.3 Nested form for a collection of associated objects</h5>
|
262
|
+
<p>The form code for an association collection is pretty similar to that of a single associated object:</p>
|
263
|
+
<div class="code_container">
|
264
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
265
|
+
<%= form_for @person do |f| %>
|
266
|
+
<%= f.text_field :name %>
|
267
|
+
|
268
|
+
<%= f.fields_for :projects do |pf| %>
|
269
|
+
<%= pf.text_field :name %>
|
270
|
+
<% end %>
|
271
|
+
<% end %>
|
272
|
+
</pre>
|
273
|
+
</div>
|
274
|
+
<p>Which generates:</p>
|
275
|
+
<div class="code_container">
|
276
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
277
|
+
<form action="/people" class="new_person" id="new_person" method="post">
|
278
|
+
<input id="person_name" name="person[name]" size="30" type="text" />
|
279
|
+
|
280
|
+
<input id="person_projects_attributes_0_name" name="person[projects_attributes][0][name]" size="30" type="text" />
|
281
|
+
<input id="person_projects_attributes_1_name" name="person[projects_attributes][1][name]" size="30" type="text" />
|
282
|
+
</form>
|
283
|
+
</pre>
|
284
|
+
</div>
|
285
|
+
<p>As you can see it has generated 2 <tt>project name</tt> inputs, one for each new <tt>project</tt> that was built in the controller’s <tt>new</tt> action. Only this time the <tt>name</tt> attribute of the input contains a digit as an extra namespace. This will be parsed by the Rails parameter parser as:</p>
|
286
|
+
<div class="code_container">
|
287
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
288
|
+
{
|
289
|
+
"person" => {
|
290
|
+
"name" => "Eloy Duran",
|
291
|
+
"projects_attributes" => {
|
292
|
+
"0" => { "name" => "Project 1" },
|
293
|
+
"1" => { "name" => "Project 2" }
|
294
|
+
}
|
295
|
+
}
|
296
|
+
}
|
297
|
+
</pre>
|
298
|
+
</div>
|
299
|
+
<p>You can basically see the <tt>projects_attributes</tt> hash as an array of attribute hashes, one for each model instance.</p>
|
300
|
+
<div class="note"><p>The reason that <tt>fields_for</tt> constructed a form which would result in a hash instead of an array is that it won’t work for any forms nested deeper than one level deep.</p></div>
|
301
|
+
<div class="info"><p>You <em>can</em> however pass an array to the writer method generated by <tt>accepts_nested_attributes_for</tt> if you’re using plain Ruby or some other <span class="caps">API</span> access. See (<span class="caps">TODO</span>) for more info and example.</p></div>
|
302
|
+
|
303
|
+
<h3>Feedback</h3>
|
304
|
+
<p>
|
305
|
+
You're encouraged to help improve the quality of this guide.
|
306
|
+
</p>
|
307
|
+
<p>
|
308
|
+
If you see any typos or factual errors you are confident to
|
309
|
+
patch, please clone <a href="https://github.com/lifo/docrails">docrails</a>
|
310
|
+
and push the change yourself. That branch of Rails has public write access.
|
311
|
+
Commits are still reviewed, but that happens after you've submitted your
|
312
|
+
contribution. <a href="https://github.com/lifo/docrails">docrails</a> is
|
313
|
+
cross-merged with master periodically.
|
314
|
+
</p>
|
315
|
+
<p>
|
316
|
+
You may also find incomplete content, or stuff that is not up to date.
|
317
|
+
Please do add any missing documentation for master. Check the
|
318
|
+
<a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
|
319
|
+
for style and conventions.
|
320
|
+
</p>
|
321
|
+
<p>
|
322
|
+
If for whatever reason you spot something to fix but cannot patch it yourself, please
|
323
|
+
<a href="https://github.com/rails/rails/issues">open an issue</a>.
|
324
|
+
</p>
|
325
|
+
<p>And last but not least, any kind of discussion regarding Ruby on Rails
|
326
|
+
documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
|
327
|
+
</p>
|
328
|
+
</div>
|
329
|
+
</div>
|
330
|
+
</div>
|
331
|
+
|
332
|
+
<hr class="hide" />
|
333
|
+
<div id="footer">
|
334
|
+
<div class="wrapper">
|
335
|
+
<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>
|
336
|
+
<p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
|
337
|
+
</div>
|
338
|
+
</div>
|
339
|
+
|
340
|
+
<script type="text/javascript" src="javascripts/guides.js"></script>
|
341
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
|
342
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
|
343
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
|
344
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
|
345
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
|
346
|
+
<script type="text/javascript">
|
347
|
+
SyntaxHighlighter.all()
|
348
|
+
</script>
|
349
|
+
</body>
|
350
|
+
</html>
|
@@ -0,0 +1,858 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
2
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
3
|
+
|
4
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
5
|
+
<head>
|
6
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
7
|
+
|
8
|
+
<title>Ruby on Rails Guides: Performance Testing Rails Applications</title>
|
9
|
+
|
10
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
|
11
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
|
12
|
+
|
13
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
|
14
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
|
15
|
+
|
16
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
|
17
|
+
</head>
|
18
|
+
<body class="guide">
|
19
|
+
<div id="topNav">
|
20
|
+
<div class="wrapper">
|
21
|
+
<strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
|
22
|
+
<a href="http://rubyonrails.org/">Overview</a> |
|
23
|
+
<a href="http://rubyonrails.org/download">Download</a> |
|
24
|
+
<a href="http://rubyonrails.org/deploy">Deploy</a> |
|
25
|
+
<a href="https://github.com/rails/rails">Code</a> |
|
26
|
+
<a href="http://rubyonrails.org/screencasts">Screencasts</a> |
|
27
|
+
<a href="http://rubyonrails.org/documentation">Documentation</a> |
|
28
|
+
<a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
|
29
|
+
<a href="http://rubyonrails.org/community">Community</a> |
|
30
|
+
<a href="http://weblog.rubyonrails.org/">Blog</a>
|
31
|
+
</div>
|
32
|
+
</div>
|
33
|
+
<div id="header">
|
34
|
+
<div class="wrapper clearfix">
|
35
|
+
<h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
|
36
|
+
<p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
|
37
|
+
<ul class="nav">
|
38
|
+
<li><a href="index.html">Home</a></li>
|
39
|
+
<li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
|
40
|
+
<div id="guides" class="clearfix" style="display: none;">
|
41
|
+
<hr />
|
42
|
+
<dl class="L">
|
43
|
+
<dt>Start Here</dt>
|
44
|
+
<dd><a href="getting_started.html">Getting Started with Rails</a></dd>
|
45
|
+
<dt>Models</dt>
|
46
|
+
<dd><a href="migrations.html">Rails Database Migrations</a></dd>
|
47
|
+
<dd><a href="active_record_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
|
48
|
+
<dd><a href="association_basics.html">Active Record Associations</a></dd>
|
49
|
+
<dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
|
50
|
+
<dt>Views</dt>
|
51
|
+
<dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
|
52
|
+
<dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
|
53
|
+
<dt>Controllers</dt>
|
54
|
+
<dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
|
55
|
+
<dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
|
56
|
+
</dl>
|
57
|
+
<dl class="R">
|
58
|
+
<dt>Digging Deeper</dt>
|
59
|
+
<dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
|
60
|
+
<dd><a href="i18n.html">Rails Internationalization API</a></dd>
|
61
|
+
<dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
|
62
|
+
<dd><a href="testing.html">Testing Rails Applications</a></dd>
|
63
|
+
<dd><a href="security.html">Securing Rails Applications</a></dd>
|
64
|
+
<dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
|
65
|
+
<dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
|
66
|
+
<dd><a href="configuring.html">Configuring Rails Applications</a></dd>
|
67
|
+
<dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
|
68
|
+
<dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
|
69
|
+
<dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
|
70
|
+
|
71
|
+
<dt>Extending Rails</dt>
|
72
|
+
<dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
|
73
|
+
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
|
74
|
+
<dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
|
75
|
+
|
76
|
+
<dt>Contributing to Ruby on Rails</dt>
|
77
|
+
<dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
|
78
|
+
<dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
|
79
|
+
<dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
|
80
|
+
|
81
|
+
<dt>Release Notes</dt>
|
82
|
+
<dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
|
83
|
+
<dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
|
84
|
+
<dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
|
85
|
+
<dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
|
86
|
+
</dl>
|
87
|
+
</div>
|
88
|
+
</li>
|
89
|
+
<li><a href="contributing_to_ruby_on_rails.html">Contribute</a></li>
|
90
|
+
<li><a href="credits.html">Credits</a></li>
|
91
|
+
</ul>
|
92
|
+
</div>
|
93
|
+
</div>
|
94
|
+
<hr class="hide" />
|
95
|
+
|
96
|
+
<div id="feature">
|
97
|
+
<div class="wrapper">
|
98
|
+
<h2>Performance Testing Rails Applications</h2>
|
99
|
+
<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>
|
100
|
+
<ul>
|
101
|
+
<li>Understand the various types of benchmarking and profiling metrics</li>
|
102
|
+
<li>Generate performance and benchmarking tests</li>
|
103
|
+
<li>Install and use a GC-patched Ruby binary to measure memory usage and object allocation</li>
|
104
|
+
<li>Understand the benchmarking information provided by Rails inside the log files</li>
|
105
|
+
<li>Learn about various tools facilitating benchmarking and profiling</li>
|
106
|
+
</ul>
|
107
|
+
<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>
|
108
|
+
|
109
|
+
<div id="subCol">
|
110
|
+
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
|
111
|
+
<ol class="chapters">
|
112
|
+
<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>
|
113
|
+
</div>
|
114
|
+
</div>
|
115
|
+
|
116
|
+
<div id="container">
|
117
|
+
<div class="wrapper">
|
118
|
+
<div id="mainCol">
|
119
|
+
<h3 id="performance-test-cases">1 Performance Test Cases</h3>
|
120
|
+
<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>
|
121
|
+
<p>In a freshly generated Rails application, <tt>test/performance/browsing_test.rb</tt> contains an example of a performance test:</p>
|
122
|
+
<div class="code_container">
|
123
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
124
|
+
require 'test_helper'
|
125
|
+
require 'rails/performance_test_help'
|
126
|
+
|
127
|
+
# Profiling results for each test method are written to tmp/performance.
|
128
|
+
class BrowsingTest < ActionDispatch::PerformanceTest
|
129
|
+
def test_homepage
|
130
|
+
get '/'
|
131
|
+
end
|
132
|
+
end
|
133
|
+
</pre>
|
134
|
+
</div>
|
135
|
+
<p>This example is a simple performance test case for profiling a <span class="caps">GET</span> request to the application’s homepage.</p>
|
136
|
+
<h4 id="generating-performance-tests">1.1 Generating Performance Tests</h4>
|
137
|
+
<p>Rails provides a generator called <tt>performance_test</tt> for creating new performance tests:</p>
|
138
|
+
<div class="code_container">
|
139
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
140
|
+
$ rails generate performance_test homepage
|
141
|
+
</pre>
|
142
|
+
</div>
|
143
|
+
<p>This generates <tt>homepage_test.rb</tt> in the <tt>test/performance</tt> directory:</p>
|
144
|
+
<div class="code_container">
|
145
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
146
|
+
require 'test_helper'
|
147
|
+
require 'rails/performance_test_help'
|
148
|
+
|
149
|
+
class HomepageTest < ActionDispatch::PerformanceTest
|
150
|
+
# Replace this with your real tests.
|
151
|
+
def test_homepage
|
152
|
+
get '/'
|
153
|
+
end
|
154
|
+
end
|
155
|
+
</pre>
|
156
|
+
</div>
|
157
|
+
<h4 id="examples">1.2 Examples</h4>
|
158
|
+
<p>Let’s assume your application has the following controller and model:</p>
|
159
|
+
<div class="code_container">
|
160
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
161
|
+
# routes.rb
|
162
|
+
root :to => 'home#index'
|
163
|
+
resources :posts
|
164
|
+
|
165
|
+
# home_controller.rb
|
166
|
+
class HomeController < ApplicationController
|
167
|
+
def dashboard
|
168
|
+
@users = User.last_ten.includes(:avatars)
|
169
|
+
@posts = Post.all_today
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
# posts_controller.rb
|
174
|
+
class PostsController < ApplicationController
|
175
|
+
def create
|
176
|
+
@post = Post.create(params[:post])
|
177
|
+
redirect_to(@post)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
# post.rb
|
182
|
+
class Post < ActiveRecord::Base
|
183
|
+
before_save :recalculate_costly_stats
|
184
|
+
|
185
|
+
def slow_method
|
186
|
+
# I fire gallzilion queries sleeping all around
|
187
|
+
end
|
188
|
+
|
189
|
+
private
|
190
|
+
|
191
|
+
def recalculate_costly_stats
|
192
|
+
# CPU heavy calculations
|
193
|
+
end
|
194
|
+
end
|
195
|
+
</pre>
|
196
|
+
</div>
|
197
|
+
<h5 id="controller-example">1.2.1 Controller Example</h5>
|
198
|
+
<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>
|
199
|
+
<p>Here’s the performance test for <tt>HomeController#dashboard</tt> and <tt>PostsController#create</tt>:</p>
|
200
|
+
<div class="code_container">
|
201
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
202
|
+
require 'test_helper'
|
203
|
+
require 'rails/performance_test_help'
|
204
|
+
|
205
|
+
class PostPerformanceTest < ActionDispatch::PerformanceTest
|
206
|
+
def setup
|
207
|
+
# Application requires logged-in user
|
208
|
+
login_as(:lifo)
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_homepage
|
212
|
+
get '/dashboard'
|
213
|
+
end
|
214
|
+
|
215
|
+
def test_creating_new_post
|
216
|
+
post '/posts', :post => { :body => 'lifo is fooling you' }
|
217
|
+
end
|
218
|
+
end
|
219
|
+
</pre>
|
220
|
+
</div>
|
221
|
+
<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>
|
222
|
+
<h5 id="model-example">1.2.2 Model Example</h5>
|
223
|
+
<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>
|
224
|
+
<p>Performance test for <tt>Post</tt> model:</p>
|
225
|
+
<div class="code_container">
|
226
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
227
|
+
require 'test_helper'
|
228
|
+
require 'rails/performance_test_help'
|
229
|
+
|
230
|
+
class PostModelTest < ActionDispatch::PerformanceTest
|
231
|
+
def test_creation
|
232
|
+
Post.create :body => 'still fooling you', :cost => '100'
|
233
|
+
end
|
234
|
+
|
235
|
+
def test_slow_method
|
236
|
+
# Using posts(:awesome) fixture
|
237
|
+
posts(:awesome).slow_method
|
238
|
+
end
|
239
|
+
end
|
240
|
+
</pre>
|
241
|
+
</div>
|
242
|
+
<h4 id="modes">1.3 Modes</h4>
|
243
|
+
<p>Performance tests can be run in two modes: Benchmarking and Profiling.</p>
|
244
|
+
<h5 id="benchmarking">1.3.1 Benchmarking</h5>
|
245
|
+
<p>Benchmarking makes it easy to quickly gather a few metrics about each test tun. By default, each test case is run <strong>4 times</strong> in benchmarking mode.</p>
|
246
|
+
<p>To run performance tests in benchmarking mode:</p>
|
247
|
+
<div class="code_container">
|
248
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
249
|
+
$ rake test:benchmark
|
250
|
+
</pre>
|
251
|
+
</div>
|
252
|
+
<h5 id="profiling">1.3.2 Profiling</h5>
|
253
|
+
<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>
|
254
|
+
<p>To run performance tests in profiling mode:</p>
|
255
|
+
<div class="code_container">
|
256
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
257
|
+
$ rake test:profile
|
258
|
+
</pre>
|
259
|
+
</div>
|
260
|
+
<h4 id="metrics">1.4 Metrics</h4>
|
261
|
+
<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>
|
262
|
+
<h5 id="wall-time">1.4.1 Wall Time</h5>
|
263
|
+
<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>
|
264
|
+
<h5 id="process-time">1.4.2 Process Time</h5>
|
265
|
+
<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>
|
266
|
+
<h5 id="cpu-time">1.4.3 <span class="caps">CPU</span> Time</h5>
|
267
|
+
<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>
|
268
|
+
<h5 id="user-time">1.4.4 User Time</h5>
|
269
|
+
<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>
|
270
|
+
<h5 id="memory">1.4.5 Memory</h5>
|
271
|
+
<p>Memory measures the amount of memory used for the performance test case.</p>
|
272
|
+
<h5 id="objects">1.4.6 Objects</h5>
|
273
|
+
<p>Objects measures the number of objects allocated for the performance test case.</p>
|
274
|
+
<h5 id="gc-runs">1.4.7 GC Runs</h5>
|
275
|
+
<p>GC Runs measures the number of times GC was invoked for the performance test case.</p>
|
276
|
+
<h5 id="gc-time">1.4.8 GC Time</h5>
|
277
|
+
<p>GC Time measures the amount of time spent in GC for the performance test case.</p>
|
278
|
+
<h5 id="metric-availability">1.4.9 Metric Availability</h5>
|
279
|
+
<h6 id="benchmarking_1">1.4.9.1 Benchmarking</h6>
|
280
|
+
<table>
|
281
|
+
<tr>
|
282
|
+
<th>Interpreter</th>
|
283
|
+
<th>Wall Time</th>
|
284
|
+
<th>Process Time</th>
|
285
|
+
<th><span class="caps">CPU</span> Time</th>
|
286
|
+
<th>User Time</th>
|
287
|
+
<th>Memory</th>
|
288
|
+
<th>Objects</th>
|
289
|
+
<th>GC Runs</th>
|
290
|
+
<th>GC Time</th>
|
291
|
+
</tr>
|
292
|
+
<tr>
|
293
|
+
<th><span class="caps">MRI</span> </th>
|
294
|
+
<td> yes </td>
|
295
|
+
<td> yes </td>
|
296
|
+
<td> yes </td>
|
297
|
+
<td> no </td>
|
298
|
+
<td> yes </td>
|
299
|
+
<td> yes </td>
|
300
|
+
<td> yes </td>
|
301
|
+
<td> yes </td>
|
302
|
+
</tr>
|
303
|
+
<tr>
|
304
|
+
<th><span class="caps">REE</span> </th>
|
305
|
+
<td> yes </td>
|
306
|
+
<td> yes </td>
|
307
|
+
<td> yes </td>
|
308
|
+
<td> no </td>
|
309
|
+
<td> yes </td>
|
310
|
+
<td> yes </td>
|
311
|
+
<td> yes </td>
|
312
|
+
<td> yes </td>
|
313
|
+
</tr>
|
314
|
+
<tr>
|
315
|
+
<th>Rubinius </th>
|
316
|
+
<td> yes </td>
|
317
|
+
<td> no </td>
|
318
|
+
<td> no </td>
|
319
|
+
<td> no </td>
|
320
|
+
<td> yes </td>
|
321
|
+
<td> yes </td>
|
322
|
+
<td> yes </td>
|
323
|
+
<td> yes </td>
|
324
|
+
</tr>
|
325
|
+
<tr>
|
326
|
+
<th>JRuby </th>
|
327
|
+
<td> yes </td>
|
328
|
+
<td> no </td>
|
329
|
+
<td> no </td>
|
330
|
+
<td> yes </td>
|
331
|
+
<td> yes </td>
|
332
|
+
<td> yes </td>
|
333
|
+
<td> yes </td>
|
334
|
+
<td> yes </td>
|
335
|
+
</tr>
|
336
|
+
</table>
|
337
|
+
<h6 id="profiling_1">1.4.9.2 Profiling</h6>
|
338
|
+
<table>
|
339
|
+
<tr>
|
340
|
+
<th>Interpreter</th>
|
341
|
+
<th>Wall Time</th>
|
342
|
+
<th>Process Time</th>
|
343
|
+
<th><span class="caps">CPU</span> Time</th>
|
344
|
+
<th>User Time</th>
|
345
|
+
<th>Memory</th>
|
346
|
+
<th>Objects</th>
|
347
|
+
<th>GC Runs</th>
|
348
|
+
<th>GC Time</th>
|
349
|
+
</tr>
|
350
|
+
<tr>
|
351
|
+
<th><span class="caps">MRI</span> </th>
|
352
|
+
<td> yes </td>
|
353
|
+
<td> yes </td>
|
354
|
+
<td> no </td>
|
355
|
+
<td> no </td>
|
356
|
+
<td> yes </td>
|
357
|
+
<td> yes </td>
|
358
|
+
<td> yes </td>
|
359
|
+
<td> yes </td>
|
360
|
+
</tr>
|
361
|
+
<tr>
|
362
|
+
<th><span class="caps">REE</span> </th>
|
363
|
+
<td> yes </td>
|
364
|
+
<td> yes </td>
|
365
|
+
<td> no </td>
|
366
|
+
<td> no </td>
|
367
|
+
<td> yes </td>
|
368
|
+
<td> yes </td>
|
369
|
+
<td> yes </td>
|
370
|
+
<td> yes </td>
|
371
|
+
</tr>
|
372
|
+
<tr>
|
373
|
+
<th>Rubinius </th>
|
374
|
+
<td> yes </td>
|
375
|
+
<td> no </td>
|
376
|
+
<td> no </td>
|
377
|
+
<td> no </td>
|
378
|
+
<td> no </td>
|
379
|
+
<td> no </td>
|
380
|
+
<td> no </td>
|
381
|
+
<td> no </td>
|
382
|
+
</tr>
|
383
|
+
<tr>
|
384
|
+
<th>JRuby </th>
|
385
|
+
<td> yes </td>
|
386
|
+
<td> no </td>
|
387
|
+
<td> no </td>
|
388
|
+
<td> no </td>
|
389
|
+
<td> no </td>
|
390
|
+
<td> no </td>
|
391
|
+
<td> no </td>
|
392
|
+
<td> no </td>
|
393
|
+
</tr>
|
394
|
+
</table>
|
395
|
+
<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>
|
396
|
+
<h4 id="understanding-the-output">1.5 Understanding the Output</h4>
|
397
|
+
<p>Performance tests generate different outputs inside <tt>tmp/performance</tt> directory depending on their mode and metric.</p>
|
398
|
+
<h5 id="output-benchmarking">1.5.1 Benchmarking</h5>
|
399
|
+
<p>In benchmarking mode, performance tests generate two types of outputs.</p>
|
400
|
+
<h6 id="output-command-line">1.5.1.1 Command Line</h6>
|
401
|
+
<p>This is the primary form of output in benchmarking mode. Example:</p>
|
402
|
+
<div class="code_container">
|
403
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
404
|
+
BrowsingTest#test_homepage (31 ms warmup)
|
405
|
+
wall_time: 6 ms
|
406
|
+
memory: 437.27 KB
|
407
|
+
objects: 5,514
|
408
|
+
gc_runs: 0
|
409
|
+
gc_time: 19 ms
|
410
|
+
</pre>
|
411
|
+
</div>
|
412
|
+
<h6 id="csv-files">1.5.1.2 <span class="caps">CSV</span> Files</h6>
|
413
|
+
<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>
|
414
|
+
<ul>
|
415
|
+
<li>BrowsingTest#test_homepage_gc_runs.csv</li>
|
416
|
+
<li>BrowsingTest#test_homepage_gc_time.csv</li>
|
417
|
+
<li>BrowsingTest#test_homepage_memory.csv</li>
|
418
|
+
<li>BrowsingTest#test_homepage_objects.csv</li>
|
419
|
+
<li>BrowsingTest#test_homepage_wall_time.csv</li>
|
420
|
+
</ul>
|
421
|
+
<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>
|
422
|
+
<p>Sample output of <tt>BrowsingTest#test_homepage_wall_time.csv</tt>:</p>
|
423
|
+
<div class="code_container">
|
424
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
425
|
+
measurement,created_at,app,rails,ruby,platform
|
426
|
+
0.00738224999999992,2009-01-08T03:40:29Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
427
|
+
0.00755874999999984,2009-01-08T03:46:18Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
428
|
+
0.00762099999999993,2009-01-08T03:49:25Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
429
|
+
0.00603075000000008,2009-01-08T04:03:29Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
430
|
+
0.00619899999999995,2009-01-08T04:03:53Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
431
|
+
0.00755449999999991,2009-01-08T04:04:55Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
432
|
+
0.00595999999999997,2009-01-08T04:05:06Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
433
|
+
0.00740450000000004,2009-01-09T03:54:47Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
434
|
+
0.00603150000000008,2009-01-09T03:54:57Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
435
|
+
0.00771250000000012,2009-01-09T15:46:03Z,,3.0.0,ruby-1.8.7.249,x86_64-linux
|
436
|
+
</pre>
|
437
|
+
</div>
|
438
|
+
<h5 id="output-profiling">1.5.2 Profiling</h5>
|
439
|
+
<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>
|
440
|
+
<h6 id="command-line">1.5.2.1 Command Line</h6>
|
441
|
+
<p>This is a very basic form of output in profiling mode:</p>
|
442
|
+
<div class="code_container">
|
443
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
444
|
+
BrowsingTest#test_homepage (58 ms warmup)
|
445
|
+
process_time: 63 ms
|
446
|
+
memory: 832.13 KB
|
447
|
+
objects: 7,882
|
448
|
+
</pre>
|
449
|
+
</div>
|
450
|
+
<h6 id="flat">1.5.2.2 Flat</h6>
|
451
|
+
<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>
|
452
|
+
<h6 id="graph">1.5.2.3 Graph</h6>
|
453
|
+
<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>
|
454
|
+
<h6 id="tree">1.5.2.4 Tree</h6>
|
455
|
+
<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>
|
456
|
+
<h6 id="output-availability">1.5.2.5 Output Availability</h6>
|
457
|
+
<table>
|
458
|
+
<tr>
|
459
|
+
<th></th>
|
460
|
+
<th>Flat</th>
|
461
|
+
<th>Graph</th>
|
462
|
+
<th>Tree</th>
|
463
|
+
</tr>
|
464
|
+
<tr>
|
465
|
+
<th><span class="caps">MRI</span> </th>
|
466
|
+
<td> yes </td>
|
467
|
+
<td> yes </td>
|
468
|
+
<td> yes </td>
|
469
|
+
</tr>
|
470
|
+
<tr>
|
471
|
+
<th><span class="caps">REE</span> </th>
|
472
|
+
<td> yes </td>
|
473
|
+
<td> yes </td>
|
474
|
+
<td> yes </td>
|
475
|
+
</tr>
|
476
|
+
<tr>
|
477
|
+
<th>Rubinius </th>
|
478
|
+
<td> yes </td>
|
479
|
+
<td> yes </td>
|
480
|
+
<td> no </td>
|
481
|
+
</tr>
|
482
|
+
<tr>
|
483
|
+
<th>JRuby </th>
|
484
|
+
<td> yes </td>
|
485
|
+
<td> yes </td>
|
486
|
+
<td> no </td>
|
487
|
+
</tr>
|
488
|
+
</table>
|
489
|
+
<h4 id="tuning-test-runs">1.6 Tuning Test Runs</h4>
|
490
|
+
<p>Test runs can be tuned by setting the <tt>profile_options</tt> class variable on your test class.</p>
|
491
|
+
<div class="code_container">
|
492
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
493
|
+
require 'test_helper'
|
494
|
+
require 'rails/performance_test_help'
|
495
|
+
|
496
|
+
# Profiling results for each test method are written to tmp/performance.
|
497
|
+
class BrowsingTest < ActionDispatch::PerformanceTest
|
498
|
+
self.profile_options = { :runs => 5,
|
499
|
+
:metrics => [:wall_time, :memory] }
|
500
|
+
|
501
|
+
def test_homepage
|
502
|
+
get '/'
|
503
|
+
end
|
504
|
+
end
|
505
|
+
</pre>
|
506
|
+
</div>
|
507
|
+
<p>In this example, the test would run 5 times and measure wall time and memory. There are a few configurable options:</p>
|
508
|
+
<table>
|
509
|
+
<tr>
|
510
|
+
<th>Option </th>
|
511
|
+
<th>Description</th>
|
512
|
+
<th>Default</th>
|
513
|
+
<th>Mode</th>
|
514
|
+
</tr>
|
515
|
+
<tr>
|
516
|
+
<td><tt>:runs</tt> </td>
|
517
|
+
<td>Number of runs.</td>
|
518
|
+
<td>Benchmarking: 4, Profiling: 1</td>
|
519
|
+
<td>Both</td>
|
520
|
+
</tr>
|
521
|
+
<tr>
|
522
|
+
<td><tt>:output</tt> </td>
|
523
|
+
<td>Directory to use when writing the results.</td>
|
524
|
+
<td><tt>tmp/performance</tt></td>
|
525
|
+
<td>Both</td>
|
526
|
+
</tr>
|
527
|
+
<tr>
|
528
|
+
<td><tt>:metrics</tt> </td>
|
529
|
+
<td>Metrics to use.</td>
|
530
|
+
<td>See below.</td>
|
531
|
+
<td>Both</td>
|
532
|
+
</tr>
|
533
|
+
<tr>
|
534
|
+
<td><tt>:formats</tt> </td>
|
535
|
+
<td>Formats to output to.</td>
|
536
|
+
<td>See below.</td>
|
537
|
+
<td>Profiling</td>
|
538
|
+
</tr>
|
539
|
+
</table>
|
540
|
+
<p>Metrics and formats have different defaults depending on the interpreter in use.</p>
|
541
|
+
<table>
|
542
|
+
<tr>
|
543
|
+
<th>Interpreter</th>
|
544
|
+
<th>Mode</th>
|
545
|
+
<th>Default metrics</th>
|
546
|
+
<th>Default formats</th>
|
547
|
+
</tr>
|
548
|
+
<tr>
|
549
|
+
<td rowspan="2"><span class="caps">MRI</span>/<span class="caps">REE</span> </td>
|
550
|
+
<td>Benchmarking</td>
|
551
|
+
<td><tt>[:wall_time, :memory, :objects, :gc_runs, :gc_time]</tt></td>
|
552
|
+
<td>N/A</td>
|
553
|
+
</tr>
|
554
|
+
<tr>
|
555
|
+
<td>Profiling </td>
|
556
|
+
<td><tt>[:process_time, :memory, :objects]</tt></td>
|
557
|
+
<td><tt>[:flat, :graph_html, :call_tree, :call_stack]</tt></td>
|
558
|
+
</tr>
|
559
|
+
<tr>
|
560
|
+
<td rowspan="2">Rubinius</td>
|
561
|
+
<td>Benchmarking</td>
|
562
|
+
<td><tt>[:wall_time, :memory, :objects, :gc_runs, :gc_time]</tt></td>
|
563
|
+
<td>N/A</td>
|
564
|
+
</tr>
|
565
|
+
<tr>
|
566
|
+
<td>Profiling </td>
|
567
|
+
<td><tt>[:wall_time]</tt></td>
|
568
|
+
<td><tt>[:flat, :graph]</tt></td>
|
569
|
+
</tr>
|
570
|
+
<tr>
|
571
|
+
<td rowspan="2">JRuby </td>
|
572
|
+
<td>Benchmarking</td>
|
573
|
+
<td><tt>[:wall_time, :user_time, :memory, :gc_runs, :gc_time]</tt></td>
|
574
|
+
<td>N/A</td>
|
575
|
+
</tr>
|
576
|
+
<tr>
|
577
|
+
<td>Profiling </td>
|
578
|
+
<td><tt>[:wall_time]</tt></td>
|
579
|
+
<td><tt>[:flat, :graph]</tt></td>
|
580
|
+
</tr>
|
581
|
+
</table>
|
582
|
+
<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>
|
583
|
+
<h4 id="performance-test-environment">1.7 Performance Test Environment</h4>
|
584
|
+
<p>Performance tests are run in the <tt>test</tt> environment. But running performance tests will set the following configuration parameters:</p>
|
585
|
+
<div class="code_container">
|
586
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
587
|
+
ActionController::Base.perform_caching = true
|
588
|
+
ActiveSupport::Dependencies.mechanism = :require
|
589
|
+
Rails.logger.level = ActiveSupport::BufferedLogger::INFO
|
590
|
+
</pre>
|
591
|
+
</div>
|
592
|
+
<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>
|
593
|
+
<h4 id="installing-gc-patched-mri">1.8 Installing GC-Patched <span class="caps">MRI</span></h4>
|
594
|
+
<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>
|
595
|
+
<p>The recommended patches for each <span class="caps">MRI</span> version are:</p>
|
596
|
+
<table>
|
597
|
+
<tr>
|
598
|
+
<th>Version</th>
|
599
|
+
<th>Patch</th>
|
600
|
+
</tr>
|
601
|
+
<tr>
|
602
|
+
<td>1.8.6</td>
|
603
|
+
<td>ruby186gc</td>
|
604
|
+
</tr>
|
605
|
+
<tr>
|
606
|
+
<td>1.8.7</td>
|
607
|
+
<td>ruby187gc</td>
|
608
|
+
</tr>
|
609
|
+
<tr>
|
610
|
+
<td>1.9.2 and above</td>
|
611
|
+
<td>gcdata</td>
|
612
|
+
</tr>
|
613
|
+
</table>
|
614
|
+
<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>
|
615
|
+
<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>
|
616
|
+
<h5 id="install-using-rvm">1.8.1 Install Using <span class="caps">RVM</span></h5>
|
617
|
+
<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>
|
618
|
+
<div class="code_container">
|
619
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
620
|
+
$ rvm install 1.9.2-p180 --patch gcdata
|
621
|
+
$ rvm install 1.8.7 --patch ruby187gc
|
622
|
+
$ rvm install 1.9.2-p180 --patch ~/Downloads/downloaded_gcdata_patch.patch
|
623
|
+
</pre>
|
624
|
+
</div>
|
625
|
+
<p>You can even keep your regular interpreter by assigning a name to the patched one:</p>
|
626
|
+
<div class="code_container">
|
627
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
628
|
+
$ rvm install 1.9.2-p180 --patch gcdata --name gcdata
|
629
|
+
$ rvm use 1.9.2-p180 # your regular ruby
|
630
|
+
$ rvm use 1.9.2-p180-gcdata # your patched ruby
|
631
|
+
</pre>
|
632
|
+
</div>
|
633
|
+
<p>And it’s done! You have installed a patched Ruby interpreter.</p>
|
634
|
+
<h5 id="install-from-source">1.8.2 Install From Source</h5>
|
635
|
+
<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>
|
636
|
+
<h6 id="download-and-extract">1.8.2.1 Download and Extract</h6>
|
637
|
+
<div class="code_container">
|
638
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
639
|
+
$ mkdir rubygc
|
640
|
+
$ wget <the version you want from ftp://ftp.ruby-lang.org/pub/ruby>
|
641
|
+
$ tar -xzvf <ruby-version.tar.gz>
|
642
|
+
$ cd <ruby-version>
|
643
|
+
</pre>
|
644
|
+
</div>
|
645
|
+
<h6 id="apply-the-patch">1.8.2.2 Apply the Patch</h6>
|
646
|
+
<div class="code_container">
|
647
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
648
|
+
$ 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!
|
649
|
+
$ curl http://github.com/wayneeseguin/rvm/raw/master/patches/ruby/1.8.7/ruby187gc.patch | patch -p0 # if you're on 1.8.7!
|
650
|
+
</pre>
|
651
|
+
</div>
|
652
|
+
<h6 id="configure-and-install">1.8.2.3 Configure and Install</h6>
|
653
|
+
<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>
|
654
|
+
<div class="code_container">
|
655
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
656
|
+
$ ./configure --prefix=/<homedir>/rubygc
|
657
|
+
$ make && make install
|
658
|
+
</pre>
|
659
|
+
</div>
|
660
|
+
<h6 id="prepare-aliases">1.8.2.4 Prepare Aliases</h6>
|
661
|
+
<p>For convenience, add the following lines in your <tt>~/.profile</tt>:</p>
|
662
|
+
<div class="code_container">
|
663
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
664
|
+
alias gcruby='~/rubygc/bin/ruby'
|
665
|
+
alias gcrake='~/rubygc/bin/rake'
|
666
|
+
alias gcgem='~/rubygc/bin/gem'
|
667
|
+
alias gcirb='~/rubygc/bin/irb'
|
668
|
+
alias gcrails='~/rubygc/bin/rails'
|
669
|
+
</pre>
|
670
|
+
</div>
|
671
|
+
<p>Don’t forget to use your aliases from now on.</p>
|
672
|
+
<h6 id="install-rubygems-1-8-only">1.8.2.5 Install RubyGems (1.8 only!)</h6>
|
673
|
+
<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>
|
674
|
+
<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>
|
675
|
+
<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>
|
676
|
+
<div class="code_container">
|
677
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
678
|
+
gem 'ruby-prof', :path => 'git://github.com/wycats/ruby-prof.git'
|
679
|
+
</pre>
|
680
|
+
</div>
|
681
|
+
<p>Now run <tt>bundle install</tt> and you’re ready to go.</p>
|
682
|
+
<h3 id="command-line-tools">2 Command Line Tools</h3>
|
683
|
+
<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>
|
684
|
+
<h4 id="benchmarker">2.1 <tt>benchmarker</tt></h4>
|
685
|
+
<p>Usage:</p>
|
686
|
+
<div class="code_container">
|
687
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
688
|
+
Usage: rails benchmarker 'Ruby.code' 'Ruby.more_code' ... [OPTS]
|
689
|
+
-r, --runs N Number of runs.
|
690
|
+
Default: 4
|
691
|
+
-o, --output PATH Directory to use when writing the results.
|
692
|
+
Default: tmp/performance
|
693
|
+
-m, --metrics a,b,c Metrics to use.
|
694
|
+
Default: wall_time,memory,objects,gc_runs,gc_time
|
695
|
+
</pre>
|
696
|
+
</div>
|
697
|
+
<p>Example:</p>
|
698
|
+
<div class="code_container">
|
699
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
700
|
+
$ rails benchmarker 'Item.all' 'CouchItem.all' --runs 3 --metrics wall_time,memory
|
701
|
+
</pre>
|
702
|
+
</div>
|
703
|
+
<h4 id="profiler">2.2 <tt>profiler</tt></h4>
|
704
|
+
<p>Usage:</p>
|
705
|
+
<div class="code_container">
|
706
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
707
|
+
Usage: rails profiler 'Ruby.code' 'Ruby.more_code' ... [OPTS]
|
708
|
+
-r, --runs N Number of runs.
|
709
|
+
Default: 1
|
710
|
+
-o, --output PATH Directory to use when writing the results.
|
711
|
+
Default: tmp/performance
|
712
|
+
--metrics a,b,c Metrics to use.
|
713
|
+
Default: process_time,memory,objects
|
714
|
+
-m, --formats x,y,z Formats to output to.
|
715
|
+
Default: flat,graph_html,call_tree
|
716
|
+
</pre>
|
717
|
+
</div>
|
718
|
+
<p>Example:</p>
|
719
|
+
<div class="code_container">
|
720
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
721
|
+
$ rails profiler 'Item.all' 'CouchItem.all' --runs 2 --metrics process_time --formats flat
|
722
|
+
</pre>
|
723
|
+
</div>
|
724
|
+
<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>
|
725
|
+
<h3 id="helper-methods">3 Helper Methods</h3>
|
726
|
+
<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>
|
727
|
+
<h4 id="model">3.1 Model</h4>
|
728
|
+
<div class="code_container">
|
729
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
730
|
+
Project.benchmark("Creating project") do
|
731
|
+
project = Project.create("name" => "stuff")
|
732
|
+
project.create_manager("name" => "David")
|
733
|
+
project.milestones << Milestone.all
|
734
|
+
end
|
735
|
+
</pre>
|
736
|
+
</div>
|
737
|
+
<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>
|
738
|
+
<div class="code_container">
|
739
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
740
|
+
Creating project (185.3ms)
|
741
|
+
</pre>
|
742
|
+
</div>
|
743
|
+
<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>
|
744
|
+
<h4 id="controller">3.2 Controller</h4>
|
745
|
+
<p>Similarly, you could use this helper method inside <a href="http://api.rubyonrails.org/classes/ActionController/Benchmarking/ClassMethods.html#M000715">controllers</a></p>
|
746
|
+
<div class="code_container">
|
747
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
748
|
+
def process_projects
|
749
|
+
self.class.benchmark("Processing projects") do
|
750
|
+
Project.process(params[:project_ids])
|
751
|
+
Project.update_cached_projects
|
752
|
+
end
|
753
|
+
end
|
754
|
+
</pre>
|
755
|
+
</div>
|
756
|
+
<div class="note"><p><tt>benchmark</tt> is a class method inside controllers</p></div>
|
757
|
+
<h4 id="view">3.3 View</h4>
|
758
|
+
<p>And in <a href="http://api.rubyonrails.org/classes/ActionController/Benchmarking/ClassMethods.html#M000715">views</a>:</p>
|
759
|
+
<div class="code_container">
|
760
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
761
|
+
<% benchmark("Showing projects partial") do %>
|
762
|
+
<%= render @projects %>
|
763
|
+
<% end %>
|
764
|
+
</pre>
|
765
|
+
</div>
|
766
|
+
<h3 id="request-logging">4 Request Logging</h3>
|
767
|
+
<p>Rails log files contain very useful information about the time taken to serve each request. Here’s a typical log file entry:</p>
|
768
|
+
<div class="code_container">
|
769
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
770
|
+
Processing ItemsController#index (for 127.0.0.1 at 2009-01-08 03:06:39) [GET]
|
771
|
+
Rendering template within layouts/items
|
772
|
+
Rendering items/index
|
773
|
+
Completed in 5ms (View: 2, DB: 0) | 200 OK [http://0.0.0.0/items]
|
774
|
+
</pre>
|
775
|
+
</div>
|
776
|
+
<p>For this section, we’re only interested in the last line:</p>
|
777
|
+
<div class="code_container">
|
778
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
779
|
+
Completed in 5ms (View: 2, DB: 0) | 200 OK [http://0.0.0.0/items]
|
780
|
+
</pre>
|
781
|
+
</div>
|
782
|
+
<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>
|
783
|
+
<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>
|
784
|
+
<h3 id="useful-links">5 Useful Links</h3>
|
785
|
+
<h4 id="rails-plugins-and-gems">5.1 Rails Plugins and Gems</h4>
|
786
|
+
<ul>
|
787
|
+
<li><a href="http://rails-analyzer.rubyforge.org">Rails Analyzer</a></li>
|
788
|
+
<li><a href="http://www.flyingmachinestudios.com/programming/announcing-palmist">Palmist</a></li>
|
789
|
+
<li><a href="https://github.com/josevalim/rails-footnotes/tree/master">Rails Footnotes</a></li>
|
790
|
+
<li><a href="https://github.com/dsboulder/query_reviewer/tree/master">Query Reviewer</a></li>
|
791
|
+
</ul>
|
792
|
+
<h4 id="generic-tools">5.2 Generic Tools</h4>
|
793
|
+
<ul>
|
794
|
+
<li><a href="http://www.hpl.hp.com/research/linux/httperf/">httperf</a></li>
|
795
|
+
<li><a href="http://httpd.apache.org/docs/2.2/programs/ab.html">ab</a></li>
|
796
|
+
<li><a href="http://jakarta.apache.org/jmeter/">JMeter</a></li>
|
797
|
+
<li><a href="http://kcachegrind.sourceforge.net/html/Home.html">kcachegrind</a></li>
|
798
|
+
</ul>
|
799
|
+
<h4 id="tutorials-and-documentation">5.3 Tutorials and Documentation</h4>
|
800
|
+
<ul>
|
801
|
+
<li><a href="http://ruby-prof.rubyforge.org">ruby-prof <span class="caps">API</span> Documentation</a></li>
|
802
|
+
<li><a href="http://railscasts.com/episodes/98-request-profiling">Request Profiling Railscast</a> – Outdated, but useful for understanding call graphs</li>
|
803
|
+
</ul>
|
804
|
+
<h3 id="commercial-products">6 Commercial Products</h3>
|
805
|
+
<p>Rails has been lucky to have a few companies dedicated to Rails-specific performance tools. A couple of those are:</p>
|
806
|
+
<ul>
|
807
|
+
<li><a href="http://www.newrelic.com">New Relic</a></li>
|
808
|
+
<li><a href="http://scoutapp.com">Scout</a></li>
|
809
|
+
</ul>
|
810
|
+
|
811
|
+
<h3>Feedback</h3>
|
812
|
+
<p>
|
813
|
+
You're encouraged to help improve the quality of this guide.
|
814
|
+
</p>
|
815
|
+
<p>
|
816
|
+
If you see any typos or factual errors you are confident to
|
817
|
+
patch, please clone <a href="https://github.com/lifo/docrails">docrails</a>
|
818
|
+
and push the change yourself. That branch of Rails has public write access.
|
819
|
+
Commits are still reviewed, but that happens after you've submitted your
|
820
|
+
contribution. <a href="https://github.com/lifo/docrails">docrails</a> is
|
821
|
+
cross-merged with master periodically.
|
822
|
+
</p>
|
823
|
+
<p>
|
824
|
+
You may also find incomplete content, or stuff that is not up to date.
|
825
|
+
Please do add any missing documentation for master. Check the
|
826
|
+
<a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
|
827
|
+
for style and conventions.
|
828
|
+
</p>
|
829
|
+
<p>
|
830
|
+
If for whatever reason you spot something to fix but cannot patch it yourself, please
|
831
|
+
<a href="https://github.com/rails/rails/issues">open an issue</a>.
|
832
|
+
</p>
|
833
|
+
<p>And last but not least, any kind of discussion regarding Ruby on Rails
|
834
|
+
documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
|
835
|
+
</p>
|
836
|
+
</div>
|
837
|
+
</div>
|
838
|
+
</div>
|
839
|
+
|
840
|
+
<hr class="hide" />
|
841
|
+
<div id="footer">
|
842
|
+
<div class="wrapper">
|
843
|
+
<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>
|
844
|
+
<p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
|
845
|
+
</div>
|
846
|
+
</div>
|
847
|
+
|
848
|
+
<script type="text/javascript" src="javascripts/guides.js"></script>
|
849
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
|
850
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
|
851
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
|
852
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
|
853
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
|
854
|
+
<script type="text/javascript">
|
855
|
+
SyntaxHighlighter.all()
|
856
|
+
</script>
|
857
|
+
</body>
|
858
|
+
</html>
|