Froze rails gems
[depot.git] / vendor / rails / railties / doc / guides / source / benchmarking_and_profiling / edge_rails_features.txt
1 == Performance Testing Built into Rails ==
2
3 As of June 20, 2008 edge rails has had a new type of Unit test geared towards profiling. Of course like most great things, getting it working takes bit of work. The test relies on statistics gathered from the Garbage Collection that isn't readily available from standard compiled ruby. There is a patch located at http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch[http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch]
4
5 Also the test requires a new version of Ruby-Prof version of 0.6.1. It is not readily available at the moment and can most easily be found as a tarball on github. It's repository is located at git://github.com/jeremy/ruby-prof.git.
6
7 What follows is a description of how to set up an alternative ruby install to use these features
8
9 === Compiling the Interpreter ===
10
11
12 [source, shell]
13 ----------------------------------------------------------------------------
14 [User ~]$ mkdir rubygc
15 [User ~]$ wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.6-p111.tar.gz
16 [User ~]$ tar -xzvf ruby-1.8.6-p111.tar.gz
17 [User ~]$ cd ruby-1.8.6-p111
18 [User ruby-1.8.6-p111]$ curl http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch | patch -p0
19
20 #I like putting my alternative ruby builds in an opt directory, set the prefix to where ever you feel is most comfortable.
21
22 [User ruby-1.8.6-p111]$ ./configure --prefix=/opt/rubygc
23 [User ruby-1.8.6-p111]$ sudo make && make install
24 ----------------------------------------------------------------------------
25
26 Add the following lines in your \~/.profile or \~/.bash\_login for convenience.
27
28 ----------------------------------------------------------------------------
29 alias gcruby='/opt/rubygc/rubygc/bin/ruby'
30 alias gcrake='/opt/rubygc/rubygc/bin/rake'
31 alias gcgem='/opt/rubygc/rubygc/bin/gem'
32 alias gcirb=/opt/rubygc/rubygc/bin/irb'
33 alias gcrails='/opt/rubygc/rubygc/bin/rails'
34 ----------------------------------------------------------------------------
35
36 === Installing RubyGems ===
37
38 Next we need to install rubygems and rails so that we can use the interpreter properly.
39
40
41 [source, shell]
42 ----------------------------------------------------------------------------
43 [User ~]$ wget http://rubyforge.org/frs/download.php/38646/rubygems-1.2.0.tgz
44 [User ~]$ tar -xzvf rubygems-1.2.0.tgz
45 [User ~]$ cd rubygems-1.2.0
46 [User rubygems-1.2.0]$ gcruby setup.rb
47 [User rubygems-1.2.0]$ cd ~
48 [User ~]$ gcgem install rake
49 [User ~]$ gcgem install mysql
50 [User ~]$ gcgem install rails
51 ----------------------------------------------------------------------------
52
53 If installing mysql gem fails ( like it did for me ), you will have to manually install it :
54
55 [source, shell]
56 ----------------------------------------------------------------------------
57 [User ~]$ cd /Users/lifo/rubygc/lib/ruby/gems/1.8/gems/mysql-2.7/
58 [User mysql-2.7]$ gcruby extconf.rb --with-mysql-config
59 [User mysql-2.7]$ make && make install
60 ----------------------------------------------------------------------------
61
62 === Installing Jeremy Kemper's ruby-prof ===
63
64 We are in the home stretch. All we need now is ruby-proff 0.6.1
65
66
67 [source, shell]
68 ----------------------------------------------------------------------------
69 [User ~]$ git clone git://github.com/jeremy/ruby-prof.git
70 [User ~]$ cd ruby-prof/
71 [User ruby-prof (master)]$ gcrake gem
72 [User ruby-prof (master)]$ gcgem install pkg/ruby-prof-0.6.1.gem
73 ----------------------------------------------------------------------------
74
75 Finished, go get yourself a power drink!
76
77 === Ok so I lied, a few more things we need to do ===
78
79 You have everything we need to start profiling through rails Unit Testing. Unfortunately we are still missing a few files. I'm going to do the next step on a fresh Rails app, but it will work just as well on developmental 2.1 rails application.
80
81 ==== The Rails App ====
82
83 First I need to generate a rail app
84
85 [source, shell]
86 ----------------------------------------------------------------------------
87 [User ~]$ gcrails profiling_tester -d mysql
88 [User ~]$ cd profiling_tester
89 [User profiling_tester]$ script/generate scaffold item name:string
90 [User profiling_tester]$ gcrake db:create:all
91 [User profiling_tester]$ gcrake db:migrate
92 [User profiling_tester (master)]$ rm public/index.html
93 ----------------------------------------------------------------------------
94
95 Now I'm going to init it as a git repository and add edge rails as a submodule to it.
96
97 [source, shell]
98 ----------------------------------------------------------------------------
99 [User profiling_tester]$ git init
100 [User profiling_tester (master)]$ git submodule add git://github.com/rails/rails.git vendor/rails
101 ----------------------------------------------------------------------------
102
103 Finally we want to change config.cache_classes to true in our environment.rb
104
105 ----------------------------------------------------------------------------
106 config.cache_classes = true
107 ----------------------------------------------------------------------------
108
109 If we don't cache classes, then the time Rails spends reloading and compiling our models and controllers will confound our results. Obviously we will try to make our test setup as similar as possible to our production environment.
110
111 === Generating and Fixing the tests ===
112
113 Ok next we need to generate the test script.
114
115 [source, shell]
116 ----------------------------------------------------------------------------
117 [User profiling_tester (master)]$ script/generate performance_test homepage
118 ----------------------------------------------------------------------------
119
120 This will generate _test/performance/homepage_test.rb_ for you. However, as I have generated the project using Rails 2.1 gem, we'll need to manually generate one more file before we can go ahead.
121
122 We need to put the following inside _test/performance/test_helper.rb
123
124
125 [source, ruby]
126 ----------------------------------------------------------------------------
127 require 'test_helper'
128 require 'performance_test_help'
129 ----------------------------------------------------------------------------
130
131 Though this depends where you run your tests from and your system config. I myself run my tests from the Application root directory
132
133 so instead of
134
135 [source, ruby]
136 ----------------------------------------------------------------------------
137 require 'test_helper'
138
139 #I have
140
141 require 'test/test_helper'
142 ----------------------------------------------------------------------------
143
144 Also I needed to change homepage_test.rb to reflect this also
145
146 [source, ruby]
147 ----------------------------------------------------------------------------
148 require 'test/performance/test_helper.rb'
149 ----------------------------------------------------------------------------
150
151 === Testing ===
152
153 #TODO is there some way to compare multiple request at once like ruby_analyze
154
155 Now, if we look at the generated performance test ( one we generated using _script/generate performance_test_ ), it'll look something like :
156
157 [source, ruby]
158 ----------------------------------------------------------------------------
159 .require 'performance/test_helper'
160
161 class HomepageTest < ActionController::PerformanceTest
162 # Replace this with your real tests.
163 def test_homepage
164 get '/'
165 end
166 end
167 ----------------------------------------------------------------------------
168
169
170 The format looks very similar to that of an integration test. And guess what, that's what it is. But that doesn't stop you from testing your Model methods. You could very well write something like :
171
172 [source, ruby]
173 ----------------------------------------------------------------------------
174 require 'performance/test_helper'
175
176 class UserModelTest < ActionController::PerformanceTest
177 # Replace this with your real tests.
178 def test_slow_find
179 User.this_takes_shlong_to_run
180 end
181 end
182 ----------------------------------------------------------------------------
183
184
185 Which is very useful way to profile individual processes.