Home arrow Ruby-on-Rails arrow Page 2 - The Action Profiler and Performance Optimization
RUBY-ON-RAILS

The Action Profiler and Performance Optimization


In this third article on performance optimization for Ruby on Rails applications, you'll learn how to run the Action Profiler tool and how to start using caching to improve your application's performance. This article is excerpted from chapter 13 of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
December 08, 2010
TABLE OF CONTENTS:
  1. · The Action Profiler and Performance Optimization
  2. · Caching

print this article
SEARCH DEVARTICLES

The Action Profiler and Performance Optimization - Caching
(Page 2 of 2 )

 

Now that we have measured the performance of our application and tracked slow actions, itís time to do something about them. One of the most common ways to speed up a website is to use some kind of caching. If an often-viewed page needs a large amount of database queries or otherwise expensive calculations to produce its output, storing the output in a cache can make the site a lot more responsive.

Out of the box, Rails sports three caching levels: page, action, and fragment caching. You can also use thecached_modellibrary for caching ActiveRecord objects, when you need to go beyond the usual caching.

Page Caching

Page caching is the fastest of the caching schemes in Rails. With page caching, the cached page is stored as a static HTML file in the document root of the web server and served directly from there on subsequent requests. This means that Rails can be bypassed altogether, and the web server can serve the page in the same way that it serves other static files. This obviously means a huge impact on the performance. Whereas a single Rails process can serve a few dozen non-cached pages per second, a real web server serving static files can easily reach speeds of up to 1000 requests per second.

Should all our actions then be using page caching? Well, no. Page caching stores the output of an action in a file and serves that same file all the time, to all the people. This has many drawbacks. The page is not really dynamic anymore. You canít use authentication, since Rails is bypassed. You also canít have any personalization on the page. Therefore, page caching is not really recommended for anything but the most static pages served by a Rails application.

Page caching is also kind of against the shared nothing architecture, since the cached pages are stored on the file system. You could use a networked file system that all the application servers would use as the document root, but you would still be vulnerable to different application servers trying to write the same file at the same time.

However, we do have one page in the Emporium application that hardly ever changes: the About page. Letís implement page caching for it as an exercise.

It turns out it is extremely easy to use page caching. The only thing we need to do is add a single row to the controller in question, which in this case isapp/controllers/about_controller.rb:

class AboutController < ApplicationController
 
caches_page :index

  def index
    @page_title = 'About Emporium'
  end
end

The next time you load the About page, it is stored inapp/public/about.htmland served from there ever after.

We can use thebenchcommand (part of the Rails Analyzer tool set) to look at the performance of the page without and with caching (repeat the runs several times until the results stabilize). First try it without caching:

$ bench -u http://localhost/about -r 100 -c 10

--------------------------------------------100.........90.........80.........70.........60.........50
.........40.........30.........20.........10.........
Total time: 48.2463064193726
Average time: 0.482463064193726
--------------------------------------------

And then runbenchwith caching (remember to restart the application to make the code changes live on a production server):

$ bench -u http://localhost:3000/about -r 100 -c 10

--------------------------------------------100.........90.........80.........70.........60.........50
.........40.........30.........20.........10.........
Total time: 5.81317806243896
Average time: 0.0581317806243896
--------------------------------------------


Note  By default caching is turned on only in the production environment. If you want to test it in the development environment, change theconfig.action_controller.perform_cachingparameter totrueinconfig/environments/development.rb.


The difference is noticeable, although not nearly as big as it is in reality, since weíre running thebenchcommand on the same machine and it is consuming part of the processing power itself.

But what if you want to change the page and expire the cache some time? Thatís easy, too. Just callexpire_page :action => "index"in the action where you change the page.

Please check back for the next part of this series.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials