Froze rails gems
[depot.git] / vendor / rails / railties / doc / guides / html / benchmarking_and_profiling.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5 <title>Benchmarking and Profiling Rails</title>
6 <!--[if lt IE 8]>
7 <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
8 <![endif]-->
9 <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
10 <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
11 <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
12 <style type="text/css">
13 div#container {
14 max-width: 900px;
15 padding-bottom: 3em;
16 }
17
18 div#content {
19 margin-left: 200px;
20 }
21
22 div#container.notoc {
23 max-width: 600px;
24 }
25
26 .notoc div#content {
27 margin-left: 0;
28 }
29
30 pre {
31 line-height: 1.4em;
32 }
33
34 #content p tt {
35 background: #eeeeee;
36 border: solid 1px #cccccc;
37 padding: 3px;
38 }
39
40 dt {
41 font-weight: bold;
42 }
43
44 #content dt tt {
45 font-size: 10pt;
46 }
47
48 dd {
49 margin-left: 3em;
50 }
51
52 #content dt tt, #content pre tt {
53 background: none;
54 padding: 0;
55 border: 0;
56 }
57
58 #content .olist ol {
59 margin-left: 2em;
60 }
61
62 #header {
63 position: relative;
64 max-width: 840px;
65 margin-left: auto;
66 margin-right: auto;
67 }
68
69 #header.notoc {
70 max-width: 580px;
71 }
72
73 #logo {
74 position: absolute;
75 left: 10px;
76 top: 10px;
77 width: 110px;
78 height: 140px;
79 }
80
81 div#header h1#site_title {
82 background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
83 position: absolute;
84 width: 392px;
85 height: 55px;
86 left: 145px;
87 top: 20px;
88 margin: 0;
89 padding: 0;
90 }
91
92 #site_title span {
93 display: none;
94 }
95
96 #site_title_tagline {
97 display: none;
98 }
99
100 ul#navMain {
101 position: absolute;
102 margin: 0;
103 padding: 0;
104 top: 97px;
105 left: 145px;
106 }
107
108 .left-floaty, .right-floaty {
109 padding: 15px;
110 }
111
112 .admonitionblock,
113 .tableblock {
114 margin-left: 1em;
115 margin-right: 1em;
116 margin-top: 0.25em;
117 margin-bottom: 1em;
118 }
119
120 .admonitionblock .icon {
121 padding-right: 8px;
122 }
123
124 .admonitionblock .content {
125 border: solid 1px #ffda78;
126 background: #fffebd;
127 padding: 10px;
128 padding-top: 8px;
129 padding-bottom: 8px;
130 }
131
132 .admonitionblock .title {
133 font-size: 140%;
134 margin-bottom: 0.5em;
135 }
136
137 .tableblock table {
138 border: solid 1px #aaaaff;
139 background: #f0f0ff;
140 }
141
142 .tableblock th {
143 background: #e0e0e0;
144 }
145
146 .tableblock th,
147 .tableblock td {
148 padding: 3px;
149 padding-left: 5px;
150 padding-right: 5px;
151 }
152
153 .sidebarblock {
154 margin-top: 0.25em;
155 margin: 1em;
156 border: solid 1px #ccccbb;
157 padding: 8px;
158 background: #ffffe0;
159 }
160
161 .sidebarblock .sidebar-title {
162 font-size: 140%;
163 font-weight: 600;
164 margin-bottom: 0.3em;
165 }
166
167 .sidebarblock .sidebar-content > .para:last-child > p {
168 margin-bottom: 0;
169 }
170
171 .sidebarblock .sidebar-title a {
172 text-decoration: none;
173 }
174
175 .sidebarblock .sidebar-title a:hover {
176 text-decoration: underline;
177 }
178
179 </style>
180 </head>
181 <body>
182 <div id="header" >
183 <div id="logo">
184 <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
185 </div>
186
187 <h1 id="site_title"><span>Ruby on Rails</span></h1>
188 <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
189
190 <ul id="navMain">
191 <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
192 <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
193 </ul>
194 </div>
195
196 <div id="container">
197
198 <div id="sidebar">
199 <h2>Chapters</h2>
200 <ol>
201 <li>
202 <a href="#_why_benchmark_and_profile">Why Benchmark and Profile ?</a>
203 <ul>
204
205 <li><a href="#_what_is_the_difference_between_benchmarking_and_profiling">What is the difference between benchmarking and profiling ?</a></li>
206
207 </ul>
208 </li>
209 <li>
210 <a href="#_using_and_understanding_the_log_files">Using and understanding the log files</a>
211 </li>
212 <li>
213 <a href="#_helper_methods">Helper methods</a>
214 </li>
215 <li>
216 <a href="#_performance_test_cases">Performance Test Cases</a>
217 <ul>
218
219 <li><a href="#_modes">Modes</a></li>
220
221 <li><a href="#_metrics">Metrics</a></li>
222
223 <li><a href="#_preparing_ruby_and_ruby_prof">Preparing Ruby and Ruby-prof</a></li>
224
225 <li><a href="#_installing_jeremy_kemper_s_ruby_prof">Installing Jeremy Kemper's ruby-prof</a></li>
226
227 <li><a href="#_generating_performance_test">Generating performance test</a></li>
228
229 <li><a href="#_running_tests">Running tests</a></li>
230
231 </ul>
232 </li>
233 <li>
234 <a href="#_understanding_performance_tests_outputs">Understanding Performance Tests Outputs</a>
235 <ul>
236
237 <li><a href="#_our_first_performance_test">Our First Performance Test</a></li>
238
239 <li><a href="#_flat_files">Flat Files</a></li>
240
241 <li><a href="#_graph_files">Graph Files</a></li>
242
243 <li><a href="#_tree_files">Tree Files</a></li>
244
245 </ul>
246 </li>
247 <li>
248 <a href="#_getting_to_the_point_of_all_of_this">Getting to the Point of all of this</a>
249 </li>
250 <li>
251 <a href="#_real_life_example">Real Life Example</a>
252 <ul>
253
254 <li><a href="#_the_setup">The setup</a></li>
255
256 </ul>
257 </li>
258 <li>
259 <a href="#_get_yourself_a_game_plan">Get Yourself a Game Plan</a>
260 <ul>
261
262 <li><a href="#_the_analysis_process">The Analysis Process</a></li>
263
264 </ul>
265 </li>
266 <li>
267 <a href="#_other_profiling_tools">Other Profiling Tools</a>
268 <ul>
269
270 <li><a href="#_httperf">httperf</a></li>
271
272 <li><a href="#_rails_analyzer">Rails Analyzer</a></li>
273
274 <li><a href="#_palmist">Palmist</a></li>
275
276 <li><a href="#_new_relic">New Relic</a></li>
277
278 </ul>
279 </li>
280 <li>
281 <a href="#_changelog">Changelog</a>
282 </li>
283 </ol>
284 </div>
285
286 <div id="content">
287 <h1>Benchmarking and Profiling Rails</h1>
288 <div id="preamble">
289 <div class="sectionbody">
290 <div class="para"><p>This guide covers the benchmarking and profiling tactics/tools of Rails and Ruby in general. By referring to this guide, you will be able to:</p></div>
291 <div class="ilist"><ul>
292 <li>
293 <p>
294 Understand the various types of benchmarking and profiling metrics
295 </p>
296 </li>
297 <li>
298 <p>
299 Generate performance/benchmarking tests
300 </p>
301 </li>
302 <li>
303 <p>
304 Use GC patched Ruby binary to measure memory usage and object allocation
305 </p>
306 </li>
307 <li>
308 <p>
309 Understand the information provided by Rails inside the log files
310 </p>
311 </li>
312 <li>
313 <p>
314 Learn about various tools facilitating benchmarking and profiling
315 </p>
316 </li>
317 </ul></div>
318 </div>
319 </div>
320 <h2 id="_why_benchmark_and_profile">1. Why Benchmark and Profile ?</h2>
321 <div class="sectionbody">
322 <div class="para"><p>Benchmarking and Profiling 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 plesant browsing experience to the end users and cutting cost of unnecessary hardwares is important for any web application.</p></div>
323 <h3 id="_what_is_the_difference_between_benchmarking_and_profiling">1.1. What is the difference between benchmarking and profiling ?</h3>
324 <div class="para"><p>Benchmarking is the process of finding out if a piece of code is slow or not. Whereas profiling is the process of finding out what exactly is slowing down that piece of code.</p></div>
325 </div>
326 <h2 id="_using_and_understanding_the_log_files">2. Using and understanding the log files</h2>
327 <div class="sectionbody">
328 <div class="para"><p>Rails logs files containt basic but very useful information about the time taken to serve every request. A typical log entry looks something like :</p></div>
329 <div class="listingblock">
330 <div class="content"><!-- Generator: GNU source-highlight 2.9
331 by Lorenzo Bettini
332 http://www.lorenzobettini.it
333 http://www.gnu.org/software/src-highlite -->
334 <pre><tt>Processing ItemsController<span style="font-style: italic"><span style="color: #9A1900">#index (for 127.0.0.1 at 2008-10-17 00:08:18) [GET]</span></span>
335 Session ID<span style="color: #990000">:</span> BAh7BiIKZmxhc2hJQzonQWN0aHsABjoKQHVzZWR7AA<span style="color: #990000">==--</span>83cff4fe0a897074a65335
336 Parameters<span style="color: #990000">:</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">"action"</span><span style="color: #990000">=&gt;</span><span style="color: #FF0000">"index"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"controller"</span><span style="color: #990000">=&gt;</span><span style="color: #FF0000">"items"</span><span style="color: #FF0000">}</span>
337 Rendering template within layouts<span style="color: #990000">/</span>items
338 Rendering items<span style="color: #990000">/</span>index
339 Completed <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> 5ms <span style="color: #990000">(</span>View<span style="color: #990000">:</span> <span style="color: #993399">2</span><span style="color: #990000">,</span> DB<span style="color: #990000">:</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> <span style="color: #990000">|</span> <span style="color: #993399">200</span> OK <span style="color: #990000">[</span>http<span style="color: #990000">:</span><span style="color: #FF6600">//localhost/</span>items<span style="color: #990000">]</span>
340 </tt></pre></div></div>
341 <div class="para"><p>For this section, we're only interested in the last line from that log entry:</p></div>
342 <div class="listingblock">
343 <div class="content"><!-- Generator: GNU source-highlight 2.9
344 by Lorenzo Bettini
345 http://www.lorenzobettini.it
346 http://www.gnu.org/software/src-highlite -->
347 <pre><tt>Completed <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> 5ms <span style="color: #990000">(</span>View<span style="color: #990000">:</span> <span style="color: #993399">2</span><span style="color: #990000">,</span> DB<span style="color: #990000">:</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> <span style="color: #990000">|</span> <span style="color: #993399">200</span> OK <span style="color: #990000">[</span>http<span style="color: #990000">:</span><span style="color: #FF6600">//localhost/</span>items<span style="color: #990000">]</span>
348 </tt></pre></div></div>
349 <div class="para"><p>This data is fairly straight forward to understand. Rails uses millisecond(ms) as the metric to measures 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></div>
350 </div>
351 <h2 id="_helper_methods">3. Helper methods</h2>
352 <div class="sectionbody">
353 <div class="para"><p>Rails provides various helper methods inside Active Record, Action Controller and Action View to measure the time taken by a specific code. The method is called <tt>benchmark()</tt> in all three components.</p></div>
354 <div class="listingblock">
355 <div class="content"><!-- Generator: GNU source-highlight 2.9
356 by Lorenzo Bettini
357 http://www.lorenzobettini.it
358 http://www.gnu.org/software/src-highlite -->
359 <pre><tt>Project<span style="color: #990000">.</span>benchmark<span style="color: #990000">(</span><span style="color: #FF0000">"Creating project"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
360 project <span style="color: #990000">=</span> Project<span style="color: #990000">.</span>create<span style="color: #990000">(</span><span style="color: #FF0000">"name"</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"stuff"</span><span style="color: #990000">)</span>
361 project<span style="color: #990000">.</span>create_manager<span style="color: #990000">(</span><span style="color: #FF0000">"name"</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"David"</span><span style="color: #990000">)</span>
362 project<span style="color: #990000">.</span>milestones <span style="color: #990000">&lt;&lt;</span> Milestone<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">)</span>
363 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
364 </tt></pre></div></div>
365 <div class="para"><p>The above code benchmarks the multiple statments enclosed inside <tt>Project.benchmark("Creating project") do..end</tt> block and prints the results inside log files. The statement inside log files will look like:</p></div>
366 <div class="listingblock">
367 <div class="content"><!-- Generator: GNU source-highlight 2.9
368 by Lorenzo Bettini
369 http://www.lorenzobettini.it
370 http://www.gnu.org/software/src-highlite -->
371 <pre><tt>Creating projectem <span style="color: #990000">(</span><span style="color: #993399">185</span><span style="color: #990000">.</span>3ms<span style="color: #990000">)</span>
372 </tt></pre></div></div>
373 <div class="para"><p>Please refer to <a href="http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001336">API docs</a> for optional options to <tt>benchmark()</tt></p></div>
374 <div class="para"><p>Similarly, you could use this helper method inside <a href="http://api.rubyonrails.com/classes/ActionController/Benchmarking/ClassMethods.html#M000715">controllers</a> ( Note that it's a class method here ):</p></div>
375 <div class="listingblock">
376 <div class="content"><!-- Generator: GNU source-highlight 2.9
377 by Lorenzo Bettini
378 http://www.lorenzobettini.it
379 http://www.gnu.org/software/src-highlite -->
380 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> process_projects
381 <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span><span style="color: #990000">.</span>benchmark<span style="color: #990000">(</span><span style="color: #FF0000">"Processing projects"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
382 Project<span style="color: #990000">.</span>process<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>project_ids<span style="color: #990000">])</span>
383 Project<span style="color: #990000">.</span>update_cached_projects
384 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
385 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
386 </tt></pre></div></div>
387 <div class="para"><p>and <a href="http://api.rubyonrails.com/classes/ActionController/Benchmarking/ClassMethods.html#M000715">views</a>:</p></div>
388 <div class="listingblock">
389 <div class="content"><!-- Generator: GNU source-highlight 2.9
390 by Lorenzo Bettini
391 http://www.lorenzobettini.it
392 http://www.gnu.org/software/src-highlite -->
393 <pre><tt><span style="color: #FF0000">&lt;% benchmark("Showing projects partial") do %&gt;</span>
394 <span style="color: #FF0000">&lt;%= render :partial =&gt;</span> <span style="color: #009900">@projects</span> <span style="color: #990000">%&gt;</span>
395 <span style="color: #FF0000">&lt;% end %&gt;</span>
396 </tt></pre></div></div>
397 </div>
398 <h2 id="_performance_test_cases">4. Performance Test Cases</h2>
399 <div class="sectionbody">
400 <div class="para"><p>Rails provides a very easy to write performance test cases, which look just like the regular integration tests.</p></div>
401 <div class="para"><p>If you have a look at <tt>test/performance/browsing_test.rb</tt> in a newly created Rails application:</p></div>
402 <div class="listingblock">
403 <div class="content"><!-- Generator: GNU source-highlight 2.9
404 by Lorenzo Bettini
405 http://www.lorenzobettini.it
406 http://www.gnu.org/software/src-highlite -->
407 <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
408 <span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'performance_test_help'</span>
409
410 <span style="font-style: italic"><span style="color: #9A1900"># Profiling results for each test method are written to tmp/performance.</span></span>
411 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> BrowsingTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>PerformanceTest
412 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_homepage
413 get <span style="color: #FF0000">'/'</span>
414 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
415 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
416 </tt></pre></div></div>
417 <div class="para"><p>This is an automatically generated example performance test file, for testing performance of homepage(<em>/</em>) of the application.</p></div>
418 <h3 id="_modes">4.1. Modes</h3>
419 <h4 id="_benchmarking">4.1.1. Benchmarking</h4>
420 <h4 id="_profiling">4.1.2. Profiling</h4>
421 <h3 id="_metrics">4.2. Metrics</h3>
422 <h4 id="_process_time">4.2.1. Process Time</h4>
423 <div class="para"><p>CPU Cycles.</p></div>
424 <h4 id="_memory">4.2.2. Memory</h4>
425 <div class="para"><p>Memory taken.</p></div>
426 <h4 id="_objects">4.2.3. Objects</h4>
427 <div class="para"><p>Objects allocated.</p></div>
428 <h4 id="_gc_runs">4.2.4. GC Runs</h4>
429 <div class="para"><p>Number of times the Ruby GC was run.</p></div>
430 <h4 id="_gc_time">4.2.5. GC Time</h4>
431 <div class="para"><p>Time spent running the Ruby GC.</p></div>
432 <h3 id="_preparing_ruby_and_ruby_prof">4.3. Preparing Ruby and Ruby-prof</h3>
433 <div class="para"><p>Before we go ahead, Rails performance testing requires you to build a special Ruby binary with some super powers - GC patch for measuring GC Runs/Time. This process is very straight forward. If you've never compiled a Ruby binary before, you can follow the following steps to build a ruby binary inside your home directory:</p></div>
434 <h4 id="_compile">4.3.1. Compile</h4>
435 <div class="listingblock">
436 <div class="content"><!-- Generator: GNU source-highlight 2.9
437 by Lorenzo Bettini
438 http://www.lorenzobettini.it
439 http://www.gnu.org/software/src-highlite -->
440 <pre><tt><span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ mkdir rubygc
441 <span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ wget ftp<span style="color: #990000">:</span>//ftp<span style="color: #990000">.</span>ruby-lang<span style="color: #990000">.</span>org/pub/ruby<span style="color: #990000">/</span><span style="color: #993399">1.8</span>/ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6</span>-p<span style="color: #993399">111</span><span style="color: #990000">.</span>tar<span style="color: #990000">.</span>gz
442 <span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ tar -xzvf ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6</span>-p<span style="color: #993399">111</span><span style="color: #990000">.</span>tar<span style="color: #990000">.</span>gz
443 <span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ cd ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6</span>-p<span style="color: #993399">111</span>
444 <span style="color: #990000">[</span>lifo@null ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6</span>-p<span style="color: #993399">111</span><span style="color: #990000">]</span>$ curl http<span style="color: #990000">:</span>//rubyforge<span style="color: #990000">.</span>org/tracker/download<span style="color: #990000">.</span>php<span style="color: #990000">/</span><span style="color: #993399">1814</span><span style="color: #990000">/</span><span style="color: #993399">7062</span><span style="color: #990000">/</span><span style="color: #993399">17676</span><span style="color: #990000">/</span><span style="color: #993399">3291</span>/ruby186gc<span style="color: #990000">.</span>patch <span style="color: #990000">|</span> patch -p<span style="color: #993399">0</span>
445 <span style="color: #990000">[</span>lifo@null ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6</span>-p<span style="color: #993399">111</span><span style="color: #990000">]</span>$ <span style="color: #990000">.</span>/configure --prefix<span style="color: #990000">=</span>/Users/lifo/rubygc
446 <span style="color: #990000">[</span>lifo@null ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6</span>-p<span style="color: #993399">111</span><span style="color: #990000">]</span>$ make <span style="color: #990000">&amp;&amp;</span> make install
447 </tt></pre></div></div>
448 <h4 id="_prepare_aliases">4.3.2. Prepare aliases</h4>
449 <div class="para"><p>Add the following lines in your ~/.profile for convenience:</p></div>
450 <div class="listingblock">
451 <div class="content">
452 <pre><tt>alias gcruby='/Users/lifo/rubygc/bin/ruby'
453 alias gcrake='/Users/lifo/rubygc/bin/rake'
454 alias gcgem='/Users/lifo/rubygc/bin/gem'
455 alias gcirb='/Users/lifo/rubygc/bin/irb'
456 alias gcrails='/Users/lifo/rubygc/bin/rails'</tt></pre>
457 </div></div>
458 <h4 id="_install_rubygems_and_some_basic_gems">4.3.3. Install rubygems and some basic gems</h4>
459 <div class="listingblock">
460 <div class="content">
461 <pre><tt>[lifo@null ~]$ wget http://rubyforge.org/frs/download.php/38646/rubygems-1.2.0.tgz
462 [lifo@null ~]$ tar -xzvf rubygems-1.2.0.tgz
463 [lifo@null ~]$ cd rubygems-1.2.0
464 [lifo@null rubygems-1.2.0]$ gcruby setup.rb
465 [lifo@null rubygems-1.2.0]$ cd ~
466 [lifo@null ~]$ gcgem install rake
467 [lifo@null ~]$ gcgem install rails</tt></pre>
468 </div></div>
469 <h4 id="_install_mysql_gem">4.3.4. Install MySQL gem</h4>
470 <div class="listingblock">
471 <div class="content">
472 <pre><tt>[lifo@null ~]$ gcgem install mysql</tt></pre>
473 </div></div>
474 <div class="para"><p>If this fails, you can try to install it manually:</p></div>
475 <div class="listingblock">
476 <div class="content">
477 <pre><tt>[lifo@null ~]$ cd /Users/lifo/rubygc/lib/ruby/gems/1.8/gems/mysql-2.7/
478 [lifo@null mysql-2.7]$ gcruby extconf.rb --with-mysql-config
479 [lifo@null mysql-2.7]$ make &amp;&amp; make install</tt></pre>
480 </div></div>
481 <h3 id="_installing_jeremy_kemper_s_ruby_prof">4.4. Installing Jeremy Kemper's ruby-prof</h3>
482 <div class="para"><p>We also need to install Jeremy's ruby-prof gem using our newly built ruby:</p></div>
483 <div class="listingblock">
484 <div class="content"><!-- Generator: GNU source-highlight 2.9
485 by Lorenzo Bettini
486 http://www.lorenzobettini.it
487 http://www.gnu.org/software/src-highlite -->
488 <pre><tt><span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ git clone git<span style="color: #990000">:</span>//github<span style="color: #990000">.</span>com/jeremy/ruby-prof<span style="color: #990000">.</span>git
489 <span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ cd ruby-prof<span style="color: #990000">/</span>
490 <span style="color: #990000">[</span>lifo@null ruby-prof <span style="color: #990000">(</span>master<span style="color: #990000">)]</span>$ gcrake gem
491 <span style="color: #990000">[</span>lifo@null ruby-prof <span style="color: #990000">(</span>master<span style="color: #990000">)]</span>$ gcgem install pkg/ruby-prof-<span style="color: #993399">0.6</span><span style="color: #990000">.</span><span style="color: #993399">1</span><span style="color: #990000">.</span>gem
492 </tt></pre></div></div>
493 <h3 id="_generating_performance_test">4.5. Generating performance test</h3>
494 <div class="para"><p>Rails provides a simple generator for creating new performance tests:</p></div>
495 <div class="listingblock">
496 <div class="content"><!-- Generator: GNU source-highlight 2.9
497 by Lorenzo Bettini
498 http://www.lorenzobettini.it
499 http://www.gnu.org/software/src-highlite -->
500 <pre><tt><span style="color: #990000">[</span>lifo@null application <span style="color: #990000">(</span>master<span style="color: #990000">)]</span>$ script/generate performance_test homepage
501 </tt></pre></div></div>
502 <div class="para"><p>This will generate <tt>test/performance/homepage_test.rb</tt>:</p></div>
503 <div class="listingblock">
504 <div class="content"><!-- Generator: GNU source-highlight 2.9
505 by Lorenzo Bettini
506 http://www.lorenzobettini.it
507 http://www.gnu.org/software/src-highlite -->
508 <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
509 <span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'performance_test_help'</span>
510
511 <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> HomepageTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>PerformanceTest
512 <span style="font-style: italic"><span style="color: #9A1900"># Replace this with your real tests.</span></span>
513 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_homepage
514 get <span style="color: #FF0000">'/'</span>
515 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
516 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
517 </tt></pre></div></div>
518 <div class="para"><p>Which you can modify to suit your needs.</p></div>
519 <h3 id="_running_tests">4.6. Running tests</h3>
520 </div>
521 <h2 id="_understanding_performance_tests_outputs">5. Understanding Performance Tests Outputs</h2>
522 <div class="sectionbody">
523 <h3 id="_our_first_performance_test">5.1. Our First Performance Test</h3>
524 <div class="para"><p>So how do we profile a request.</p></div>
525 <div class="para"><p>One of the things that is important to us is how long it takes to render the home page - so let's make a request to the home page. Once the request is complete, the results will be outputted in the terminal.</p></div>
526 <div class="para"><p>In the terminal run</p></div>
527 <div class="listingblock">
528 <div class="content"><!-- Generator: GNU source-highlight 2.9
529 by Lorenzo Bettini
530 http://www.lorenzobettini.it
531 http://www.gnu.org/software/src-highlite -->
532 <pre><tt><span style="color: #990000">[</span>User profiling_tester<span style="color: #990000">]</span>$ gcruby tests<span style="color: #FF6600">/performance/</span>homepage<span style="color: #990000">.</span>rb
533 </tt></pre></div></div>
534 <div class="para"><p>After the tests runs for a few seconds you should see something like this.</p></div>
535 <div class="listingblock">
536 <div class="content">
537 <pre><tt>HomepageTest#test_homepage (19 ms warmup)
538 process_time: 26 ms
539 memory: 298.79 KB
540 objects: 1917
541
542 Finished in 2.207428 seconds.</tt></pre>
543 </div></div>
544 <div class="para"><p>Simple but efficient.</p></div>
545 <div class="ilist"><ul>
546 <li>
547 <p>
548 Process Time refers to amount of time necessary to complete the action.
549 </p>
550 </li>
551 <li>
552 <p>
553 memory is the amount of information loaded into memory
554 </p>
555 </li>
556 <li>
557 <p>
558 object ??? #TODO find a good definition. Is it the amount of objects put into a ruby heap for this process?
559 </p>
560 </li>
561 </ul></div>
562 <div class="para"><p>In addition we also gain three types of itemized log files for each of these outputs. They can be found in your tmp directory of your application.</p></div>
563 <div class="para"><p><strong>The Three types are</strong></p></div>
564 <div class="ilist"><ul>
565 <li>
566 <p>
567 Flat File - A simple text file with the data laid out in a grid
568 </p>
569 </li>
570 <li>
571 <p>
572 Graphical File - A html colored coded version of the simple text file with hyperlinks between the various methods. Most useful is the bolding of the main processes for each portion of the action.
573 </p>
574 </li>
575 <li>
576 <p>
577 Tree File - A file output that can be use in conjunction with KCachegrind to visualize the process
578 </p>
579 </li>
580 </ul></div>
581 <div class="admonitionblock">
582 <table><tr>
583 <td class="icon">
584 <img src="./images/icons/note.png" alt="Note" />
585 </td>
586 <td class="content">KCachegrind is Linux only. For Mac this means you have to do a full KDE install to have it working in your OS. Which is over 3 gigs in size. For windows there is clone called wincachegrind but it is no longer actively being developed.</td>
587 </tr></table>
588 </div>
589 <div class="para"><p>Below are examples for Flat Files and Graphical Files</p></div>
590 <h3 id="_flat_files">5.2. Flat Files</h3>
591 <div class="exampleblock">
592 <div class="title">Example: Flat File Output Processing Time</div>
593 <div class="exampleblock-content">
594 <div class="para"><p>Thread ID: 2279160
595 Total: 0.026097</p></div>
596 <div class="literalblock">
597 <div class="content">
598 <pre><tt>%self total self wait child calls name
599 6.41 0.06 0.04 0.00 0.02 571 Kernel#===
600 3.17 0.00 0.00 0.00 0.00 172 Hash#[]
601 2.42 0.00 0.00 0.00 0.00 13 MonitorMixin#mon_exit
602 2.05 0.00 0.00 0.00 0.00 15 Array#each
603 1.56 0.00 0.00 0.00 0.00 6 Logger#add
604 1.55 0.00 0.00 0.00 0.00 13 MonitorMixin#mon_enter
605 1.36 0.03 0.00 0.00 0.03 1 ActionController::Integration::Session#process
606 1.31 0.00 0.00 0.00 0.00 13 MonitorMixin#mon_release
607 1.15 0.00 0.00 0.00 0.00 8 MonitorMixin#synchronize-1
608 1.09 0.00 0.00 0.00 0.00 23 Class#new
609 1.03 0.01 0.00 0.00 0.01 5 MonitorMixin#synchronize
610 0.89 0.00 0.00 0.00 0.00 74 Hash#default
611 0.89 0.00 0.00 0.00 0.00 6 Hodel3000CompliantLogger#format_message
612 0.80 0.00 0.00 0.00 0.00 9 c
613 0.80 0.00 0.00 0.00 0.00 11 ActiveRecord::ConnectionAdapters::ConnectionHandler#retrieve_connection_pool
614 0.79 0.01 0.00 0.00 0.01 1 ActionController::Benchmarking#perform_action_without_rescue
615 0.18 0.00 0.00 0.00 0.00 17 &lt;Class::Object&gt;#allocate</tt></pre>
616 </div></div>
617 </div></div>
618 <div class="para"><p>So what do these columns tell us:</p></div>
619 <div class="ilist"><ul>
620 <li>
621 <p>
622 %self - The percentage of time spent processing the method. This is derived from self_time/total_time
623 </p>
624 </li>
625 <li>
626 <p>
627 total - The time spent in this method and its children.
628 </p>
629 </li>
630 <li>
631 <p>
632 self - The time spent in this method.
633 </p>
634 </li>
635 <li>
636 <p>
637 wait - Time processed was queued
638 </p>
639 </li>
640 <li>
641 <p>
642 child - The time spent in this method's children.
643 </p>
644 </li>
645 <li>
646 <p>
647 calls - The number of times this method was called.
648 </p>
649 </li>
650 <li>
651 <p>
652 name - The name of the method.
653 </p>
654 </li>
655 </ul></div>
656 <div class="para"><p>Name can be displayed three seperate ways:
657 <strong> #toplevel - The root method that calls all other methods
658 </strong> MyObject#method - Example Hash#each, The class Hash is calling the method each
659 * &lt;Object:MyObject&gt;#test - The &lt;&gt; characters indicate a singleton method on a singleton class. Example &lt;Class::Object&gt;#allocate</p></div>
660 <div class="para"><p>Methods are sorted based on %self. Hence the ones taking the most time and resources will be at the top.</p></div>
661 <div class="para"><p>So for Array#each which is calling each on the class array. We find that it processing time is 2% of the total and was called 15 times. The rest of the information is 0.00 because the process is so fast it isn't recording times less then 100 ms.</p></div>
662 <div class="exampleblock">
663 <div class="title">Example: Flat File Memory Output</div>
664 <div class="exampleblock-content">
665 <div class="para"><p>Thread ID: 2279160
666 Total: 509.724609</p></div>
667 <div class="literalblock">
668 <div class="content">
669 <pre><tt>%self total self wait child calls name
670 4.62 23.57 23.57 0.00 0.00 34 String#split
671 3.95 57.66 20.13 0.00 37.53 3 &lt;Module::YAML&gt;#quick_emit
672 2.82 23.70 14.35 0.00 9.34 2 &lt;Module::YAML&gt;#quick_emit-1
673 1.37 35.87 6.96 0.00 28.91 1 ActionView::Helpers::FormTagHelper#form_tag
674 1.35 7.69 6.88 0.00 0.81 1 ActionController::HttpAuthentication::Basic::ControllerMethods#authenticate_with_http_basic
675 1.06 6.09 5.42 0.00 0.67 90 String#gsub
676 1.01 5.13 5.13 0.00 0.00 27 Array#-</tt></pre>
677 </div></div>
678 </div></div>
679 <div class="para"><p>Very similar to the processing time format. The main difference here is that instead of calculating time we are now concerned with the amount of KB put into memory <strong>(or is it strictly into the heap) can I get clarification on this minor point?</strong></p></div>
680 <div class="para"><p>So for &lt;Module::YAML&gt;#quick_emit which is singleton method on the class YAML it uses 57.66 KB in total, 23.57 through its own actions, 6.69 from actions it calls itself and that it was called twice.</p></div>
681 <div class="exampleblock">
682 <div class="title">Example: Flat File Objects</div>
683 <div class="exampleblock-content">
684 <div class="para"><p>Thread ID: 2279160
685 Total: 6537.000000</p></div>
686 <div class="literalblock">
687 <div class="content">
688 <pre><tt>%self total self wait child calls name
689 15.16 1096.00 991.00 0.00 105.00 66 Hash#each
690 5.25 343.00 343.00 0.00 0.00 4 Mysql::Result#each_hash
691 4.74 2203.00 310.00 0.00 1893.00 42 Array#each
692 3.75 4529.00 245.00 0.00 4284.00 1 ActionView::Base::CompiledTemplates#_run_erb_47app47views47layouts47application46html46erb
693 2.00 136.00 131.00 0.00 5.00 90 String#gsub
694 1.73 113.00 113.00 0.00 0.00 34 String#split
695 1.44 111.00 94.00 0.00 17.00 31 Array#each-1</tt></pre>
696 </div></div>
697 </div></div>
698 <div class="literalblock">
699 <div class="content">
700 <pre><tt>#TODO Find correct terminology for how to describe what this is exactly profiling as in are there really 2203 array objects or 2203 pointers to array objects?.</tt></pre>
701 </div></div>
702 <h3 id="_graph_files">5.3. Graph Files</h3>
703 <div class="para"><p>While the information gleamed from flat files is very useful we still don't know which processes each method is calling. We only know how many. This is not true for a graph file. Below is a text representation of a graph file. The actual graph file is an html entity and an example of which can be found <a href="examples/graph.html">Here</a></p></div>
704 <div class="para"><p>#TODO (Handily the graph file has links both between it many processes and to the files that actually contain them for debugging.
705 )</p></div>
706 <div class="exampleblock">
707 <div class="title">Example: Graph File</div>
708 <div class="exampleblock-content">
709 <div class="para"><p>Thread ID: 21277412</p></div>
710 <div class="literalblock">
711 <div class="content">
712 <pre><tt> %total %self total self children calls Name
713 /____________________________________________________________________________/
714 100.00% 0.00% 8.77 0.00 8.77 1 #toplevel*
715 8.77 0.00 8.77 1/1 Object#run_primes
716 /____________________________________________________________________________/
717 8.77 0.00 8.77 1/1 #toplevel
718 100.00% 0.00% 8.77 0.00 8.77 1 Object#run_primes*
719 0.02 0.00 0.02 1/1 Object#make_random_array
720 2.09 0.00 2.09 1/1 Object#find_largest
721 6.66 0.00 6.66 1/1 Object#find_primes
722 /____________________________________________________________________________/
723 0.02 0.02 0.00 1/1 Object#make_random_array
724 0.18% 0.18% 0.02 0.02 0.00 1 Array#each_index
725 0.00 0.00 0.00 500/500 Kernel.rand
726 0.00 0.00 0.00 500/501 Array#[]=
727 /____________________________________________________________________________/</tt></pre>
728 </div></div>
729 </div></div>
730 <div class="para"><p>As you can see the calls have been separated into slices, no longer is the order determined by process time but instead from hierarchy. Each slice profiles a primary entry, with the primary entry's parents being shown above itself and it's children found below. A primary entry can be ascertained by it having values in the %total and %self columns. Here the main entry here have been bolded for connivence.</p></div>
731 <div class="para"><p>So if we look at the last slice. The primary entry would be Array#each_index. It takes 0.18% of the total process time and it is only called once. It is called from Object#make_random_array which is only called once. It's children are Kernal.rand which is called by it all 500 its times that it was call in this action and Arry#[]= which was called 500 times by Array#each_index and once by some other entry.</p></div>
732 <h3 id="_tree_files">5.4. Tree Files</h3>
733 <div class="para"><p>It's pointless trying to represent a tree file textually so here's a few pretty pictures of it's usefulness</p></div>
734 <div class="para"><div class="title">KCachegrind Graph</div><p><span class="image">
735 <img src="images/kgraph.png" alt="Graph created by KCachegrind" title="Graph created by KCachegrind" />
736 </span></p></div>
737 <div class="para"><div class="title">KCachegrind List</div><p><span class="image">
738 <img src="images/klist.png" alt="List created by KCachegrind" title="List created by KCachegrind" />
739 </span></p></div>
740 <div class="para"><p>#TODO Add a bit more information to this.</p></div>
741 </div>
742 <h2 id="_getting_to_the_point_of_all_of_this">6. Getting to the Point of all of this</h2>
743 <div class="sectionbody">
744 <div class="para"><p>Now I know all of this is a bit dry and academic. But it's a very powerful tool when you know how to leverage it properly. Which we are going to take a look at in our next section</p></div>
745 </div>
746 <h2 id="_real_life_example">7. Real Life Example</h2>
747 <div class="sectionbody">
748 <h3 id="_the_setup">7.1. The setup</h3>
749 <div class="para"><p>So I have been building this application for the last month and feel pretty good about the ruby code. I'm readying it for beta testers when I discover to my shock that with less then twenty people it starts to crash. It's a pretty simple Ecommerce site so I'm very confused by what I'm seeing. On running looking through my log files I find to my shock that the lowest time for a page run is running around 240 ms. My database finds aren't the problems so I'm lost as to what is happening to cause all this. Lets run a benchmark.</p></div>
750 <div class="listingblock">
751 <div class="content"><!-- Generator: GNU source-highlight 2.9
752 by Lorenzo Bettini
753 http://www.lorenzobettini.it
754 http://www.gnu.org/software/src-highlite -->
755 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> HomepageTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>PerformanceTest
756 <span style="font-style: italic"><span style="color: #9A1900"># Replace this with your real tests.</span></span>
757 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_homepage
758 get <span style="color: #FF0000">'/'</span>
759 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
760 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
761 </tt></pre></div></div>
762 <div class="listingblock">
763 <div class="title">Example: Output</div>
764 <div class="content">
765 <pre><tt>HomepageTest#test_homepage (115 ms warmup)
766 process_time: 591 ms
767 memory: 3052.90 KB
768 objects: 59471</tt></pre>
769 </div></div>
770 <div class="para"><p>Obviously something is very very wrong here. 3052.90 Kb to load my minimal homepage. For Comparison for another site running well I get this for my homepage test.</p></div>
771 <div class="listingblock">
772 <div class="title">Example: Default</div>
773 <div class="content">
774 <pre><tt>HomepageTest#test_homepage (19 ms warmup)
775 process_time: 26 ms
776 memory: 298.79 KB
777 objects: 1917</tt></pre>
778 </div></div>
779 <div class="para"><p>that over a factor of ten difference. Lets look at our flat process time file to see if anything pops out at us.</p></div>
780 <div class="listingblock">
781 <div class="title">Example: Process time</div>
782 <div class="content">
783 <pre><tt>20.73 0.39 0.12 0.00 0.27 420 Pathname#cleanpath_aggressive
784 17.07 0.14 0.10 0.00 0.04 3186 Pathname#chop_basename
785 6.47 0.06 0.04 0.00 0.02 6571 Kernel#===
786 5.04 0.06 0.03 0.00 0.03 840 Pathname#initialize
787 5.03 0.05 0.03 0.00 0.02 4 ERB::Compiler::ExplicitScanner#scan
788 4.51 0.03 0.03 0.00 0.00 9504 String#==
789 2.94 0.46 0.02 0.00 0.44 1393 String#gsub
790 2.66 0.09 0.02 0.00 0.07 480 Array#each
791 2.46 0.01 0.01 0.00 0.00 3606 Regexp#to_s</tt></pre>
792 </div></div>
793 <div class="para"><p>Yes indeed we seem to have found the problem. Pathname#cleanpath_aggressive is taking nearly a quarter our process time and Pathname#chop_basename another 17%. From here I do a few more benchmarks to make sure that these processes are slowing down the other pages. They are so now I know what I must do. <strong>If we can get rid of or shorten these processes we can make our pages run much quicker</strong>.</p></div>
794 <div class="para"><p>Now both of these are main ruby processes so are goal right now is to find out what other process is calling them. Glancing at our Graph file I see that #cleanpath is calling #cleanpath_aggressive. #cleanpath is being called by String#gsub and from there some html template errors. But my page seems to be rendering fine. why would it be calling template errors. I'm decide to check my object flat file to see if I can find any more information.</p></div>
795 <div class="listingblock">
796 <div class="title">Example: Objects Created</div>
797 <div class="content">
798 <pre><tt>20.74 34800.00 12324.00 0.00 22476.00 420 Pathname#cleanpath_aggressive
799 16.79 18696.00 9978.00 0.00 8718.00 3186 Pathname#chop_basename
800 11.47 13197.00 6813.00 0.00 6384.00 480 Array#each
801 8.51 41964.00 5059.00 0.00 36905.00 1386 String#gsub
802 6.07 3606.00 3606.00 0.00 0.00 3606 Regexp#to_s</tt></pre>
803 </div></div>
804 <div class="para"><p>nope nothing new here. Lets look at memory usage</p></div>
805 <div class="listingblock">
806 <div class="title">Example: Memory Consuption</div>
807 <div class="content">
808 <pre><tt> 40.17 1706.80 1223.70 0.00 483.10 3186 Pathname#chop_basename
809 14.92 454.47 454.47 0.00 0.00 3606 Regexp#to_s
810 7.09 2381.36 215.99 0.00 2165.37 1386 String#gsub
811 5.08 231.19 154.73 0.00 76.46 420 Pathname#prepend_prefix
812 2.34 71.35 71.35 0.00 0.00 1265 String#initialize_copy</tt></pre>
813 </div></div>
814 <div class="para"><p>Ok so it seems Regexp#to_s is the second costliest process. At this point I try to figure out what could be calling a regular expression cause I very rarely use them. Going over my standard layout I discover at the top.</p></div>
815 <div class="listingblock">
816 <div class="content"><!-- Generator: GNU source-highlight 2.9
817 by Lorenzo Bettini
818 http://www.lorenzobettini.it
819 http://www.gnu.org/software/src-highlite -->
820 <pre><tt>&lt;%if request.env["HTTP_USER_AGENT"].match(/Opera/)%&gt;
821 &lt;%= stylesheet_link_tag "opera" %&gt;
822 &lt;% end %&gt;
823 </tt></pre></div></div>
824 <div class="para"><p>That's wrong. I mistakenly am using a search function for a simple compare function. Lets fix that.</p></div>
825 <div class="listingblock">
826 <div class="content"><!-- Generator: GNU source-highlight 2.9
827 by Lorenzo Bettini
828 http://www.lorenzobettini.it
829 http://www.gnu.org/software/src-highlite -->
830 <pre><tt>&lt;%if request.env["HTTP_USER_AGENT"] =~ /Opera/%&gt;
831 &lt;%= stylesheet_link_tag "opera" %&gt;
832 &lt;% end %&gt;
833 </tt></pre></div></div>
834 <div class="para"><p>I'll now try my test again.</p></div>
835 <div class="listingblock">
836 <div class="content">
837 <pre><tt>process_time: 75 ms
838 memory: 519.95 KB
839 objects: 6537</tt></pre>
840 </div></div>
841 <div class="para"><p>Much better. The problem has been solved. Now I should have realized earlier due to the String#gsub that my problem had to be with reqexp serch function but such knowledge comes with time. Looking through the mass output data is a skill.</p></div>
842 </div>
843 <h2 id="_get_yourself_a_game_plan">8. Get Yourself a Game Plan</h2>
844 <div class="sectionbody">
845 <div class="para"><p>You end up dealing with a large amount of data whenever you profile an application. It's crucial to use a rigorous approach to analyzing your application's performance else fail miserably in a vortex of numbers. This leads us to -</p></div>
846 <h3 id="_the_analysis_process">8.1. The Analysis Process</h3>
847 <div class="para"><p>I’m going to give an example methodology for conducting your benchmarking and profiling on an application. It is based on your typical scientific method.</p></div>
848 <div class="para"><p>For something as complex as Benchmarking you need to take any methodology with a grain of salt but there are some basic strictures that you can depend on.</p></div>
849 <div class="para"><p>Formulate a question you need to answer which is simple, tests the smallest measurable thing possible, and is exact. This is typically the hardest part of the experiment. From there some steps that you should follow are.</p></div>
850 <div class="ilist"><ul>
851 <li>
852 <p>
853 Develop a set of variables and processes to measure in order to answer this question!
854 </p>
855 </li>
856 <li>
857 <p>
858 Profile based on the question and variables. Key problems to avoid when designing this experiment are:
859 </p>
860 <div class="ilist"><ul>
861 <li>
862 <p>
863 Confounding: Test one thing at a time, keep everything the same so you don't poison the data with uncontrolled processes.
864 </p>
865 </li>
866 <li>
867 <p>
868 Cross Contamination: Make sure that runs from one test do not harm the other tests.
869 </p>
870 </li>
871 <li>
872 <p>
873 Steady States: If you’re testing long running process. You must take the ramp up time and performance hit into your initial measurements.
874 </p>
875 </li>
876 <li>
877 <p>
878 Sampling Error: Data should perform have a steady variance or range. If you get wild swings or sudden spikes, etc. then you must either account for the reason why or you have a sampling error.
879 </p>
880 </li>
881 <li>
882 <p>
883 Measurement Error: Aka Human error, always go through your calculations at least twice to make sure there are no mathematical errors. .
884 </p>
885 </li>
886 </ul></div>
887 </li>
888 <li>
889 <p>
890 Do a small run of the experiment to verify the design.
891 </p>
892 </li>
893 <li>
894 <p>
895 Use the small run to determine a proper sample size.
896 </p>
897 </li>
898 <li>
899 <p>
900 Run the test.
901 </p>
902 </li>
903 <li>
904 <p>
905 Perform the analysis on the results and determine where to go from there.
906 </p>
907 </li>
908 </ul></div>
909 <div class="para"><p>Note: Even though we are using the typical scientific method; developing a hypothesis is not always useful in terms of profiling.</p></div>
910 </div>
911 <h2 id="_other_profiling_tools">9. Other Profiling Tools</h2>
912 <div class="sectionbody">
913 <div class="para"><p>There are a lot of great profiling tools out there. Some free, some not so free. This is a sort list detailing some of them.</p></div>
914 <h3 id="_httperf">9.1. httperf</h3>
915 <div class="para"><p><a href="http://www.hpl.hp.com/research/linux/httperf/">http://www.hpl.hp.com/research/linux/httperf/</a></p></div>
916 <div class="para"><p>A necessary tool in your arsenal. Very useful for load testing your website.</p></div>
917 <div class="para"><p>#TODO write and link to a short article on how to use httperf. Anybody have a good tutorial availble.</p></div>
918 <h3 id="_rails_analyzer">9.2. Rails Analyzer</h3>
919 <div class="para"><p>The Rails Analyzer project contains a collection of tools for Rails. It's open source and pretty speedy. It's not being actively worked on but is still contains some very useful tools.</p></div>
920 <div class="ilist"><ul>
921 <li>
922 <p>
923 The Production Log Analyzer examines Rails log files and gives back a report. It also includes action_grep which will give you all log results for a particular action.
924 </p>
925 </li>
926 <li>
927 <p>
928 The Action Profiler similar to Ruby-Prof profiler.
929 </p>
930 </li>
931 <li>
932 <p>
933 rails_stat which gives a live counter of requests per second of a running Rails app.
934 </p>
935 </li>
936 <li>
937 <p>
938 The SQL Dependency Grapher allows you to visualize the frequency of table dependencies in a Rails application.
939 </p>
940 </li>
941 </ul></div>
942 <div class="para"><p>Their project homepage can be found at <a href="http://rails-analyzer.rubyforge.org/">http://rails-analyzer.rubyforge.org/</a></p></div>
943 <div class="para"><p>The one major caveat is that it needs your log to be in a different format from how rails sets it up specifically SyslogLogger.</p></div>
944 <h4 id="_sysloglogger">9.2.1. SyslogLogger</h4>
945 <div class="para"><p>SyslogLogger is a Logger work-alike that logs via syslog instead of to a file. You can add SyslogLogger to your Rails production environment to aggregate logs between multiple machines.</p></div>
946 <div class="para"><p>More information can be found out at <a href="http://rails-analyzer.rubyforge.org/hacks/classes/SyslogLogger.html">http://rails-analyzer.rubyforge.org/hacks/classes/SyslogLogger.html</a></p></div>
947 <div class="para"><p>If you don't have access to your machines root system or just want something a bit easier to implement there is also a module developed by Geoffrey Grosenbach</p></div>
948 <h4 id="_a_hodel_3000_compliant_logger_for_the_rest_of_us">9.2.2. A Hodel 3000 Compliant Logger for the Rest of Us</h4>
949 <div class="para"><p>Directions taken from
950 <a href="http://topfunky.net/svn/plugins/hodel_3000_compliant_logger/lib/hodel_3000_compliant_logger.rb">link to module file</a></p></div>
951 <div class="para"><p>Just put the module in your lib directory and add this to your environment.rb in it's config portion.</p></div>
952 <div class="listingblock">
953 <div class="content">
954 <pre><tt>require 'hodel_3000_compliant_logger'
955 config.logger = Hodel3000CompliantLogger.new(config.log_path)</tt></pre>
956 </div></div>
957 <div class="para"><p>It's that simple. Your log output on restart should look like this.</p></div>
958 <div class="listingblock">
959 <div class="title">Example: Hodel 3000 Example</div>
960 <div class="content">
961 <pre><tt>Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
962 Parameters: {"action"=&gt;"shipping", "controller"=&gt;"checkout"}
963 Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: \e
964 [4;36;1mBook Columns (0.003155)\e[0m \e[0;1mSHOW FIELDS FROM `books`\e[0m
965 Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: \e
966 [4;35;1mBook Load (0.000881)\e[0m \e[0mSELECT * FROM `books` WHERE (`books`.`id` = 1 AND (`books`.`sold` = 1)) \e[0m
967 Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: \e
968 [4;36;1mShippingAddress Columns (0.002683)\e[0m \e[0;1mSHOW FIELDS FROM `shipping_addresses`\e[0m
969 Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: \e
970 [4;35;1mBook Load (0.000362)\e[0m \e[0mSELECT ounces FROM `books` WHERE (`books`.`id` = 1) \e[0m
971 Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
972 Rendering template within layouts/application
973 Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
974 Rendering checkout/shipping
975 Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: \e
976 [4;36;1mBook Load (0.000548)\e[0m \e[0;1mSELECT * FROM `books`
977 WHERE (sold = 0) LIMIT 3\e[0m
978 Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: \e
979 [4;35;1mAuthor Columns (0.002571)\e[0m \e[0mSHOW FIELDS FROM `authors`\e[0m
980 Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
981 \e[4;36;1mAuthor Load (0.000811)\e[0m \e[0;1mSELECT * FROM `authors` WHERE (`authors`.`id` = 1) \e[0m
982 Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
983 Rendered store/_new_books (0.01358)
984 Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
985 Completed in 0.37297 (2 reqs/sec) | Rendering: 0.02971 (7%) | DB: 0.01697 (4%) | 200 OK [https://secure.jeffbooks/checkout/shipping]</tt></pre>
986 </div></div>
987 <h3 id="_palmist">9.3. Palmist</h3>
988 <div class="para"><p>An open source mysql query analyzer. Full featured and easy to work with. Also requires Hodel 3000
989 <a href="http://www.flyingmachinestudios.com/projects/">http://www.flyingmachinestudios.com/projects/</a></p></div>
990 <h3 id="_new_relic">9.4. New Relic</h3>
991 <div class="para"><p><a href="http://www.newrelic.com/">http://www.newrelic.com/</a></p></div>
992 <div class="para"><p>Pretty nifty performance tools, pricey though. They do have a basic free
993 service both for when in development and when you put your application into production. Very simple installation and signup.</p></div>
994 <div class="para"><p>#TODO more in-depth without being like an advertisement.</p></div>
995 <h4 id="_manage">9.4.1. Manage</h4>
996 <div class="para"><p>Like new relic a production monitoring tool.</p></div>
997 </div>
998 <h2 id="_changelog">10. Changelog</h2>
999 <div class="sectionbody">
1000 <div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/4">Lighthouse ticket</a></p></div>
1001 <div class="ilist"><ul>
1002 <li>
1003 <p>
1004 October 17, 2008: First revision by Pratik
1005 </p>
1006 </li>
1007 <li>
1008 <p>
1009 September 6, 2008: Initial version by Matthew Bergman &lt;<a href="mailto:MzbPhoto@gmail.com">MzbPhoto@gmail.com</a>&gt;
1010 </p>
1011 </li>
1012 </ul></div>
1013 </div>
1014
1015 </div>
1016 </div>
1017 </body>
1018 </html>