Benchmark and profile your Rails apps
This guide covers the various ways of performance testing a Ruby on Rails application.
After reading this guide, you will know:
Performance testing 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 pleasant browsing experience for end users and cutting the cost of unnecessary hardware is important for any non-trivial web application.
As of rails 4 performance tests are no longer part of the default stack. If you want to use performance tests simply follow these instructions.
Add this line to your application's Gemfile:
gem 'rails-perftest'If you want to benchmark/profile under MRI or REE, add this line as well:
gem 'ruby-prof'Now run bundle install and you're ready to go.
Rails performance tests are a special type of integration tests, designed for benchmarking and profiling the test code. With performance tests, you can determine where your application's memory or speed problems are coming from, and get a more in-depth picture of those problems.
Rails provides a generator called performance_test for creating new
performance tests:
$ rails generate performance_test homepageThis generates homepage_test.rb in the test/performance directory:
require 'test_helper'
require 'rails/performance_test_help'
class HomepageTest "stuff")
project.create_manager("name" => "David")
project.milestones
Rails log files contain very useful information about the time taken to serve each request. Here's a typical log file entry:
Processing ItemsController#index (for 127.0.0.1 at 2009-01-08 03:06:39) [GET]
Rendering template within layouts/items
Rendering items/index
Completed in 5ms (View: 2, DB: 0) | 200 OK [http://0.0.0.0/items]For this section, we're only interested in the last line:
Completed in 5ms (View: 2, DB: 0) | 200 OK [http://0.0.0.0/items]This data is fairly straightforward to understand. Rails uses millisecond(ms) as the metric to measure 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.
Rails has been lucky to have a few companies dedicated to Rails-specific performance tools:
No open issues yet, or sync has not completed.