rails 2.3.8 → 2.3.9.pre
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rails might be problematic. Click here for more details.
- data/CHANGELOG +3 -2
- data/Rakefile +6 -7
- data/builtin/rails_info/rails/info.rb +1 -1
- data/environments/boot.rb +6 -2
- data/environments/environment.rb +1 -1
- data/lib/initializer.rb +44 -20
- data/lib/rails/plugin.rb +1 -1
- data/lib/rails/plugin/loader.rb +3 -3
- data/lib/rails/version.rb +1 -1
- data/lib/rails_generator/generators/components/migration/migration_generator.rb +1 -1
- data/lib/rails_generator/generators/components/plugin/templates/test_helper.rb +2 -1
- metadata +25 -124
- data/guides/files/javascripts/code_highlighter.js +0 -188
- data/guides/files/javascripts/guides.js +0 -8
- data/guides/files/javascripts/highlighters.js +0 -90
- data/guides/files/stylesheets/main.css +0 -441
- data/guides/files/stylesheets/print.css +0 -52
- data/guides/files/stylesheets/reset.css +0 -43
- data/guides/files/stylesheets/style.css +0 -13
- data/guides/files/stylesheets/syntax.css +0 -31
- data/guides/images/belongs_to.png +0 -0
- data/guides/images/book_icon.gif +0 -0
- data/guides/images/bullet.gif +0 -0
- data/guides/images/chapters_icon.gif +0 -0
- data/guides/images/check_bullet.gif +0 -0
- data/guides/images/credits_pic_blank.gif +0 -0
- data/guides/images/csrf.png +0 -0
- data/guides/images/customized_error_messages.png +0 -0
- data/guides/images/error_messages.png +0 -0
- data/guides/images/feature_tile.gif +0 -0
- data/guides/images/footer_tile.gif +0 -0
- data/guides/images/fxn.jpg +0 -0
- data/guides/images/grey_bullet.gif +0 -0
- data/guides/images/habtm.png +0 -0
- data/guides/images/has_many.png +0 -0
- data/guides/images/has_many_through.png +0 -0
- data/guides/images/has_one.png +0 -0
- data/guides/images/has_one_through.png +0 -0
- data/guides/images/header_backdrop.png +0 -0
- data/guides/images/header_tile.gif +0 -0
- data/guides/images/i18n/demo_localized_pirate.png +0 -0
- data/guides/images/i18n/demo_translated_en.png +0 -0
- data/guides/images/i18n/demo_translated_pirate.png +0 -0
- data/guides/images/i18n/demo_translation_missing.png +0 -0
- data/guides/images/i18n/demo_untranslated.png +0 -0
- data/guides/images/icons/README +0 -5
- data/guides/images/icons/callouts/1.png +0 -0
- data/guides/images/icons/callouts/10.png +0 -0
- data/guides/images/icons/callouts/11.png +0 -0
- data/guides/images/icons/callouts/12.png +0 -0
- data/guides/images/icons/callouts/13.png +0 -0
- data/guides/images/icons/callouts/14.png +0 -0
- data/guides/images/icons/callouts/15.png +0 -0
- data/guides/images/icons/callouts/2.png +0 -0
- data/guides/images/icons/callouts/3.png +0 -0
- data/guides/images/icons/callouts/4.png +0 -0
- data/guides/images/icons/callouts/5.png +0 -0
- data/guides/images/icons/callouts/6.png +0 -0
- data/guides/images/icons/callouts/7.png +0 -0
- data/guides/images/icons/callouts/8.png +0 -0
- data/guides/images/icons/callouts/9.png +0 -0
- data/guides/images/icons/caution.png +0 -0
- data/guides/images/icons/example.png +0 -0
- data/guides/images/icons/home.png +0 -0
- data/guides/images/icons/important.png +0 -0
- data/guides/images/icons/next.png +0 -0
- data/guides/images/icons/note.png +0 -0
- data/guides/images/icons/prev.png +0 -0
- data/guides/images/icons/tip.png +0 -0
- data/guides/images/icons/up.png +0 -0
- data/guides/images/icons/warning.png +0 -0
- data/guides/images/nav_arrow.gif +0 -0
- data/guides/images/polymorphic.png +0 -0
- data/guides/images/posts_index.png +0 -0
- data/guides/images/rails_guides_logo.gif +0 -0
- data/guides/images/rails_logo_remix.gif +0 -0
- data/guides/images/rails_welcome.png +0 -0
- data/guides/images/session_fixation.png +0 -0
- data/guides/images/tab_grey.gif +0 -0
- data/guides/images/tab_info.gif +0 -0
- data/guides/images/tab_note.gif +0 -0
- data/guides/images/tab_red.gif +0 -0
- data/guides/images/tab_yellow.gif +0 -0
- data/guides/images/tab_yellow.png +0 -0
- data/guides/images/validation_error_messages.png +0 -0
- data/guides/rails_guides.rb +0 -42
- data/guides/rails_guides/generator.rb +0 -138
- data/guides/rails_guides/helpers.rb +0 -34
- data/guides/rails_guides/indexer.rb +0 -55
- data/guides/rails_guides/textile_extensions.rb +0 -41
- data/guides/source/2_2_release_notes.textile +0 -422
- data/guides/source/2_3_release_notes.textile +0 -610
- data/guides/source/action_controller_overview.textile +0 -776
- data/guides/source/action_mailer_basics.textile +0 -424
- data/guides/source/active_record_basics.textile +0 -135
- data/guides/source/active_record_querying.textile +0 -969
- data/guides/source/activerecord_validations_callbacks.textile +0 -1086
- data/guides/source/association_basics.textile +0 -1781
- data/guides/source/caching_with_rails.textile +0 -524
- data/guides/source/command_line.textile +0 -589
- data/guides/source/configuring.textile +0 -234
- data/guides/source/contribute.textile +0 -71
- data/guides/source/contributing_to_rails.textile +0 -239
- data/guides/source/credits.erb.textile +0 -52
- data/guides/source/debugging_rails_applications.textile +0 -709
- data/guides/source/form_helpers.textile +0 -766
- data/guides/source/getting_started.textile +0 -1297
- data/guides/source/i18n.textile +0 -912
- data/guides/source/index.erb.textile +0 -124
- data/guides/source/layout.html.erb +0 -103
- data/guides/source/layouts_and_rendering.textile +0 -979
- data/guides/source/migrations.textile +0 -591
- data/guides/source/nested_model_forms.textile +0 -222
- data/guides/source/performance_testing.textile +0 -531
- data/guides/source/plugins.textile +0 -1512
- data/guides/source/rails_on_rack.textile +0 -309
- data/guides/source/routing.textile +0 -903
- data/guides/source/security.textile +0 -986
- data/guides/source/testing.textile +0 -951
@@ -1,222 +0,0 @@
|
|
1
|
-
h2. Rails nested model forms
|
2
|
-
|
3
|
-
Creating a form for a model _and_ its associations can become quite tedious. Therefor Rails provides helpers to assist in dealing with the complexities of generating these forms _and_ the required CRUD operations to create, update, and destroy associations.
|
4
|
-
|
5
|
-
In this guide you will:
|
6
|
-
|
7
|
-
* do stuff
|
8
|
-
|
9
|
-
endprologue.
|
10
|
-
|
11
|
-
NOTE: This guide assumes the user knows how to use the "Rails form helpers":form_helpers.html in general. Also, it’s *not* an API reference. For a complete reference please visit "the Rails API documentation":http://api.rubyonrails.org/.
|
12
|
-
|
13
|
-
|
14
|
-
h3. Model setup
|
15
|
-
|
16
|
-
To be able to use the nested model functionality in your forms, the model will need to support some basic operations.
|
17
|
-
|
18
|
-
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 +fields_for+ form helper will look for this method to decide whether or not a nested model form should be build.
|
19
|
-
|
20
|
-
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.
|
21
|
-
|
22
|
-
Consider a Person model with an associated Address. When asked to yield a nested FormBuilder for the +:address+ attribute, the +fields_for+ form helper will look for a method on the Person instance named +address_attributes=+.
|
23
|
-
|
24
|
-
h4. ActiveRecord::Base model
|
25
|
-
|
26
|
-
For an ActiveRecord::Base model and association this writer method is commonly defined with the +accepts_nested_attributes_for+ class method:
|
27
|
-
|
28
|
-
h5. has_one
|
29
|
-
|
30
|
-
<ruby>
|
31
|
-
class Person < ActiveRecord::Base
|
32
|
-
has_one :address
|
33
|
-
accepts_nested_attributes_for :address
|
34
|
-
end
|
35
|
-
</ruby>
|
36
|
-
|
37
|
-
h5. belongs_to
|
38
|
-
|
39
|
-
<ruby>
|
40
|
-
class Person < ActiveRecord::Base
|
41
|
-
belongs_to :firm
|
42
|
-
accepts_nested_attributes_for :firm
|
43
|
-
end
|
44
|
-
</ruby>
|
45
|
-
|
46
|
-
h5. has_many / has_and_belongs_to_many
|
47
|
-
|
48
|
-
<ruby>
|
49
|
-
class Person < ActiveRecord::Base
|
50
|
-
has_many :projects
|
51
|
-
accepts_nested_attributes_for :projects
|
52
|
-
end
|
53
|
-
</ruby>
|
54
|
-
|
55
|
-
h4. Custom model
|
56
|
-
|
57
|
-
As you might have inflected from this explanation, you _don’t_ necessarily need an ActiveRecord::Base model to use this functionality. The following examples are sufficient to enable the nested model form behaviour:
|
58
|
-
|
59
|
-
h5. Single associated object
|
60
|
-
|
61
|
-
<ruby>
|
62
|
-
class Person
|
63
|
-
def address
|
64
|
-
Address.new
|
65
|
-
end
|
66
|
-
|
67
|
-
def address_attributes=(attributes)
|
68
|
-
# ...
|
69
|
-
end
|
70
|
-
end
|
71
|
-
</ruby>
|
72
|
-
|
73
|
-
h5. Association collection
|
74
|
-
|
75
|
-
<ruby>
|
76
|
-
class Person
|
77
|
-
def projects
|
78
|
-
[Project.new, Project.new]
|
79
|
-
end
|
80
|
-
|
81
|
-
def projects_attributes=(attributes)
|
82
|
-
# ...
|
83
|
-
end
|
84
|
-
end
|
85
|
-
</ruby>
|
86
|
-
|
87
|
-
NOTE: See (TODO) in the advanced section for more information on how to deal with the CRUD operations in your custom model.
|
88
|
-
|
89
|
-
h3. Views
|
90
|
-
|
91
|
-
h4. Controller code
|
92
|
-
|
93
|
-
A nested model form will _only_ be build 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.
|
94
|
-
|
95
|
-
Consider the following typical RESTful controller which will prepare a new Person instance and its +address+ and +projects+ associations before rendering the +new+ template:
|
96
|
-
|
97
|
-
<ruby>
|
98
|
-
class PeopleController < ActionController:Base
|
99
|
-
def new
|
100
|
-
@person = Person.new
|
101
|
-
@person.built_address
|
102
|
-
2.times { @person.projects.build }
|
103
|
-
end
|
104
|
-
|
105
|
-
def create
|
106
|
-
@person = Person.new(params[:person])
|
107
|
-
if @person.save
|
108
|
-
# ...
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
</ruby>
|
113
|
-
|
114
|
-
NOTE: Obviously the instantiation of the associated object(s) can become tedious and not DRY, so you might want to move that into the model itself. ActiveRecord::Base provides an +after_initialize+ callback which is a good way to refactor this.
|
115
|
-
|
116
|
-
h4. Form code
|
117
|
-
|
118
|
-
Now that you have a model instance, with the appropriate methods and associated object(s), you can start building the nested model form.
|
119
|
-
|
120
|
-
h5. Standard form
|
121
|
-
|
122
|
-
Start out with a regular RESTful form:
|
123
|
-
|
124
|
-
<erb>
|
125
|
-
<% form_for @person do |f| %>
|
126
|
-
<%= f.text_field :name %>
|
127
|
-
<% end %>
|
128
|
-
</erb>
|
129
|
-
|
130
|
-
This will generate the following html:
|
131
|
-
|
132
|
-
<html>
|
133
|
-
<form action="/people" class="new_person" id="new_person" method="post">
|
134
|
-
<input id="person_name" name="person[name]" size="30" type="text" />
|
135
|
-
</form>
|
136
|
-
</html>
|
137
|
-
|
138
|
-
h5. Nested form for a single associated object
|
139
|
-
|
140
|
-
Now add a nested form for the +address+ association:
|
141
|
-
|
142
|
-
<erb>
|
143
|
-
<% form_for @person do |f| %>
|
144
|
-
<%= f.text_field :name %>
|
145
|
-
|
146
|
-
<% f.fields_for :address do |af| %>
|
147
|
-
<%= f.text_field :street %>
|
148
|
-
<% end %>
|
149
|
-
<% end %>
|
150
|
-
</erb>
|
151
|
-
|
152
|
-
This generates:
|
153
|
-
|
154
|
-
<html>
|
155
|
-
<form action="/people" class="new_person" id="new_person" method="post">
|
156
|
-
<input id="person_name" name="person[name]" size="30" type="text" />
|
157
|
-
|
158
|
-
<input id="person_address_attributes_street" name="person[address_attributes][street]" size="30" type="text" />
|
159
|
-
</form>
|
160
|
-
</html>
|
161
|
-
|
162
|
-
Notice that +fields_for+ recognized the +address+ as an association for which a nested model form should be build by the way it has namespaced the +name+ attribute.
|
163
|
-
|
164
|
-
When this form is posted the Rails parameter parser will construct a hash like the following:
|
165
|
-
|
166
|
-
<ruby>
|
167
|
-
{
|
168
|
-
"person" => {
|
169
|
-
"name" => "Eloy Duran",
|
170
|
-
"address_attributes" => {
|
171
|
-
"street" => "Nieuwe Prinsengracht"
|
172
|
-
}
|
173
|
-
}
|
174
|
-
}
|
175
|
-
</ruby>
|
176
|
-
|
177
|
-
That’s it. The controller will simply pass this hash on to the model from the +create+ action. The model will then handle building the +address+ association for you and automatically save it when the parent (+person+) is saved.
|
178
|
-
|
179
|
-
h5. Nested form for a collection of associated objects
|
180
|
-
|
181
|
-
The form code for an association collection is pretty similar to that of a single associated object:
|
182
|
-
|
183
|
-
<erb>
|
184
|
-
<% form_for @person do |f| %>
|
185
|
-
<%= f.text_field :name %>
|
186
|
-
|
187
|
-
<% f.fields_for :projects do |pf| %>
|
188
|
-
<%= f.text_field :name %>
|
189
|
-
<% end %>
|
190
|
-
<% end %>
|
191
|
-
</erb>
|
192
|
-
|
193
|
-
Which generates:
|
194
|
-
|
195
|
-
<html>
|
196
|
-
<form action="/people" class="new_person" id="new_person" method="post">
|
197
|
-
<input id="person_name" name="person[name]" size="30" type="text" />
|
198
|
-
|
199
|
-
<input id="person_projects_attributes_0_name" name="person[projects_attributes][0][name]" size="30" type="text" />
|
200
|
-
<input id="person_projects_attributes_1_name" name="person[projects_attributes][1][name]" size="30" type="text" />
|
201
|
-
</form>
|
202
|
-
</html>
|
203
|
-
|
204
|
-
As you can see it has generated 2 +project name+ inputs, one for each new +project+ that’s build in the controllers +new+ action. Only this time the +name+ attribute of the input contains a digit as an extra namespace. This will be parsed by the Rails parameter parser as:
|
205
|
-
|
206
|
-
<ruby>
|
207
|
-
{
|
208
|
-
"person" => {
|
209
|
-
"name" => "Eloy Duran",
|
210
|
-
"projects_attributes" => {
|
211
|
-
"0" => { "name" => "Project 1" },
|
212
|
-
"1" => { "name" => "Project 2" }
|
213
|
-
}
|
214
|
-
}
|
215
|
-
}
|
216
|
-
</ruby>
|
217
|
-
|
218
|
-
You can basically see the +projects_attributes+ hash as an array of attribute hashes. One for each model instance.
|
219
|
-
|
220
|
-
NOTE: The reason that +fields_for+ 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.
|
221
|
-
|
222
|
-
TIP: You _can_ however pass an array to the writer method generated by +accepts_nested_attributes_for+ if you're using plain Ruby or some other API access. See (TODO) for more info and example.
|
@@ -1,531 +0,0 @@
|
|
1
|
-
h2. Performance Testing Rails Applications
|
2
|
-
|
3
|
-
This guide covers the various ways of performance testing a Ruby on Rails application. By referring to this guide, you will be able to:
|
4
|
-
|
5
|
-
* Understand the various types of benchmarking and profiling metrics
|
6
|
-
* Generate performance and benchmarking tests
|
7
|
-
* Use a GC-patched Ruby binary to measure memory usage and object allocation
|
8
|
-
* Understand the benchmarking information provided by Rails inside the log files
|
9
|
-
* Learn about various tools facilitating benchmarking and profiling
|
10
|
-
|
11
|
-
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.
|
12
|
-
|
13
|
-
endprologue.
|
14
|
-
|
15
|
-
h3. Performance Test Cases
|
16
|
-
|
17
|
-
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.
|
18
|
-
|
19
|
-
In a freshly generated Rails application, +test/performance/browsing_test.rb+ contains an example of a performance test:
|
20
|
-
|
21
|
-
<ruby>
|
22
|
-
require 'test_helper'
|
23
|
-
require 'performance_test_help'
|
24
|
-
|
25
|
-
# Profiling results for each test method are written to tmp/performance.
|
26
|
-
class BrowsingTest < ActionController::PerformanceTest
|
27
|
-
def test_homepage
|
28
|
-
get '/'
|
29
|
-
end
|
30
|
-
end
|
31
|
-
</ruby>
|
32
|
-
|
33
|
-
This example is a simple performance test case for profiling a GET request to the application's homepage.
|
34
|
-
|
35
|
-
h4. Generating Performance Tests
|
36
|
-
|
37
|
-
Rails provides a generator called +performance_test+ for creating new performance tests:
|
38
|
-
|
39
|
-
<shell>
|
40
|
-
script/generate performance_test homepage
|
41
|
-
</shell>
|
42
|
-
|
43
|
-
This generates +homepage_test.rb+ in the +test/performance+ directory:
|
44
|
-
|
45
|
-
<ruby>
|
46
|
-
require 'test_helper'
|
47
|
-
require 'performance_test_help'
|
48
|
-
|
49
|
-
class HomepageTest < ActionController::PerformanceTest
|
50
|
-
# Replace this with your real tests.
|
51
|
-
def test_homepage
|
52
|
-
get '/'
|
53
|
-
end
|
54
|
-
end
|
55
|
-
</ruby>
|
56
|
-
|
57
|
-
h4. Examples
|
58
|
-
|
59
|
-
Let's assume your application has the following controller and model:
|
60
|
-
|
61
|
-
<ruby>
|
62
|
-
# routes.rb
|
63
|
-
map.root :controller => 'home'
|
64
|
-
map.resources :posts
|
65
|
-
|
66
|
-
# home_controller.rb
|
67
|
-
class HomeController < ApplicationController
|
68
|
-
def dashboard
|
69
|
-
@users = User.last_ten(:include => :avatars)
|
70
|
-
@posts = Post.all_today
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
# posts_controller.rb
|
75
|
-
class PostsController < ApplicationController
|
76
|
-
def create
|
77
|
-
@post = Post.create(params[:post])
|
78
|
-
redirect_to(@post)
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
# post.rb
|
83
|
-
class Post < ActiveRecord::Base
|
84
|
-
before_save :recalculate_costly_stats
|
85
|
-
|
86
|
-
def slow_method
|
87
|
-
# I fire gallzilion queries sleeping all around
|
88
|
-
end
|
89
|
-
|
90
|
-
private
|
91
|
-
|
92
|
-
def recalculate_costly_stats
|
93
|
-
# CPU heavy calculations
|
94
|
-
end
|
95
|
-
end
|
96
|
-
</ruby>
|
97
|
-
|
98
|
-
h5. Controller Example
|
99
|
-
|
100
|
-
Because performance tests are a special kind of integration test, you can use the +get+ and +post+ methods in them.
|
101
|
-
|
102
|
-
Here's the performance test for +HomeController#dashboard+ and +PostsController#create+:
|
103
|
-
|
104
|
-
<ruby>
|
105
|
-
require 'test_helper'
|
106
|
-
require 'performance_test_help'
|
107
|
-
|
108
|
-
class PostPerformanceTest < ActionController::PerformanceTest
|
109
|
-
def setup
|
110
|
-
# Application requires logged-in user
|
111
|
-
login_as(:lifo)
|
112
|
-
end
|
113
|
-
|
114
|
-
def test_homepage
|
115
|
-
get '/dashboard'
|
116
|
-
end
|
117
|
-
|
118
|
-
def test_creating_new_post
|
119
|
-
post '/posts', :post => { :body => 'lifo is fooling you' }
|
120
|
-
end
|
121
|
-
end
|
122
|
-
</ruby>
|
123
|
-
|
124
|
-
You can find more details about the +get+ and +post+ methods in the "Testing Rails Applications":testing.html guide.
|
125
|
-
|
126
|
-
h5. Model Example
|
127
|
-
|
128
|
-
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.
|
129
|
-
|
130
|
-
Performance test for +Post+ model:
|
131
|
-
|
132
|
-
<ruby>
|
133
|
-
require 'test_helper'
|
134
|
-
require 'performance_test_help'
|
135
|
-
|
136
|
-
class PostModelTest < ActionController::PerformanceTest
|
137
|
-
def test_creation
|
138
|
-
Post.create :body => 'still fooling you', :cost => '100'
|
139
|
-
end
|
140
|
-
|
141
|
-
def test_slow_method
|
142
|
-
# Using posts(:awesome) fixture
|
143
|
-
posts(:awesome).slow_method
|
144
|
-
end
|
145
|
-
end
|
146
|
-
</ruby>
|
147
|
-
|
148
|
-
h4. Modes
|
149
|
-
|
150
|
-
Performance tests can be run in two modes: Benchmarking and Profiling.
|
151
|
-
|
152
|
-
h5. Benchmarking
|
153
|
-
|
154
|
-
Benchmarking helps find out how fast each performance test runs. Each test case is run +4 times+ in benchmarking mode.
|
155
|
-
|
156
|
-
To run performance tests in benchmarking mode:
|
157
|
-
|
158
|
-
<shell>
|
159
|
-
$ rake test:benchmark
|
160
|
-
</shell>
|
161
|
-
|
162
|
-
h5. Profiling
|
163
|
-
|
164
|
-
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 +1 time+ in profiling mode.
|
165
|
-
|
166
|
-
To run performance tests in profiling mode:
|
167
|
-
|
168
|
-
<shell>
|
169
|
-
$ rake test:profile
|
170
|
-
</shell>
|
171
|
-
|
172
|
-
h4. Metrics
|
173
|
-
|
174
|
-
Benchmarking and profiling run performance tests in various modes described below.
|
175
|
-
|
176
|
-
h5. Wall Time
|
177
|
-
|
178
|
-
Wall time measures the real world time elapsed during the test run. It is affected by any other processes concurrently running on the system.
|
179
|
-
|
180
|
-
Mode: Benchmarking
|
181
|
-
|
182
|
-
h5. Process Time
|
183
|
-
|
184
|
-
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.
|
185
|
-
|
186
|
-
Mode: Profiling
|
187
|
-
|
188
|
-
h5. Memory
|
189
|
-
|
190
|
-
Memory measures the amount of memory used for the performance test case.
|
191
|
-
|
192
|
-
Mode: Benchmarking, Profiling "Requires GC Patched Ruby":#installing-gc-patched-ruby
|
193
|
-
|
194
|
-
h5. Objects
|
195
|
-
|
196
|
-
Objects measures the number of objects allocated for the performance test case.
|
197
|
-
|
198
|
-
Mode: Benchmarking, Profiling "Requires GC Patched Ruby":#installing-gc-patched-ruby
|
199
|
-
|
200
|
-
h5. GC Runs
|
201
|
-
|
202
|
-
GC Runs measures the number of times GC was invoked for the performance test case.
|
203
|
-
|
204
|
-
Mode: Benchmarking "Requires GC Patched Ruby":#installing-gc-patched-ruby
|
205
|
-
|
206
|
-
h5. GC Time
|
207
|
-
|
208
|
-
GC Time measures the amount of time spent in GC for the performance test case.
|
209
|
-
|
210
|
-
Mode: Benchmarking "Requires GC Patched Ruby":#installing-gc-patched-ruby
|
211
|
-
|
212
|
-
h4. Understanding the Output
|
213
|
-
|
214
|
-
Performance tests generate different outputs inside +tmp/performance+ directory depending on their mode and metric.
|
215
|
-
|
216
|
-
h5. Benchmarking
|
217
|
-
|
218
|
-
In benchmarking mode, performance tests generate two types of outputs:
|
219
|
-
|
220
|
-
h6. Command Line
|
221
|
-
|
222
|
-
This is the primary form of output in benchmarking mode. Example:
|
223
|
-
|
224
|
-
<shell>
|
225
|
-
BrowsingTest#test_homepage (31 ms warmup)
|
226
|
-
wall_time: 6 ms
|
227
|
-
memory: 437.27 KB
|
228
|
-
objects: 5514
|
229
|
-
gc_runs: 0
|
230
|
-
gc_time: 19 ms
|
231
|
-
</shell>
|
232
|
-
|
233
|
-
h6. CSV Files
|
234
|
-
|
235
|
-
Performance test results are also appended to +.csv+ files inside +tmp/performance+. For example, running the default +BrowsingTest#test_homepage+ will generate following five files:
|
236
|
-
|
237
|
-
* BrowsingTest#test_homepage_gc_runs.csv
|
238
|
-
* BrowsingTest#test_homepage_gc_time.csv
|
239
|
-
* BrowsingTest#test_homepage_memory.csv
|
240
|
-
* BrowsingTest#test_homepage_objects.csv
|
241
|
-
* BrowsingTest#test_homepage_wall_time.csv
|
242
|
-
|
243
|
-
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.
|
244
|
-
|
245
|
-
Sample output of +BrowsingTest#test_homepage_wall_time.csv+:
|
246
|
-
|
247
|
-
<shell>
|
248
|
-
measurement,created_at,app,rails,ruby,platform
|
249
|
-
0.00738224999999992,2009-01-08T03:40:29Z,,2.3.0.master.0744148,ruby-1.8.6.110,i686-darwin9.0.0
|
250
|
-
0.00755874999999984,2009-01-08T03:46:18Z,,2.3.0.master.0744148,ruby-1.8.6.110,i686-darwin9.0.0
|
251
|
-
0.00762099999999993,2009-01-08T03:49:25Z,,2.3.0.master.0744148,ruby-1.8.6.110,i686-darwin9.0.0
|
252
|
-
0.00603075000000008,2009-01-08T04:03:29Z,,2.3.0.master.0744148,ruby-1.8.6.111,i686-darwin9.1.0
|
253
|
-
0.00619899999999995,2009-01-08T04:03:53Z,,2.3.0.master.0744148,ruby-1.8.6.111,i686-darwin9.1.0
|
254
|
-
0.00755449999999991,2009-01-08T04:04:55Z,,2.3.0.master.0744148,ruby-1.8.6.110,i686-darwin9.0.0
|
255
|
-
0.00595999999999997,2009-01-08T04:05:06Z,,2.3.0.master.0744148,ruby-1.8.6.111,i686-darwin9.1.0
|
256
|
-
0.00740450000000004,2009-01-09T03:54:47Z,,2.3.0.master.859e150,ruby-1.8.6.110,i686-darwin9.0.0
|
257
|
-
0.00603150000000008,2009-01-09T03:54:57Z,,2.3.0.master.859e150,ruby-1.8.6.111,i686-darwin9.1.0
|
258
|
-
0.00771250000000012,2009-01-09T15:46:03Z,,2.3.0.master.859e150,ruby-1.8.6.110,i686-darwin9.0.0
|
259
|
-
</shell>
|
260
|
-
|
261
|
-
h5. Profiling
|
262
|
-
|
263
|
-
In profiling mode, you can choose from four types of output.
|
264
|
-
|
265
|
-
h6. Command Line
|
266
|
-
|
267
|
-
This is a very basic form of output in profiling mode:
|
268
|
-
|
269
|
-
<shell>
|
270
|
-
BrowsingTest#test_homepage (58 ms warmup)
|
271
|
-
process_time: 63 ms
|
272
|
-
memory: 832.13 KB
|
273
|
-
objects: 7882
|
274
|
-
</shell>
|
275
|
-
|
276
|
-
h6. Flat
|
277
|
-
|
278
|
-
Flat output shows the total amount of time spent in each method. "Check ruby prof documentation for a better explanation":http://ruby-prof.rubyforge.org/files/examples/flat_txt.html.
|
279
|
-
|
280
|
-
h6. Graph
|
281
|
-
|
282
|
-
Graph output shows how long each method takes to run, which methods call it and which methods it calls. "Check ruby prof documentation for a better explanation":http://ruby-prof.rubyforge.org/files/examples/graph_txt.html.
|
283
|
-
|
284
|
-
h6. Tree
|
285
|
-
|
286
|
-
Tree output is profiling information in calltree format for use by "kcachegrind":http://kcachegrind.sourceforge.net/html/Home.html and similar tools.
|
287
|
-
|
288
|
-
h4. Tuning Test Runs
|
289
|
-
|
290
|
-
By default, each performance test is run +4 times+ in benchmarking mode and +1 time+ in profiling. However, test runs can easily be configured.
|
291
|
-
|
292
|
-
WARNING: Performance test configurability is not yet enabled in Rails. But it will be soon.
|
293
|
-
|
294
|
-
h4. Performance Test Environment
|
295
|
-
|
296
|
-
Performance tests are run in the +development+ environment. But running performance tests will set the following configuration parameters:
|
297
|
-
|
298
|
-
<shell>
|
299
|
-
ActionController::Base.perform_caching = true
|
300
|
-
ActiveSupport::Dependencies.mechanism = :require
|
301
|
-
Rails.logger.level = ActiveSupport::BufferedLogger::INFO
|
302
|
-
</shell>
|
303
|
-
|
304
|
-
As +ActionController::Base.perform_caching+ is set to +true+, performance tests will behave much as they do in the +production+ environment.
|
305
|
-
|
306
|
-
h4. Installing GC-Patched Ruby
|
307
|
-
|
308
|
-
To get the best from Rails performance tests, you need to build a special Ruby binary with some super powers - "GC patch":http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch for measuring GC Runs/Time and memory/object allocation.
|
309
|
-
|
310
|
-
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:
|
311
|
-
|
312
|
-
h5. Installation
|
313
|
-
|
314
|
-
Compile Ruby and apply this "GC Patch":http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch.
|
315
|
-
|
316
|
-
h5. Download and Extract
|
317
|
-
|
318
|
-
<shell>
|
319
|
-
[lifo@null ~]$ mkdir rubygc
|
320
|
-
[lifo@null ~]$ wget <download the latest stable ruby from ftp://ftp.ruby-lang.org/pub/ruby>
|
321
|
-
[lifo@null ~]$ tar -xzvf <ruby-version.tar.gz>
|
322
|
-
[lifo@null ~]$ cd <ruby-version>
|
323
|
-
</shell>
|
324
|
-
|
325
|
-
h5. Apply the Patch
|
326
|
-
|
327
|
-
<shell>
|
328
|
-
[lifo@null ruby-version]$ curl http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch | patch -p0
|
329
|
-
</shell>
|
330
|
-
|
331
|
-
h5. Configure and Install
|
332
|
-
|
333
|
-
The following will install ruby in your home directory's +/rubygc+ directory. Make sure to replace +<homedir>+ with a full patch to your actual home directory.
|
334
|
-
|
335
|
-
<shell>
|
336
|
-
[lifo@null ruby-version]$ ./configure --prefix=/<homedir>/rubygc
|
337
|
-
[lifo@null ruby-version]$ make && make install
|
338
|
-
</shell>
|
339
|
-
|
340
|
-
h5. Prepare Aliases
|
341
|
-
|
342
|
-
For convenience, add the following lines in your +~/.profile+:
|
343
|
-
|
344
|
-
<shell>
|
345
|
-
alias gcruby='~/rubygc/bin/ruby'
|
346
|
-
alias gcrake='~/rubygc/bin/rake'
|
347
|
-
alias gcgem='~/rubygc/bin/gem'
|
348
|
-
alias gcirb='~/rubygc/bin/irb'
|
349
|
-
alias gcrails='~/rubygc/bin/rails'
|
350
|
-
</shell>
|
351
|
-
|
352
|
-
h5. Install Rubygems and Dependency Gems
|
353
|
-
|
354
|
-
Download "Rubygems":http://rubyforge.org/projects/rubygems and install it from source. Rubygem's README file should have necessary installation instructions.
|
355
|
-
|
356
|
-
Additionally, install the following gems:
|
357
|
-
|
358
|
-
* +rake+
|
359
|
-
* +rails+
|
360
|
-
* +ruby-prof+
|
361
|
-
* +rack+
|
362
|
-
* +mysql+
|
363
|
-
|
364
|
-
If installing +mysql+ fails, you can try to install it manually:
|
365
|
-
|
366
|
-
<shell>
|
367
|
-
[lifo@null mysql]$ gcruby extconf.rb --with-mysql-config
|
368
|
-
[lifo@null mysql]$ make && make install
|
369
|
-
</shell>
|
370
|
-
|
371
|
-
And you're ready to go. Don't forget to use +gcruby+ and +gcrake+ aliases when running the performance tests.
|
372
|
-
|
373
|
-
h3. Command Line Tools
|
374
|
-
|
375
|
-
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:
|
376
|
-
|
377
|
-
h4. +benchmarker+
|
378
|
-
|
379
|
-
+benchmarker+ is a wrapper around Ruby's "Benchmark":http://ruby-doc.org/core/classes/Benchmark.html standard library.
|
380
|
-
|
381
|
-
Usage:
|
382
|
-
|
383
|
-
<shell>
|
384
|
-
$ script/performance/benchmarker [times] 'Person.expensive_way' 'Person.another_expensive_way' ...
|
385
|
-
</shell>
|
386
|
-
|
387
|
-
Examples:
|
388
|
-
|
389
|
-
<shell>
|
390
|
-
$ script/performance/benchmarker 10 'Item.all' 'CouchItem.all'
|
391
|
-
</shell>
|
392
|
-
|
393
|
-
If the +[times]+ argument is omitted, supplied methods are run just once:
|
394
|
-
|
395
|
-
<shell>
|
396
|
-
$ script/performance/benchmarker 'Item.first' 'Item.last'
|
397
|
-
</shell>
|
398
|
-
|
399
|
-
h4. +profiler+
|
400
|
-
|
401
|
-
+profiler+ is a wrapper around http://ruby-prof.rubyforge.org/[ruby-prof] gem.
|
402
|
-
|
403
|
-
Usage:
|
404
|
-
|
405
|
-
<shell>
|
406
|
-
$ script/performance/profiler 'Person.expensive_method(10)' [times] [flat|graph|graph_html]
|
407
|
-
</shell>
|
408
|
-
|
409
|
-
Examples:
|
410
|
-
|
411
|
-
<shell>
|
412
|
-
$ script/performance/profiler 'Item.all'
|
413
|
-
</shell>
|
414
|
-
|
415
|
-
This will profile +Item.all+ in +RubyProf::WALL_TIME+ measure mode. By default, it prints flat output to the shell.
|
416
|
-
|
417
|
-
<shell>
|
418
|
-
$ script/performance/profiler 'Item.all' 10 graph
|
419
|
-
</shell>
|
420
|
-
|
421
|
-
This will profile +10.times { Item.all }+ with +RubyProf::WALL_TIME+ measure mode and print graph output to the shell.
|
422
|
-
|
423
|
-
If you want to store the output in a file:
|
424
|
-
|
425
|
-
<shell>
|
426
|
-
$ script/performance/profiler 'Item.all' 10 graph 2> graph.txt
|
427
|
-
</shell>
|
428
|
-
|
429
|
-
h3. Helper Methods
|
430
|
-
|
431
|
-
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 +benchmark()+ in all the three components.
|
432
|
-
|
433
|
-
h4. Model
|
434
|
-
|
435
|
-
<ruby>
|
436
|
-
Project.benchmark("Creating project") do
|
437
|
-
project = Project.create("name" => "stuff")
|
438
|
-
project.create_manager("name" => "David")
|
439
|
-
project.milestones << Milestone.find(:all)
|
440
|
-
end
|
441
|
-
</ruby>
|
442
|
-
|
443
|
-
This benchmarks the code enclosed in the +Project.benchmark("Creating project") do...end+ block and prints the result to the log file:
|
444
|
-
|
445
|
-
<ruby>
|
446
|
-
Creating project (185.3ms)
|
447
|
-
</ruby>
|
448
|
-
|
449
|
-
Please refer to the "API docs":http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001336 for additional options to +benchmark()+
|
450
|
-
|
451
|
-
h4. Controller
|
452
|
-
|
453
|
-
Similarly, you could use this helper method inside "controllers":http://api.rubyonrails.org/classes/ActionController/Benchmarking/ClassMethods.html#M000715
|
454
|
-
|
455
|
-
<ruby>
|
456
|
-
def process_projects
|
457
|
-
self.class.benchmark("Processing projects") do
|
458
|
-
Project.process(params[:project_ids])
|
459
|
-
Project.update_cached_projects
|
460
|
-
end
|
461
|
-
end
|
462
|
-
</ruby>
|
463
|
-
|
464
|
-
NOTE: +benchmark+ is a class method inside controllers
|
465
|
-
|
466
|
-
h4. View
|
467
|
-
|
468
|
-
And in "views":http://api.rubyonrails.org/classes/ActionController/Benchmarking/ClassMethods.html#M000715:
|
469
|
-
|
470
|
-
<erb>
|
471
|
-
<% benchmark("Showing projects partial") do %>
|
472
|
-
<%= render :partial => @projects %>
|
473
|
-
<% end %>
|
474
|
-
</erb>
|
475
|
-
|
476
|
-
h3. Request Logging
|
477
|
-
|
478
|
-
Rails log files contain very useful information about the time taken to serve each request. Here's a typical log file entry:
|
479
|
-
|
480
|
-
<shell>
|
481
|
-
Processing ItemsController#index (for 127.0.0.1 at 2009-01-08 03:06:39) [GET]
|
482
|
-
Rendering template within layouts/items
|
483
|
-
Rendering items/index
|
484
|
-
Completed in 5ms (View: 2, DB: 0) | 200 OK [http://0.0.0.0/items]
|
485
|
-
</shell>
|
486
|
-
|
487
|
-
For this section, we're only interested in the last line:
|
488
|
-
|
489
|
-
<shell>
|
490
|
-
Completed in 5ms (View: 2, DB: 0) | 200 OK [http://0.0.0.0/items]
|
491
|
-
</shell>
|
492
|
-
|
493
|
-
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.
|
494
|
-
|
495
|
-
Michael Koziarski has an "interesting blog post":http://www.therailsway.com/2009/1/6/requests-per-second explaining the importance of using milliseconds as the metric.
|
496
|
-
|
497
|
-
h3. Useful Links
|
498
|
-
|
499
|
-
h4. Rails Plugins and Gems
|
500
|
-
|
501
|
-
* "Rails Analyzer":http://rails-analyzer.rubyforge.org
|
502
|
-
* "Palmist":http://www.flyingmachinestudios.com/projects/
|
503
|
-
* "Rails Footnotes":http://github.com/josevalim/rails-footnotes/tree/master
|
504
|
-
* "Query Reviewer":http://github.com/dsboulder/query_reviewer/tree/master
|
505
|
-
|
506
|
-
h4. Generic Tools
|
507
|
-
|
508
|
-
* "httperf":http://www.hpl.hp.com/research/linux/httperf/
|
509
|
-
* "ab":http://httpd.apache.org/docs/2.2/programs/ab.html
|
510
|
-
* "JMeter":http://jakarta.apache.org/jmeter/
|
511
|
-
* "kcachegrind":http://kcachegrind.sourceforge.net/html/Home.html
|
512
|
-
|
513
|
-
h4. Tutorials and Documentation
|
514
|
-
|
515
|
-
* "ruby-prof API Documentation":http://ruby-prof.rubyforge.org
|
516
|
-
* "Request Profiling Railscast":http://railscasts.com/episodes/98-request-profiling - Outdated, but useful for understanding call graphs
|
517
|
-
|
518
|
-
h3. Commercial Products
|
519
|
-
|
520
|
-
Rails has been lucky to have three startups dedicated to Rails specific performance tools:
|
521
|
-
|
522
|
-
* "New Relic":http://www.newrelic.com
|
523
|
-
* "Fiveruns":http://www.fiveruns.com
|
524
|
-
* "Scout":http://scoutapp.com
|
525
|
-
|
526
|
-
h3. Changelog
|
527
|
-
|
528
|
-
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/4
|
529
|
-
|
530
|
-
* January 9, 2009: Complete rewrite by "Pratik":credits.html#lifo
|
531
|
-
* September 6, 2008: Initial version by Matthew Bergman
|