Running Rails performance tests on real data
Published over 2 years ago
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:
# 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'
require 'test_helper' require 'performance_test_help'
with
require 'performance_test_helper'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:
$ rake test:benchmark $ rake test:profile