profile snippets

How to profile your Rails and Ruby applications with ruby-prof

Tagged ruby-prof, rails, gem, profiling, benchmark, profile, kcachegrind  Languages ruby

Installing ruby-prof

First install ruby-prof:

git clone git://
cd ruby-prof/
rake gem
sudo gem install pkg/ruby-prof-0.6.1.gem

Note that version 0.6.0 doesn't work, at least not with Rails 2.1.1. With 0.6.0 I got this message:

gem install ruby-prof to use the profiler

Setting up a new environment for profiling

Create config/environments/profiling.rb:

config.cache_classes = true
config.action_controller.consider_all_requests_local = false
config.action_controller.perform_caching             = true
config.action_view.cache_template_loading            = true

#config.log_level = :debug

Add the new environment to database.yml. You might want to reuse the development database.

Creating a profiling script

Next we'll create a script that simply fetches the homepage, save the following code in profiling/homepage.rb:

get '/'
say "GET / => #{path}"

Run the script

Now run the script 100 times:

RAILS_ENV=profiling ./script/performance/request -n 100 profiling/homepage.rb

Profiling plain Ruby applications

You can also profile a block of code by calling RubyProf from your code:

require 'ruby-prof'

# Profile the code
[code to profile]
results = RubyProf.stop "#{RAILS_ROOT}/tmp/profile-graph.html", 'w' do |file|
end "#{RAILS_ROOT}/tmp/profile-flat.txt", 'w' do |file|
end "#{RAILS_ROOT}/tmp/", 'w' do |file|

Analyzing results

I prefer to use the RubyProf::CallTreePrinter to output data that kcachegrind can read. The HTML and text data is difficult to read so kcachegrind will definitely make your life easier.

On OSX you can install kcachegrind with Fink (or DarwinPorts):

sudo apt-get update ; sudo apt-get install fink
sudo apt-get install kcachegrind

There's also WinCacheGrind and MacCacheGrind, but I haven't tried those.

How to create a separate development profile for Firefox

Tagged firefox, development, profile, mode  Languages bash

Extensions slow down Firefox so I prefer disabling all but the most important Firefox extensions when coding.

Here's how to do it:

  1. First, create a development profile following these instructions
  1. Next, create a shortcut that starts Firefox in development mode:
# Mac OSX
/Applications/ -P development -no-remote

# Linux
/usr/lib/firefox/firefox-bin -P development -no-remote

# Windblows
"C:\Program Files\Mozilla Firefox\firefox.exe" -P development -no-remote