From a5981517e6676818c3031834627e4a0763ce4fba Mon Sep 17 00:00:00 2001
From: Pratik Naik
-Generate performance/benchmarking tests
+Generate performance and benchmarking tests
-Use GC patched Ruby binary to measure memory usage and object allocation
+Use a GC-patched Ruby binary to measure memory usage and object allocation
-Understand the information provided by Rails inside the log files
+Understand the benchmarking information provided by Rails inside the log files
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 to the end users and cutting cost of unnecessary hardwares is important for any web application.
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.
Rails performance tests are 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 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.
In a freshly generated Rails application, test/performance/browsing_test.rb contains an example of a performance test:
The above example is a simple performance test case for profiling a GET request to the application’s homepage.
This example is a simple performance test case for profiling a GET request to the application’s homepage.
Rails provides a generator called performance_test for creating new performance tests:
script/generate performance_test homepage
This generates homepage_test.rb inside test/performance directory:
This generates homepage_test.rb in the test/performance directory:
You can find more details about get and post methods in API documentation of integration testing.
You can find more details about the get and post methods in the Testing Rails Applications guide.
Even though the performance tests are integration tests and hence closer to request/response cycle by nature, it doesn’t prevent us from performance testing pure model code.
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.
Performance test for Post model:
Performance tests can be run in two modes : Benchmarking and Profiling.
Benchmarking helps find out how fast is a performance test. Each test case is run 4 times in benchmarking mode.
Benchmarking helps find out how fast each performance test runs. Each test case is run 4 times in benchmarking mode.
To run performance tests in benchmarking mode:
$ rake test:benchmark
Profiling helps you introspect into a performance test and provide an in-depth picture of the slow and memory hungry parts. Each Test case is run 1 time in profiling mode.
Profiling helps you see the details of a performance test and provide an in-depth picture of the slow and memory hungry parts. Each test case is run 1 time in profiling mode.
To run performance tests in profiling mode:
Benchmarking and profiling run performance tests in various modes described below.
Measures the real world time elapsed during the test run. It is affected by any other processes concurrently running on the system.
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
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.
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.
Mode : Profiling
Measures the amount of memory used for the performance test case.
Mode : Benchmarking, Profiling [Requires GC Patched Ruby]
Memory measures the amount of memory used for the performance test case.
Mode : Benchmarking, Profiling [Requires GC-Patched Ruby]
Measures the number of objects allocated for the performance test case.
Mode : Benchmarking, Profiling [Requires GC Patched Ruby]
Objects measures the number of objects allocated for the performance test case.
Mode : Benchmarking, Profiling [Requires GC-Patched Ruby]
Measures the number of times GC was invoked for the performance test case.
Mode : Benchmarking [Requires GC Patched Ruby]
GC Runs measures the number of times GC was invoked for the performance test case.
Mode : Benchmarking [Requires GC-Patched Ruby]
Measures the amount of time spent in GC for the performance test case.
Mode : Benchmarking [Requires GC Patched Ruby]
GC Time measures the amount of time spent in GC for the performance test case.
Mode : Benchmarking [Requires GC-Patched Ruby]
Performance tests generate different outputs inside tmp/performance directory based on the mode it is run in and the metric.
Performance tests generate different outputs inside tmp/performance directory depending on their mode and metric.
In benchmarking mode, performance tests generate two types of outputs :
As the results are appended to these files each time the performance tests are run in benchmarking mode, it enables you to collect data over a period of time which can be very helpful with various performance analysis.
As the results are appended to these files each time the performance tests are run in benchmarking mode, you can collect data over a period of time. This can be very helpful in analyzing the effects of code changes.
Sample output of BrowsingTest#test_homepage_wall_time.csv:
In profiling mode, you can choose from four types of output.
This is a very basic form of output in profiling mode:
To get the best from Rails performance tests, you need to build a special Ruby binary with some super powers - GC patch for measuring GC Runs/Time and memory/object allocation.
The process is fairly straight forward. If you’ve never compiled a Ruby binary before, follow the following steps to build a ruby binary inside your home directory:
The process is fairly straight forward. If you’ve never compiled a Ruby binary before, follow these steps to build a ruby binary inside your home directory:
Compile Ruby and apply this GC Patch:
Writing performance test cases could be an overkill when you are looking for one time tests. Rails ships with two command line tools for allowing such quick and dirty performance testing:
Writing performance test cases could be an overkill when you are looking for one time tests. Rails ships with two command line tools that enable quick and dirty performance testing:
benchmarker is a wrapper around Ruby’s Benchmark module.
Usage:
$ script/performance/benchmarker 10 'Item.all' 'CouchItem.all'
If [times] argument is skipped, supplied methods are run just once:
If the [times] argument is omitted, supplied methods are run just once:
$ script/performance/profiler 'Item.all'
This will profile Item.all with RubyProf::WALL_TIME measure mode. By default, flat output is printed to the shell.
This will profile Item.all in RubyProf::WALL_TIME measure mode. By default, it prints flat output to the shell.
This benchmarks the code enclosed in Project.benchmark("Creating project") do..end block and prints the result to the log file:
This benchmarks the code enclosed in the Project.benchmark("Creating project") do..end block and prints the result to the log file:
Creating project (185.3ms)
Please refer to API docs for optional options to benchmark()
Please refer to the API docs for additional options to benchmark()
Similarly, you could use this helper method inside controllers
Completed in 5ms (View: 2, DB: 0) | 200 OK [http://0.0.0.0/items]
This data is fairly straight forward to understand. Rails uses millisecond(ms) as the metric to measures 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.
This data is fairly straightforward to understand. Rails uses millisecond(ms) as the metric to measures 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.
Michael Koziarski has an interesting blog post explaining the importance of using milliseconds as the metric.