forked from eifion/asciicasts.com-translations
/
en.html
executable file
·107 lines (79 loc) · 7.69 KB
/
en.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<p>This episode will show three different profiling tools that you can use with your Rails applications. They are all designed to work in development mode and while you should be careful not to do too much optimization and profiling while in development mode these tools can be really helpful in giving you an idea what Rails and your application are doing under the hood. Note that a couple of the tools are from commercial companies, but the versions we’ll be using are all free.</p>
<h3>New Relic</h3>
<p><a href="http://www.newrelic.com/">New Relic</a> is best known for its RPM monitoring tool which provides an excellent way of monitoring production Rails applications. Less well known is the <a href="http://www.newrelic.com/developer-mode.html">developer mode version</a> which can be used to see what’s going on in your application as you develop it.</p>
<div class="imageWrapper">
<img src="/system/photos/93/original/E161I01.png" width="926" height="613" alt="The New Relic Developer Mode webpage." />
</div>
<p>RPM Developer Mode is installed as a plugin. From the application’s directory we need to run</p>
<pre class="terminal">
script/plugin install http://newrelic.rubyforge.org/svn/newrelic_rpm
</pre>
<p>to install it. Once it’s installed we can start up our application in development mode and visit some of the pages we’d like to profile. When we’ve done that we can visit <code>http://localhost:3000/newrelic</code> and we’ll see the requests that have been processed and the time taken for each one.</p>
<div class="imageWrapper">
<img src="/system/photos/94/original/E161I02.png" width="926" height="613" alt="New Relic RPM showing the time taken for a number of requests." />
</div>
<p>We can click on the details for any given request and view more details on how long each section of the page takes to render.</p>
<div class="imageWrapper">
<img src="/system/photos/95/original/E161I03.png" width="926" height="613" alt="Details for a single request." />
</div>
<p>We can also see all of the SQL requests that are made for a given request.</p>
<div class="imageWrapper">
<img src="/system/photos/96/original/E161I04.png" width="926" height="613" alt="The database calls made for a single request." />
</div>
<p>We can even view details for a single SQL call including a stack trace to show on which line in the application the request was made.</p>
<div class="imageWrapper">
<img src="/system/photos/97/original/E161I05.png" width="926" height="613" alt="Details about a single database call." />
</div>
<p>Overall NewRelic RPM can give you a lot of useful information about your application.</p>
<h3>FiveRuns TuneUp</h3>
<div class="imageWrapper">
<img src="/system/photos/98/original/E161I06.png" width="927" height="546" alt="The FiveRuns TuneUp web page." />
</div>
<p>The next profiling tool we’ll demonstrate is <a href="https://tuneup.fiveruns.com/">FiveRuns TuneUp</a>. Unlike RPM, this is installed as a gem.</p>
<pre class="terminal">sudo gem install fiveruns_tuneup</pre>
<p>Once installed we can add TuneUp to our application by adding a reference to the gem in <code>/config/environment.rb</code>.</p>
<pre class="terminal">
config.gem "fiveruns_tuneup"
</pre>
<p>Now, when we start up the server and visit a page in our application there will be an extra toolbar displayed at the top that will show us information about our application’s performance.</p>
<div class="imageWrapper">
<img src="/system/photos/99/original/E161I07.png" width="926" height="613" alt="The TuneUp toolbar." />
</div>
<p>The toolbar shows the time taken to process the request and a breakdown of the relative time spent processing the models, view and controller.</p>
<div class="imageWrapper">
<img src="/system/photos/100/original/E161I08.png" width="926" height="613" alt="The TuneUp toolbar showing details about a find." />
</div>
<p>If we hover over the breakdown we’ll see more details about the different parts of the request including the time spent rendering the views and partials, and the time taken to make database calls. For the <code>find</code>s called by the models the generated SQL run against the database can be viewed and we can see the schemas for the database tables. Clicking the pencil icon next to a view will, if you have TextMate installed, take you to the exact lines of code where that rendering takes place.</p>
<p>If a request is running slowly and you’re not sure why you can click the “Login to Share this Run” on the toolbar and publish the request’s profile on FiveRuns website (either privately or publicly) and get help to optimize or debug it.</p>
<h3>Rack::Bug</h3>
<p>The final profiling tool we’ll cover is <a href="http://www.brynary.com/2009/4/22/rack-bug-debugging-toolbar-in-four-minutes">Rack::Bug</a>. Like TuneUp it puts a toolbar at the top of the pages of your application that provides a wealth of information about each request. As it makes use of Middleware it can only be used with applications running under Rails 2.3 or later. Rack::Bug is installed as a plugin and can be installed from Github.</p>
<pre class="terminal">
script/plugin install git://github.com/brynary/rack-bug.git
</pre>
<p>After installing the plugin we need to create a <code>config/initializers/middleware.rb</code> file that has the following content.</p>
<pre class="ruby">
# config/initializers/middleware.rb
require "rack/bug"
ActionController::Dispatcher.middleware.use Rack::Bug,
:ip_masks => [IPAddr.new("127.0.0.1")],
:secret_key => "epT5uCIchlsHCeR9dloOeAPG66PtHd9K8l0q9avitiaA/KUrY7DE52hD4yWY+8z1",
:password => "rack-bug-secret"
</pre>
<p>The <code>secret_key</code> and <code>password</code> values should be changed before using Rack::Bug. Once that’s done we can start up our server. To use Rack::Bug we’ll first need to visit a special path.</p>
<pre class="terminal">
http://localhost:3000/__rack_bug__/bookmarklet.html
</pre>
<p>This path will take us to a page with nothing but a link on it. If we drag the link up to our bookmarks bar a bookmarklet will be created that will allow us to turn Rack::Bug on and off.</p>
<div class="imageWrapper">
<img src="/system/photos/101/original/E161I09.png" width="926" height="284" alt="Drag the link to the bookmarks bar." />
</div>
<p>After we’ve created the bookmarklet we can visit a page in our application. Clicking the bookmarklet will bring up a dialog box in which we can enter the password we defined in the file above. When we do that an alert box that says “Rack::Bug Enabled” should appear. If we then refresh the page we’ll see the Rack::Bug toolbar at the top.</p>
<div class="imageWrapper">
<img src="/system/photos/102/original/E161I10.png" width="927" height="546" alt="Rack::Bug’s toolbar." />
</div>
<p>Each link in the toolbar opens up a panel that will show information about a different part of the request, from information about our Rails environment to a list of the request variables and the SQL queries that were made.</p>
<div class="imageWrapper">
<img src="/system/photos/103/original/E161I11.png" width="927" height="546" alt="Rack::Bug’s toolbar showing the database calls made by a request." />
</div>
<p>Other tabs let us view the number of ActiveRecord objects our request generates and the templates used to render the file.</p>
<p>Whichever profiling tool you choose for your Rails app you’ll find that it provides a host of useful information for understanding how your application is working. Remember though that it is that when your application is live and in production mode that you’ll gain the best understanding about how your application is performing.</p>