<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>OnRails.org: Tag Rails</title>
    <link>http://onrails.org/articles/tag/rails?tag=rails</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Ruby On Rails and related matters.</description>
    <item>
      <title>Installing RMagick on Leopard (without MacPorts or Fink)</title>
      <description>&lt;p&gt;I&amp;#8217;ve recently upgraded to &lt;span class="caps"&gt;OS X 10&lt;/span&gt;.5 (Leopard), and all-in-all, I&amp;#8217;m pleased with the experience.  My biggest issue has been the default stacks behavior&amp;#8212;the icon changes to the last thing added to the stack, making visual identification unnecessarily cumbersome.  I worked around this annoyance (as outlined &lt;a href="http://www.ryanirelan.com/past/2007/10/31/leopard-stack"&gt;here&lt;/a&gt;) by changing the sort to name rather than date added, and adding a dummy folder named &amp;#8220;_1&amp;#8221; that will sort to the top.  For extra bonus points, I customized the icon of the dummy folder.  For some yet unknown reason, the most recently downloaded item still peeks through from time to time, but it&amp;#8217;s much better than before.&lt;/p&gt;


	&lt;p&gt;Maybe it&amp;#8217;s my Windows history showing through, but I went with the &amp;#8220;clean-sweep&amp;#8221; erase and install method.  For a non-developer, I&amp;#8217;d probably recommend the upgrade (and in fact I used that method for my Father-in-law&amp;#8217;s MacBook), but I had lots of custom bits scattered about my machine, and didn&amp;#8217;t want to be chasing any incompatibility gremlins.&lt;/p&gt;


	&lt;p&gt;So now, to get my development environment set up on the new machine&amp;#8230;  Leopard includes a fairly complete Rails stack out of the box, with a non-&lt;a href="http://hans.fugal.net/yodl/blosxom.cgi/mac/ruby-pack.html"&gt;broken&lt;/a&gt; Ruby, readline support, and most of the commonly used gems.  Read more &lt;a href="http://trac.macosforge.org/projects/ruby/wiki/WhatsNewInLeopard"&gt;here&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;MySQL was not included, but the latest installer (mysql-5.0.45-osx10.4-i686.dmg) for 10.4 from dev.mysql.com downloads worked (mostly) fine.  The Server and the StartupItem install and operate correctly.  The PrefPane installs, but does not appear to actually do &amp;#8230; anything.  I&amp;#8217;ll have to work on that, but I can live without it for now.  After a bit of manual hacking on my database dump file from Tiger (where I was running a 5.1.x beta of MySQL), all my databases are back in place.&lt;/p&gt;


	&lt;p&gt;One last piece that I needed for my Rails apps&amp;#8212;RMagick.  I know it&amp;#8217;s possible to install RMagick and its dependencies, um, &amp;#8220;autoRMagickally&amp;#8221; via a package management system like MacPorts or Fink, but I prefer not to.  For some background on why not, you can read this &lt;a href="http://hivelogic.com/narrative/articles/using_usr_local"&gt;article at hivelogic&lt;/a&gt;.  The last time I was rebuilding my laptop and desktop near the same time, I put together a shell script to automate the process of installing RMagick.  I got it back out and dusted off the cobwebs, and voila!  RMagick on Leopard.  (note: replace wget with &amp;#8220;curl -O&amp;#8221;, if you don&amp;#8217;t have wget installed on your machine)  Here&amp;#8217;s the code:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;div class="codetitle"&gt;install_rmagick.sh&lt;/div&gt;&lt;pre&gt;&lt;code class="typocode_sh "&gt;#!/bin/sh
wget http://download.savannah.gnu.org/releases/freetype/freetype-2.3.5.tar.gz
tar xzvf freetype-2.3.5.tar.gz
cd freetype-2.3.5
./configure --prefix=/usr/local
make
sudo make install
cd ..

wget http://superb-west.dl.sourceforge.net/sourceforge/libpng/libpng-1.2.22.tar.bz2
tar jxvf libpng-1.2.22.tar.bz2
cd libpng-1.2.22
./configure --prefix=/usr/local
make
sudo make install
cd ..

wget ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6b.tar.gz
tar xzvf jpegsrc.v6b.tar.gz
cd jpeg-6b
ln -s `which glibtool` ./libtool
export MACOSX_DEPLOYMENT_TARGET=10.5
./configure --enable-shared --prefix=/usr/local
make
sudo make install
cd ..

wget ftp://ftp.remotesensing.org/libtiff/tiff-3.8.2.tar.gz
tar xzvf tiff-3.8.2.tar.gz
cd tiff-3.8.2
./configure --prefix=/usr/local
make
sudo make install
cd ..

wget http://jaist.dl.sourceforge.net/sourceforge/wvware/libwmf-0.2.8.4.tar.gz
tar xzvf libwmf-0.2.8.4.tar.gz
cd libwmf-0.2.8.4
make clean
./configure
make
sudo make install
cd ..

wget http://www.littlecms.com/lcms-1.17.tar.gz
tar xzvf lcms-1.17.tar.gz
cd lcms-1.17
make clean
./configure
make
sudo make install
cd ..

wget ftp://mirror.cs.wisc.edu/pub/mirrors/ghost/GPL/gs860/ghostscript-8.60.tar.gz
tar zxvf ghostscript-8.60.tar.gz
cd ghostscript-8.60/
./configure  --prefix=/usr/local
make
sudo make install
cd ..

wget ftp://mirror.cs.wisc.edu/pub/mirrors/ghost/GPL/current/ghostscript-fonts-std-8.11.tar.gz
tar zxvf ghostscript-fonts-std-8.11.tar.gz
sudo mv fonts /usr/local/share/ghostscript

wget http://imagemagick.site2nd.org/imagemagick/ImageMagick-6.3.5-9.tar.gz
tar xzvf ImageMagick-6.3.5-9.tar.gz
cd ImageMagick-6.3.5
export CPPFLAGS=-I/usr/local/include
export LDFLAGS=-L/usr/local/lib
./configure --prefix=/usr/local --disable-static --with-modules --without-perl --without-magick-plus-plus --with-quantum-depth=8 --with-gs-font-dir=/usr/local/share/ghostscript/fonts
make
sudo make install
cd ..

sudo gem install RMagick&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
      <pubDate>Sat, 03 Nov 2007 00:07:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:58aaed88-7522-4004-8863-8f57e819423c</guid>
      <author>Solomon White</author>
      <link>http://onrails.org/articles/2007/11/03/installing-rmagick-on-leopard-without-macports-or-fink</link>
      <category>Rails Tips</category>
      <category>Ruby On Rails</category>
      <category>Rails</category>
      <category>rmagick</category>
      <category>leopard</category>
    </item>
    <item>
      <title>Monitoring Rails Performance with Munin and a Mongrel</title>
      <description>&lt;p&gt;Rails makes things easy on developers&amp;#8212;maybe &lt;strong&gt;too&lt;/strong&gt; easy.  It&amp;#8217;s not uncommon to reference an association while iterating over a collection of objects, resulting in a performance-devouring N+1 queries being executed.  Of course, this is easily fixed with eager loading of the association, but you get my point.  Rails can be a big gun, and it&amp;#8217;s easy to blow off your foot.&lt;/p&gt;


	&lt;p&gt;Once your application is moved into production, it becomes important to keep an eye on performance over time in order to get a feel for trends and plan capacity intelligently.  Numerous Rails &lt;a href="http://rails-analyzer.rubyforge.org/"&gt;performance&lt;/a&gt; &lt;a href="http://cfis.savagexi.com/articles/2007/07/10/how-to-profile-your-rails-application"&gt;measuring&lt;/a&gt; &lt;a href="http://railsbench.rubyforge.org/"&gt;tools&lt;/a&gt; exist, but I find that it helps to correlate performance of your application with simultaneous lower-level performance metrics of your system (CPU and Memory usage, Load Average, etc).  Besides that, hey!  Pretty Graphs!  Enter &lt;a href="http://munin.projects.linpro.no/"&gt;munin&lt;/a&gt;, an open-source, extensible monitoring tool with a number of out-of-the-box plugins that are useful to SysAdmin type folks.  There&amp;#8217;s a pretty decent (though brief) &lt;a href="http://www.howtoforge.com/server_monitoring_monit_munin"&gt;writeup on howtoforge&lt;/a&gt; that explains a bit more about how to go about getting your own munin.  Go ahead and check it out&amp;#8212;I&amp;#8217;ll chill here until you&amp;#8217;re back.&lt;/p&gt;


&lt;center&gt;...&lt;/center&gt;

	&lt;p&gt;That wasn&amp;#8217;t too bad, huh?  Okay, so at this point you should have a working munin instance.  It will take a few minutes of data collection before your graphs look like anything, but going forward, you&amp;#8217;ll have historical graphs of several key metrics for managing your server(s).  It&amp;#8217;s outside the scope of this article, but you can also set up munin to monitor multiple servers on your network, and/or alert you when critical threshold levels are passed.&lt;/p&gt;


	&lt;p&gt;Well, knowing &lt;span class="caps"&gt;CPU&lt;/span&gt; usage is great, but it would also be nice to have some idea what the average user experience is like on your site.  Does your Rails app perform differently at different times of day?  How long does it typically take for your app to render a page?  Is the majority of the time spent in the database, or rendering?  Is the response time about the same between production deployment versions?  I wanted answers to these questions too, so I came up with a small ruby program that watches your Rails log in realtime, and when munin asks, provides summary information about the performance of your application.  Below, I&amp;#8217;ll go over the code section by section; for those who want it now, scroll to the bottom of the article to find the download link.&lt;/p&gt;


	&lt;p&gt;The basis of the solution is a Ruby array:  we stuff values into it, then compute the average of all the values and clear the array each time munin pings us.  Every time a value is added, we also check it against the maximum already seen, so we can report the maximum response time in addition to the average.  We keep three of these objects around, one each for DB, Rendering, and Total response times.  There is also a mode that lets you look at the current value without consuming it&amp;#8212;useful for peeking inside without affecting the data that munin will ultimately see.  Here&amp;#8217;s the class that implements this functionality:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;div class="codetitle"&gt;accumulator&lt;/div&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Accumulator&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;initialize&lt;/span&gt;
    &lt;span class="attribute"&gt;@values&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;Array&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;()&lt;/span&gt;
    &lt;span class="attribute"&gt;@max&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="number"&gt;0&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;add&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;value&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="attribute"&gt;@values&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="ident"&gt;value&lt;/span&gt;
    &lt;span class="attribute"&gt;@max&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;value&lt;/span&gt; &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="ident"&gt;value&lt;/span&gt; &lt;span class="punct"&gt;&amp;gt;&lt;/span&gt; &lt;span class="attribute"&gt;@max&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;average&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;read_only&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="constant"&gt;false&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="ident"&gt;return_value&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="attribute"&gt;@values&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;length&lt;/span&gt; &lt;span class="punct"&gt;==&lt;/span&gt; &lt;span class="number"&gt;0&lt;/span&gt;
      &lt;span class="constant"&gt;nil&lt;/span&gt;
    &lt;span class="keyword"&gt;else&lt;/span&gt;
      &lt;span class="attribute"&gt;@values&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;inject&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;0&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="punct"&gt;{|&lt;/span&gt;&lt;span class="ident"&gt;sum&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt;&lt;span class="ident"&gt;value&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;sum&lt;/span&gt; &lt;span class="punct"&gt;+&lt;/span&gt; &lt;span class="ident"&gt;value&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt; &lt;span class="punct"&gt;/&lt;/span&gt;&lt;span class="regex"&gt; @values.length
    end
    @values = Array.new() unless read_only

    return_value
  end

  def max(read_only=false)
    return_value = @max
    @max = 0 unless read_only
    return_value
  end
end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;In the next section of the code, we build our accumulators, and begin tailing the logfile to extract performance numbers.  This requires the &lt;a href="http://file-tail.rubyforge.org/"&gt;file-tail gem&lt;/a&gt;, available from rubyforge.  Note that in my setup, this file resides in a subdirectory under &lt;code&gt;lib&lt;/code&gt; in &lt;code&gt;RAILS_ROOT&lt;/code&gt;.  If you choose to place this file elsewhere, you&amp;#8217;ll have to adjust the path to the logfile accordingly.  Another thing to note:  in our environment, the load balancer continually pings a &amp;#8220;heartbeat&amp;#8221; action on each node to make sure it is still responsive.  As we will be hitting this action repeatedly, it is engineered to be as lightweight as possible.  Therefore, any numbers from it are pretty meaningless to our overall statistics, so we don&amp;#8217;t want to include them.  To keep these numbers from skewing our results, we define an &lt;code&gt;IGNORE_PATTERNS&lt;/code&gt; regexp (earlier in the code).  If the request matches a pattern we want to ignore, its statistics are not collected.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;div class="codetitle"&gt;tail&lt;/div&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="constant"&gt;LOGFILE&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;File&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;join&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;File&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;dirname&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;__FILE__&lt;/span&gt;&lt;span class="punct"&gt;),&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;..&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;..&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;log&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;&lt;span class="expr"&gt;#{RAILS_ENV}&lt;/span&gt;.log&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;)&lt;/span&gt;
&lt;span class="global"&gt;$response_data&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt; &lt;span class="symbol"&gt;:total&lt;/span&gt;     &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="constant"&gt;Accumulator&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;(),&lt;/span&gt;
                   &lt;span class="symbol"&gt;:rendering&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="constant"&gt;Accumulator&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;(),&lt;/span&gt;
                   &lt;span class="symbol"&gt;:db&lt;/span&gt;        &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="constant"&gt;Accumulator&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;()&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt;

&lt;span class="constant"&gt;Thread&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;abort_on_exception&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;true&lt;/span&gt;
&lt;span class="ident"&gt;logtail&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;Thread&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt;
  &lt;span class="constant"&gt;File&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Tail&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Logfile&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;tail&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;LOGFILE&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;line&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
    &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="ident"&gt;line&lt;/span&gt; &lt;span class="punct"&gt;=~&lt;/span&gt; &lt;span class="punct"&gt;/&lt;/span&gt;&lt;span class="regex"&gt;^Completed in &lt;/span&gt;&lt;span class="punct"&gt;/&lt;/span&gt;
      &lt;span class="ident"&gt;parts&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;line&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;split&lt;/span&gt;&lt;span class="punct"&gt;(/&lt;/span&gt;&lt;span class="regex"&gt;&lt;span class="escape"&gt;\s&lt;/span&gt;+&lt;span class="escape"&gt;\|\s&lt;/span&gt;+&lt;/span&gt;&lt;span class="punct"&gt;/)&lt;/span&gt;
      &lt;span class="ident"&gt;resp&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;parts&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;pop&lt;/span&gt;
      &lt;span class="ident"&gt;requested_url&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;resp&lt;/span&gt;&lt;span class="punct"&gt;[/&lt;/span&gt;&lt;span class="regex"&gt;http:&lt;span class="escape"&gt;\/\/&lt;/span&gt;[^&lt;span class="escape"&gt;\]&lt;/span&gt;]*&lt;/span&gt;&lt;span class="punct"&gt;/]&lt;/span&gt;
      &lt;span class="keyword"&gt;next&lt;/span&gt; &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="ident"&gt;requested_url&lt;/span&gt; &lt;span class="punct"&gt;=~&lt;/span&gt; &lt;span class="constant"&gt;IGNORE_PATTERNS&lt;/span&gt;

      &lt;span class="ident"&gt;parts&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;each&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;part&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
        &lt;span class="ident"&gt;part&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;gsub!&lt;/span&gt;&lt;span class="punct"&gt;(/&lt;/span&gt;&lt;span class="regex"&gt;Completed in&lt;/span&gt;&lt;span class="punct"&gt;/,&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;total&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;)&lt;/span&gt;
        &lt;span class="ident"&gt;type&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;time&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;pct&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;part&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;split&lt;/span&gt;&lt;span class="punct"&gt;(/&lt;/span&gt;&lt;span class="regex"&gt;&lt;span class="escape"&gt;\s&lt;/span&gt;+&lt;/span&gt;&lt;span class="punct"&gt;/)&lt;/span&gt;
        &lt;span class="ident"&gt;type&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;type&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;gsub&lt;/span&gt;&lt;span class="punct"&gt;(/&lt;/span&gt;&lt;span class="regex"&gt;:&lt;/span&gt;&lt;span class="punct"&gt;/,'&lt;/span&gt;&lt;span class="string"&gt;&lt;/span&gt;&lt;span class="punct"&gt;').&lt;/span&gt;&lt;span class="ident"&gt;downcase&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;to_sym&lt;/span&gt;

        &lt;span class="global"&gt;$response_data&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="ident"&gt;type&lt;/span&gt;&lt;span class="punct"&gt;].&lt;/span&gt;&lt;span class="ident"&gt;add&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;time&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;to_f&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
      &lt;span class="keyword"&gt;end&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;So now we have a thread busy gathering our data&amp;#8212;how can we expose the data to a munin plugin?  There are multiple ways to do this, but I chose to use a small &lt;span class="caps"&gt;HTTP&lt;/span&gt; server listening to requests from the local machine only.  We could build such a thing in Rails, but we really don&amp;#8217;t need about 90% of the features Rails has to offer.  Since we&amp;#8217;re running Rails as a Mongrel cluster, we already have a perfect tool at our disposal for writing small &lt;span class="caps"&gt;HTTP&lt;/span&gt; request handlers in Ruby:  Mongrel.  Here are a couple of pages about how to get started &lt;a href="http://www.urbanhonking.com/ideasfordozens/archives/2007/05/my_first_mongre.html"&gt;writing&lt;/a&gt; &lt;a href="http://adam.blogs.bitscribe.net/2007/05/06/howto-custom-mongrel-handlers/"&gt;Mongrel&lt;/a&gt; &lt;a href="http://errtheblog.com/post/8444"&gt;handlers&lt;/a&gt;&amp;#8212;it&amp;#8217;s pretty straightforward.  Here&amp;#8217;s our handler:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;div class="codetitle"&gt;mongrel handler&lt;/div&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;ResponseTimeHandler&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;Mongrel&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;HttpHandler&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;initialize&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;method&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="attribute"&gt;@method&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;method&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;process&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;request&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;response&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="ident"&gt;response&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;start&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;200&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;head&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt;&lt;span class="ident"&gt;out&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
      &lt;span class="ident"&gt;debug&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;Mongrel&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;HttpRequest&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;query_parse&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;request&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;params&lt;/span&gt;&lt;span class="punct"&gt;[&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;QUERY_STRING&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;]).&lt;/span&gt;&lt;span class="ident"&gt;has_key?&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;debug&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
      &lt;span class="ident"&gt;head&lt;/span&gt;&lt;span class="punct"&gt;[&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;Content-Type&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;]&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;text/plain&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
      &lt;span class="ident"&gt;output&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="global"&gt;$response_data&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;map&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;k&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt;&lt;span class="ident"&gt;v&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
        &lt;span class="ident"&gt;value&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;v&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;send&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="attribute"&gt;@method&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;debug&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
        &lt;span class="ident"&gt;formatted&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;value&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;nil?&lt;/span&gt; &lt;span class="punct"&gt;?&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;U&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt; &lt;span class="punct"&gt;:&lt;/span&gt; &lt;span class="ident"&gt;sprintf&lt;/span&gt;&lt;span class="punct"&gt;('&lt;/span&gt;&lt;span class="string"&gt;%.5f&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="ident"&gt;value&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;

        &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;&lt;span class="expr"&gt;#{k}&lt;/span&gt;.value &lt;span class="expr"&gt;#{formatted}&lt;/span&gt;&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
      &lt;span class="keyword"&gt;end&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;join&lt;/span&gt;&lt;span class="punct"&gt;(&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;&lt;span class="escape"&gt;\n&lt;/span&gt;&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;)&lt;/span&gt;
      &lt;span class="ident"&gt;output&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;&lt;span class="escape"&gt;\n&lt;/span&gt;&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
      &lt;span class="ident"&gt;out&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;write&lt;/span&gt; &lt;span class="ident"&gt;output&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;

&lt;span class="ident"&gt;h&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;Mongrel&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;HttpServer&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;(&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;127.0.0.1&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;,&lt;/span&gt; &lt;span class="constant"&gt;PORT&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
&lt;span class="ident"&gt;h&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;register&lt;/span&gt;&lt;span class="punct"&gt;(&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;/avg_response_time&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;,&lt;/span&gt; &lt;span class="constant"&gt;ResponseTimeHandler&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="symbol"&gt;:average&lt;/span&gt;&lt;span class="punct"&gt;))&lt;/span&gt;
&lt;span class="ident"&gt;h&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;register&lt;/span&gt;&lt;span class="punct"&gt;(&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;/max_response_time&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;,&lt;/span&gt; &lt;span class="constant"&gt;ResponseTimeHandler&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="symbol"&gt;:max&lt;/span&gt;&lt;span class="punct"&gt;))&lt;/span&gt;
&lt;span class="ident"&gt;h&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;run&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;join&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The handler is generic so that it can call an arbitrary method on our collection of data arrays, so we can set up one &lt;span class="caps"&gt;URI&lt;/span&gt; for the average, and one for the maximum.  With this in place, we can write a simple munin plugin script that uses &lt;code&gt;Net::HTTP&lt;/code&gt; to query our Mongrel to get at the performance data.&lt;/p&gt;


	&lt;p&gt;Basically, a munin plugin has two usage scenarios.  When called with the single argument &amp;#8220;config&amp;#8221;, it should output information about itself in a format that Munin understands.  This includes how to label the chart, scaling information, how many series will be included on the chart, etc.  When called with no arguments, the plugin should output the current values of each series.  For more information on writing your own munin plugin, start with the &lt;a href="http://munin.projects.linpro.no/wiki/HowToWritePlugins"&gt;HowToWritePlugins munin wiki page&lt;/a&gt;.  And now, our plugin script:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;div class="codetitle"&gt;rails_response_time&lt;/div&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="comment"&gt;#!/usr/bin/env ruby&lt;/span&gt;

&lt;span class="comment"&gt;# munin plugin to render rails response time graphs&lt;/span&gt;
&lt;span class="comment"&gt;# link to /etc/munin/plugins/avg_response_time and /etc/munin/plugins/max_response_time&lt;/span&gt;

&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;open-uri&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="constant"&gt;PORT&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;ENV&lt;/span&gt;&lt;span class="punct"&gt;['&lt;/span&gt;&lt;span class="string"&gt;PORT&lt;/span&gt;&lt;span class="punct"&gt;']&lt;/span&gt; &lt;span class="punct"&gt;||&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;8888&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;

&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;config&lt;/span&gt;
  &lt;span class="ident"&gt;title&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;File&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;basename&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="global"&gt;$0&lt;/span&gt;&lt;span class="punct"&gt;).&lt;/span&gt;&lt;span class="ident"&gt;split&lt;/span&gt;&lt;span class="punct"&gt;('&lt;/span&gt;&lt;span class="string"&gt;_&lt;/span&gt;&lt;span class="punct"&gt;').&lt;/span&gt;&lt;span class="ident"&gt;map&lt;/span&gt;&lt;span class="punct"&gt;{|&lt;/span&gt;&lt;span class="ident"&gt;s&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;s&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;capitalize&lt;/span&gt; &lt;span class="punct"&gt;}.&lt;/span&gt;&lt;span class="ident"&gt;join&lt;/span&gt;&lt;span class="punct"&gt;('&lt;/span&gt;&lt;span class="string"&gt; &lt;/span&gt;&lt;span class="punct"&gt;')&lt;/span&gt;
  &lt;span class="ident"&gt;config&lt;/span&gt;&lt;span class="punct"&gt;=&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="constant"&gt;__END_CONFIG__&lt;/span&gt;&lt;span class="string"&gt;
graph_title &lt;span class="expr"&gt;#{title}&lt;/span&gt;
graph_vlabel response time
graph_category rails
total.label total
rendering.label rendering
db.label db
&lt;/span&gt;&lt;span class="constant"&gt;__END_CONFIG__&lt;/span&gt;
  &lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="ident"&gt;config&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;

&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;get_data&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;read_only&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="constant"&gt;false&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
  &lt;span class="ident"&gt;qs&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;read_only&lt;/span&gt; &lt;span class="punct"&gt;?&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;?debug&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt; &lt;span class="punct"&gt;:&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
  &lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="ident"&gt;open&lt;/span&gt;&lt;span class="punct"&gt;(&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;http://127.0.0.1:&lt;span class="expr"&gt;#{PORT}&lt;/span&gt;/&lt;span class="expr"&gt;#{File.basename($0)}#{qs}&lt;/span&gt;&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;).&lt;/span&gt;&lt;span class="ident"&gt;read&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;

&lt;span class="keyword"&gt;case&lt;/span&gt; &lt;span class="constant"&gt;ARGV&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;first&lt;/span&gt;
&lt;span class="keyword"&gt;when&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;config&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
  &lt;span class="ident"&gt;config&lt;/span&gt;
&lt;span class="keyword"&gt;when&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;debug&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
  &lt;span class="ident"&gt;get_data&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;true&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
&lt;span class="keyword"&gt;else&lt;/span&gt;
  &lt;span class="ident"&gt;get_data&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The script will examine the name with which you linked it in to the munin plugins directory to determine which &lt;span class="caps"&gt;URI&lt;/span&gt; to query.  I have also added a debug mode that will show you the current values, so you&amp;#8217;re not consuming any data that munin needs to see for an accurate graph.  The final piece is a small &lt;a href="http://daemons.rubyforge.org/"&gt;Daemons&lt;/a&gt; wrapper script to control the main log-tailing process, and you should be set.  Make sure to restart munin-node so it will notice the new plugins, and after a while, you&amp;#8217;ll see something like this:&lt;/p&gt;


	&lt;p&gt;&lt;img src="/files/avg_response_time.png" alt="" /&gt;&lt;/p&gt;


	&lt;p&gt;It is worth noting that the numbers from the Rails log might not be 100% accurate, and this won&amp;#8217;t replace the results that you can get from seriously profiling your application.  Also, the information you are getting is a bit generic&amp;#8212;all actions are lumped together, so there is not a lot of information about the cause of the performance problem.  But, for insight into your production application performance, this setup should at least give you some indications about how well your baby is playing in the interwebs.&lt;/p&gt;


	&lt;p&gt;Download &lt;a href="/files/rails_log_monitor.rb"&gt;rails_log_monitor.rb&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;Download &lt;a href="/files/rails_response_time"&gt;rails_response_time&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Fri, 31 Aug 2007 18:29:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:e71c0dbd-1196-4b9f-8faf-ce5e92b1bee0</guid>
      <author>Solomon White</author>
      <link>http://onrails.org/articles/2007/08/31/monitoring-rails-performance-with-munin-and-a-mongrel</link>
      <category>Ruby On Rails</category>
      <category>Rails Tips</category>
      <category>mongrel</category>
      <category>munin</category>
      <category>performance</category>
      <category>monitoring</category>
      <category>Rails</category>
      <enclosure url="http://onrails.org/files/rails_log_monitor.rb" length="2411" type="application/octet-stream"/>
    </item>
    <item>
      <title>TextMate filetype detection for script/runner Rails scripts</title>
      <description>&lt;p&gt;So you&amp;#8217;re building some righteous automation for your killer web 2.0 app, placing scripts in &lt;code&gt;RAILS_ROOT/script&lt;/code&gt; that you can call from cron for nightly maintenance, etc.  To bootstrap your rails environment, you decide to use the shebang feature of script/runner, available since &lt;a href="http://dev.rubyonrails.org/changeset/5189"&gt;changeset 5189&lt;/a&gt;.  When you start to edit the script in TextMate (you &lt;strong&gt;are&lt;/strong&gt; using TextMate, aren&amp;#8217;t you?) there is no syntax highlighting to be found!  It&amp;#8217;s all plain text with no colors, and none of your ever-so-helpful keyboard macros work!  Frightful.  Well, take a deep breath, because together, we&amp;#8217;re going to get the filetype detection magic working for you.&lt;/p&gt;


	&lt;p&gt;Before we get started, it&amp;#8217;s helpful to know how filetype detection works.  TextMate does a couple of different types of filetype detection&amp;#8212;the first is based off of the extension, so if you named your script with a &lt;code&gt;.rb&lt;/code&gt; extension, you are probably wondering what in the world I&amp;#8217;m rambling about.  Dude.  It just works.&lt;/p&gt;


	&lt;p&gt;However, if you followed the rails convention for scripts, and did not use an extension with your filename, keep reading.  The second type of detection works by scanning the so called &amp;#8220;shebang&amp;#8221; line at the top of the script which tells the shell (and in this case TextMate) which interpreter to use to evaluate your script&amp;#8212;this is how we will tell TextMate that script/runner really means ruby.&lt;/p&gt;


	&lt;p&gt;First of all, you&amp;#8217;ll need to fire up the Bundle Editor and select &amp;#8220;Languages&amp;#8221; from the drop-down filter.  Expand the &amp;#8220;Rails&amp;#8221; node, and then select the &amp;#8220;Ruby on Rails&amp;#8221; language.  On the right side, you should see the definition being used by TextMate to detect the Ruby on Rails scope.  If you have not modified your bundle, you&amp;#8217;ll probably see that it is using a &lt;code&gt;fileTypes&lt;/code&gt; to look for &lt;code&gt;.rxml&lt;/code&gt; files.  This is where we want to insert the following line:&lt;/p&gt;


	&lt;p&gt;&lt;code&gt;    firstLineMatch = '^#!.*(script/runner)';&lt;/code&gt;&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s a screenshot of what it should look like when you are done:
&lt;img src="http://onrails.org/files/BundleEditor-ROR.gif" alt="" /&gt;&lt;/p&gt;


	&lt;p&gt;Now go back to your script and enjoy all the colorized, scope-aware editing goodness that is TextMate!&lt;/p&gt;</description>
      <pubDate>Fri, 20 Apr 2007 14:54:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:5c2e6ed5-7d4d-469e-ba06-b9b34d68c913</guid>
      <author>Solomon White</author>
      <link>http://onrails.org/articles/2007/04/20/textmate-filetype-detection-for-script-runner-rails-scripts</link>
      <category>Rails Tips</category>
      <category>Ruby On Rails</category>
      <category>Rails</category>
      <category>script/runner</category>
      <category>textmate</category>
    </item>
    <item>
      <title>Short circuiting partials</title>
      <description>I tend to think of partials as mini-components.  I don't like it when the page using the partial "knows" too much about what's in the partial.  For instance, if your partial displays a collection of things but shouldn't display anything if the collection is empty, then you'll usually see something like this:

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;outer page stuff...
&amp;lt;% unless some_object.has_many_things.empty? %&amp;gt;
  &amp;lt;h3&amp;gt;My collection of stuff&amp;lt;/h3&amp;gt;
  &amp;lt;%= render :partial =&amp;gt; 'my_partial', :locals =&amp;gt; { :collection_of_stuff =&amp;gt; some_object.has_many_things } %&amp;gt;
&amp;lt;% end %&amp;gt;
more outer page stuff...&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

The outer page has to know which collection of things the partial is going to use and performs some display logic around that.  Shouldn't this all be contained in the partial?  The outer page should just know that it is going to use a partial and that's it.  So our outer page will look like this:

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;outer page stuff...
&amp;lt;%= render :partial =&amp;gt; 'my_partial', :locals =&amp;gt; { :some_object =&amp;gt; some_object } %&amp;gt;
more outer page stuff...&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

Now it looks like a self-contained component.  Good.  Of course, we now have to put that outer display logic in the partial.

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;&amp;lt;% unless some_object.has_many_things.empty? %&amp;gt;
  &amp;lt;h3&amp;gt;My collection of stuff&amp;lt;/h3&amp;gt;
  &amp;lt;% some_object.has_many_things.each do |o| %&amp;gt;
    do some stuff with &amp;lt;%= o %&amp;gt;
  &amp;lt;% end %&amp;gt;
&amp;lt;% end %&amp;gt;&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

Now the messy part is the big &lt;code&gt;unless&lt;/code&gt; block around the whole partial.  I'd like to just short circuit the whole page if there's nothing to display, similar to what I do in normal methods:

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;def do_something(some_arg)
  return nil unless some_arg
  do_something_cool_with_some_arg
end&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

Can it be as simple as putting a &lt;code&gt;return&lt;/code&gt; statement in our partial?  It turns out, yes, it is that easy.

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;&amp;lt;% return if some_object.has_many_things.empty? %&amp;gt;
&amp;lt;h3&amp;gt;My collection of stuff&amp;lt;/h3&amp;gt;
&amp;lt;% some_object.has_many_things.each do |o| %&amp;gt;
  do some stuff with &amp;lt;%= o %&amp;gt;
&amp;lt;% end %&amp;gt;&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

Wow!  One whole line shorter.  It doesn't look like much in this small contrived example, but I think it makes the purpose of the partial clearer and doesn't clutter up the calling page.  Give it a shot.</description>
      <pubDate>Tue, 15 Aug 2006 16:26:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:bb8436b2-87db-4382-bdf9-e61505efcb04</guid>
      <author>Lee Marlow</author>
      <link>http://onrails.org/articles/2006/08/15/short-circuiting-partials</link>
      <category>Rails</category>
      <category>partials</category>
      <category>components</category>
      <category>views</category>
    </item>
    <item>
      <title>RailsLogAnalyzer – help wanted</title>
      <description>&lt;p&gt;I need some Mac users to test and give feed back on a very early version of the RailsLogAnalyzer.  This app is an &lt;span class="caps"&gt;OSX&lt;/span&gt; app and not deployed on a server, so I would like to find out the obvious bug that may occur on different Macs. I tried it on my own MacBook Pro, G5    server, and on an old PowerBook G4. But then again the people most likely to use it are Rails developers that may have differences in their environment.&lt;/p&gt;


	&lt;p&gt;So if you like to take risks :-), understand the basic structure of a Rails app, have a production.log you want to analyze, and are not afraid of some scary bugs then read on&amp;#8230;&lt;/p&gt;&lt;p&gt;The RailsLogAnalyzer is an &lt;span class="caps"&gt;OSX&lt;/span&gt; application that allows to visualize the production.log of a Rails application. The RailsLogAnalyzer is itself a Rails application, with a Flash user interface, and wrapped as an &lt;span class="caps"&gt;OSX&lt;/span&gt; app using Platypus.&lt;/p&gt;


	&lt;p&gt;To use the application you &amp;#8216;just&amp;#8217; need to drop it in your application folder (or where ever you want) and double click it. See the &amp;#8216;user guide&amp;#8217; here after.&lt;/p&gt;


	&lt;p&gt;I wrote this small utility to help me see how users where using different Rails applications I was working on by visualizing the production log data. This utility is just doing what I needed and is pretty basic. It&amp;#8217;s definitively not ready for prime time, however your input will help drive it&amp;#8217;s development.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Requirements:&lt;/strong&gt;&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;You need the Flash Player 9. For intel macs the player is in beta and can be downloaded here http://fpdownload.macromedia.com/get/flashplayer/current/fp9_beta/install_flash_player_9_ub_beta.dmg. More info here http://www.adobe.com/products/flashplayer/public_beta/. For PowerPC macs, just starting the application will open a browser window that will guide you through the flash player upgrade if you don&amp;#8217;t have version 9 installed.&lt;/li&gt;
		&lt;li&gt;Port 3000 needs to be free. So don&amp;#8217;t run any other Rails app when you launch the application. &lt;/li&gt;
		&lt;li&gt;Don&amp;#8217;t click too fast. See the user guide here after for more information.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;&lt;strong&gt;What the application does&lt;/strong&gt;&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Show graph of unique session and total request per day.&lt;/li&gt;
		&lt;li&gt;Show pie chart of controller usage (at end of the period observed) .&lt;/li&gt;
		&lt;li&gt;Show the top ten actions invoked (can be filter by http method and controller)&lt;/li&gt;
		&lt;li&gt;Show the graphical geoip information ((at end of the period observed). [Very buggy]&lt;/li&gt;
		&lt;li&gt;Allow time navigation (30 days/7 days increment)&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;&lt;strong&gt;What it doesn&amp;#8217;t do so well&lt;/strong&gt;&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Visual feedback when processing. You gotta check the launch window output to see if it&amp;#8217;s still busy.&lt;/li&gt;
		&lt;li&gt;Disable buttons when processing. You can there overload the server if you are not patient.&lt;/li&gt;
		&lt;li&gt;Report errors. In corner cases you have to open the log file that are inside the application pacakage. Use the &amp;#8216;Show Package Contents&amp;#8217; on the application then goto to /Contents/Resources/RailsLogAnalyzerPackage/log folder.&lt;/li&gt;
		&lt;li&gt;Time aggregation. It would be nice to see data aggregated by week/month.&lt;/li&gt;
		&lt;li&gt;Local File Browsing to upload the log file. Currently you need to enter the full path to the file, there is not UI to select a file.&lt;/li&gt;
		&lt;li&gt;Log parsing. Certainly can be greatly improved. Haven&amp;#8217;t tried to parse development files. Wouldn&amp;#8217;t support custom log files either. Doesn&amp;#8217;t support well multi processes logging to the same production.log (which is most cases).&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;&lt;strong&gt;Licenses&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;Well, I still need to put a complete list of what I use and the implications there off. Mostly Rails which is &lt;span class="caps"&gt;MIT&lt;/span&gt; and the GeoLiteCity.dat from maxmind.com/ that is &lt;span class="caps"&gt;GPL &lt;/span&gt;. Note the GeoLiteCity.dat file is 24mb big, thus at least partly the large size of the application. Also I am using the Flex Charting Trial components, that&amp;#8217;s visible when the application is running. My trial is running out in 17days as of this writing. I would like to buy the version of Flex Builder with Charting, but Adobe hasn&amp;#8217;t answered yet if buying the license for Windows would grant me usage of the version for &lt;span class="caps"&gt;OSX&lt;/span&gt;, when it comes out.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Performances&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;The application doesn&amp;#8217;t provide the best visual feed back yet. It also takes about 1 minute on my MacBook Pro to load a 20Mb production log file and 13 minutes for a 264Mb production log.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Side notes&lt;/strong&gt;&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;The database is a sqlite3 and is created in your  /Users/daniel/Library/Application\ Support/RailsLogAnalyzer&lt;/li&gt;
		&lt;li&gt;Note the application must be &amp;#8216;read-write&amp;#8217;. Use &amp;#8216;Get Info&amp;#8217; on the application and ensure in the &amp;#8216;Ownership &amp;#38; Permissions&amp;#8217; that the application is &amp;#8216;Read &amp;#38; Write&amp;#8217;. This is due to the fact that rails needs to write to the tmp folder and log folder that are embedded in the application it self.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Please provide feedback to daniel@onrails.org. 
Thanks in advance,
Daniel.&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://onrails.org/RailsLogAnalyzer0.1.dmg "&gt;Download RailsLogAnalyzer0.1.dmg (18.6MB)&lt;/a&gt;&lt;br&gt;
&lt;a href="http://onrails.org/pages/RailsLogAnalyzerUserGuide.html" target="_blank"&gt;User Guide&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 03 Aug 2006 22:37:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:19782f89-ac13-4b7c-afc0-7b3153053b90</guid>
      <author>Daniel Wanja</author>
      <link>http://onrails.org/articles/2006/08/03/railsloganalyzer-%E2%80%93-help-wanted</link>
      <category>Playground</category>
      <category>Ruby On Rails</category>
      <category>Flex</category>
      <category>Analyzer</category>
      <category>Log</category>
      <category>Rails</category>
    </item>
  </channel>
</rss>
