It’s not quite straight forward to run Rails performance tests on real production data. By default, performance tests will use the test database and wipe it before using it, thus making it impossible to put real data in the test database.
To run performance tests on real data:
- Create a file called performance_test_helper.rb inside your test/ directory with the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# START : HAX HAX HAX # Load Rails environment in 'test' mode RAILS_ENV = "test" require File.expand_path(File.dirname(__FILE__) + "/../config/environment") # Re-establish db connection for 'performance' mode silence_warnings { RAILS_ENV = "performance" } ActiveRecord::Base.establish_connection # STOP : HAX HAX HAX require_dependency 'application' require 'test/unit' require 'active_support/test_case' require 'action_controller/test_case' require 'action_controller/integration' require 'performance_test_help' |
- Replace the following lines in performance tests:
1 2 |
require 'test_helper' require 'performance_test_help' |
with
require 'performance_test_helper' |
- Supply configuration for ‘performance’ environment in your database.yml:
1 2 3 4 5 6 7 8 |
performance: adapter: mysql encoding: utf8 database: database_with_real_data pool: 5 username: root password: socket: /tmp/mysql.sock |
The above steps will also make sure that the performance tests use the database database_with_real_data. Also, the database will not get wiped on every run. You’ll also have to ensure the test database – database_with_real_data – is up-to-date with the schema changes.
Now you can just run your performance tests using the usual rake tasks:
1 2 |
$ rake test:benchmark $ rake test:profile |







I think it’s worth creating a whole new environment for this, because you want to turn on the caching features that the production environment uses for performance testing.
One thing to be careful about is to make sure your performance tests aren’t sending emails or hitting live APIs and what not, which is a danger if you’re using real data with a modified production environment.
Rails initially had a separate performance environment. But most of the people realize it’s closer to the test environment than anything else.
Personally, I like turn on caching in the test environment as I do like to test some basic caching stuff.
> require_dependency ‘application’
Shouldn’t it be ‘application_controller’ in an Rails-2.3 application?
Yup, in Rails 2.3+ it should be require ‘application_controller’
Excellent! Thanks for posting this. After almost an entire day of effort I had all of this EXCEPT “ActiveRecord::Base.establish_connection”, the essential piece! I would have NEVER figured that out!
That’s the problem with Rails in general… it “just works” right up until it doesn’t. Then you are screwed because the presumption is that you don’t have to be able to know how everything works inside, so the tools and docs to help understand it all are terrible. (For example, the RailsGuide about performance testing states flatly, “Performance tests are run in the development environment.” Is this wrong, or should it say, ”...except that it uses the test database” ?? Aaaaargh!)
Again thanks for explaining this. Contribs like this make Rails a viable app dev solution for me.