This example is a simple performance test case for profiling a GET request to the application's homepage.
h4. Generating performance tests
h4. Generating Performance Tests
Rails provides a generator called +performance_test+ for creating new performance tests:
...
...
@@ -95,7 +95,7 @@ class Post < ActiveRecord::Base
end
</ruby>
h5. Controller example
h5. Controller Example
Because performance tests are a special kind of integration test, you can use the +get+ and +post+ methods in them.
...
...
@@ -123,7 +123,7 @@ end
You can find more details about the +get+ and +post+ methods in the "Testing Rails Applications":testing.html guide.
h5. Model example
h5. Model Example
Even though the performance tests are integration tests and hence closer to the request/response cycle by nature, you can still performance test pure model code.
...
...
@@ -173,13 +173,13 @@ h4. Metrics
Benchmarking and profiling run performance tests in various modes described below.
h5. Wall time
h5. Wall Time
Wall time measures the real world time elapsed during the test run. It is affected by any other processes concurrently running on the system.
Mode: Benchmarking
h5. Process time
h5. Process Time
Process time measures the time taken by the process. It is unaffected by any other processes running concurrently on the same system. Hence, process time is likely to be constant for any given performance test, irrespective of the machine load.
...
...
@@ -197,19 +197,19 @@ Objects measures the number of objects allocated for the performance test case.
Performance tests generate different outputs inside +tmp/performance+ directory depending on their mode and metric.
...
...
@@ -217,7 +217,7 @@ h5. Benchmarking
In benchmarking mode, performance tests generate two types of outputs:
h6. Command line
h6. Command Line
This is the primary form of output in benchmarking mode. Example:
...
...
@@ -230,7 +230,7 @@ BrowsingTest#test_homepage (31 ms warmup)
gc_time: 19 ms
</shell>
h6. CSV files
h6. CSV Files
Performance test results are also appended to +.csv+ files inside +tmp/performance+. For example, running the default +BrowsingTest#test_homepage+ will generate following five files:
...
...
@@ -262,7 +262,7 @@ h5. Profiling
In profiling mode, you can choose from four types of output.
h6. Command line
h6. Command Line
This is a very basic form of output in profiling mode:
...
...
@@ -285,13 +285,13 @@ h6. Tree
Tree output is profiling information in calltree format for use by "kcachegrind":http://kcachegrind.sourceforge.net/html/Home.html and similar tools.
h4. Tuning test runs
h4. Tuning Test Runs
By default, each performance test is run +4 times+ in benchmarking mode and +1 time+ in profiling. However, test runs can easily be configured.
WARNING: Performance test configurability is not yet enabled in Rails. But it will be soon.
h4. Performance test environment
h4. Performance Test Environment
Performance tests are run in the +development+ environment. But running performance tests will set the following configuration parameters:
As +ActionController::Base.perform_caching+ is set to +true+, performance tests will behave much as they do in the +production+ environment.
h4. Installing GC-patched Ruby
h4. Installing GC-Patched Ruby
To get the best from Rails performance tests, you need to build a special Ruby binary with some super powers - "GC patch":http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch for measuring GC Runs/Time and memory/object allocation.
...
...
@@ -313,7 +313,7 @@ h5. Installation
Compile Ruby and apply this "GC Patch":http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch.
The following will install ruby in your home directory's +/rubygc+ directory. Make sure to replace +<homedir>+ with a full patch to your actual home directory.
...
...
@@ -337,7 +337,7 @@ The following will install ruby in your home directory's +/rubygc+ directory. Ma
[lifo@null ruby-version]$ make && make install
</shell>
h5. Prepare aliases
h5. Prepare Aliases
For convenience, add the following lines in your +~/.profile+:
...
...
@@ -349,7 +349,7 @@ alias gcirb='~/rubygc/bin/irb'
alias gcrails='~/rubygc/bin/rails'
</shell>
h5. Install Rubygems and dependency gems
h5. Install Rubygems and Dependency Gems
Download "Rubygems":http://rubyforge.org/projects/rubygems and install it from source. Rubygem's README file should have necessary installation instructions.
...
...
@@ -496,21 +496,21 @@ Michael Koziarski has an "interesting blog post":http://www.therailsway.com/2009