Skip to content

Instantly share code, notes, and snippets.

@schmurfy
Created October 29, 2008 13:04
Show Gist options
  • Save schmurfy/20687 to your computer and use it in GitHub Desktop.
Save schmurfy/20687 to your computer and use it in GitHub Desktop.
rails profiling
Installing ruby-prof
First install ruby-prof 0.6.1:
1 git clone git://github.com/jeremy/ruby-prof.git
2 cd ruby-prof/
3 rake gem
4 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:
1 `gem install ruby-prof` to use the profiler
Setting up a new environment for profiling
Create config/environments/profiling.rb:
1 config.cache_classes = true
2 config.action_controller.consider_all_requests_local = false
3 config.action_controller.perform_caching = true
4 config.action_view.cache_template_loading = true
5
6 #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:
1 get '/'
2 say "GET / => #{path}"
Run the script
Now run the script 100 times:
1 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:
1 require 'ruby-prof'
2
3 # Profile the code
4 RubyProf.start
5 ...
6 [code to profile]
7 ...
8 result = RubyProf.stop
9
10 # Print a flat profile to text
11 printer = RubyProf::FlatPrinter.new(result)
12 printer.print(STDOUT, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment