Running Rails performance tests on real data
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 |
Images:


Recent comments
1 week 1 day ago
1 week 2 days ago
3 weeks 1 day ago
5 weeks 3 days ago
20 weeks 1 day ago
23 weeks 1 day ago
23 weeks 6 days ago
23 weeks 6 days ago
24 weeks 1 day ago
26 weeks 3 days ago