aboutsummaryrefslogtreecommitdiffstats
path: root/railties/doc/guides/source
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-01-10 02:56:14 +0000
committerPratik Naik <pratiknaik@gmail.com>2009-01-10 02:56:14 +0000
commit190eb9b07f3f48d07137b25c11dca99b40fd72b1 (patch)
treec8488c2ed47ad7cc04cbf3772b820e26a84ce71d /railties/doc/guides/source
parent8342174b2800937024a06273d1891730e6cf02cc (diff)
downloadrails-190eb9b07f3f48d07137b25c11dca99b40fd72b1.tar.gz
rails-190eb9b07f3f48d07137b25c11dca99b40fd72b1.tar.bz2
rails-190eb9b07f3f48d07137b25c11dca99b40fd72b1.zip
Some examples for perf testing
Diffstat (limited to 'railties/doc/guides/source')
-rw-r--r--railties/doc/guides/source/performance_testing.txt99
1 files changed, 97 insertions, 2 deletions
diff --git a/railties/doc/guides/source/performance_testing.txt b/railties/doc/guides/source/performance_testing.txt
index f42b3686b7..e58927b758 100644
--- a/railties/doc/guides/source/performance_testing.txt
+++ b/railties/doc/guides/source/performance_testing.txt
@@ -1,4 +1,4 @@
-Performance testing Rails Applications
+Performance Testing Rails Applications
======================================
This guide covers the various ways of performance testing a Ruby on Rails application. By referring to this guide, you will be able to:
@@ -58,6 +58,98 @@ end
Which you can modify to suit your needs.
+=== Examples ===
+
+Let's assume your application have the following controller and model:
+
+[source, ruby]
+----------------------------------------------------------------------------
+# routes.rb
+map.root :controller => 'home'
+map.resources :posts
+
+# home_controller.rb
+class HomeController < ApplicationController
+ def dashboard
+ @users = User.last_ten(:include => :avatars)
+ @posts = Post.all_today
+ end
+end
+
+# posts_controller.rb
+class PostsController < ApplicationController
+ def create
+ @post = Post.create(params[:post])
+ redirect_to(@post)
+ end
+end
+
+# post.rb
+class Post < ActiveRecord::Base
+ before_save :recalculate_costly_stats
+
+ def slow_method
+ # I fire gallzilion queries sleeping all around
+ end
+
+ private
+
+ def recalculate_costly_stats
+ # CPU heavy calculations
+ end
+end
+----------------------------------------------------------------------------
+
+==== Controller Example ====
+
+Performance tests are special kind of integration tests. This allows you to use +get+ and +post+ methods inside the performance tests.
+
+Performance tests for the controller code above can be something like:
+
+[source, ruby]
+----------------------------------------------------------------------------
+require 'test_helper'
+require 'performance_test_help'
+
+class PostPerformanceTest < ActionController::PerformanceTest
+ def setup
+ # Application requires logged in user
+ login_as(:lifo)
+ end
+
+ def test_homepage
+ get '/dashboard'
+ end
+
+ def test_creating_new_post
+ post '/posts', :post => { :body => 'lifo is fooling you' }
+ end
+end
+----------------------------------------------------------------------------
+
+You can find more details about +get+ and +post+ methods in API documentation of integration testing.
+
+==== Model Example ====
+
+Even though performance tests are integration tests and hence closer to request/response cycle by nature, it doesn't prevent us from testing pure model code inside. Performance test for +Post+ model above can be somewhat like:
+
+[source, ruby]
+----------------------------------------------------------------------------
+require 'test_helper'
+require 'performance_test_help'
+
+class PostModelTest < ActionController::PerformanceTest
+ def test_creation
+ Post.create :body => 'still fooling you', :cost => '100'
+ end
+
+ def test_slow_method
+ # Using posts(:awesome) fixture
+ posts(:awesome).slow_method
+ end
+end
+----------------------------------------------------------------------------
+
=== Modes ===
Performance test cases can be run in two modes : Benchmarking and Profling.
@@ -344,7 +436,10 @@ Michael Koziarski has an http://www.therailsway.com/2009/1/6/requests-per-second
* http://rails-analyzer.rubyforge.org/[Rails Analyzer]
* http://www.flyingmachinestudios.com/projects/[Palmist]
-== Commercial products dedicated to Rails Perfomance ==
+== Commercial products ==
+
+Rails has been lucky to have three startups dedicated to Rails specific performance tools:
+
* http://www.newrelic.com[New Relic]
* http://www.fiveruns.com[Fiveruns]
* http://scoutapp.com[Scout]