Topic: Application speed and code optimisation

I have recently completed my first Rails project, a simple blogging application, having deployed the application I've noticed that its pretty slow even when its loading to the point that I'm being told to add a preloader. I've implemented fragment caching for the various partials I have using the timed_fragment_cache plugin but the app still seems pretty slow on the internet.Take for example my index page code:

def index
     @post_pages, @posts= paginate(:posts,:per_page => @blog.postsperpage, :conditions=>"poststatus=1", :order_by => 'created_at DESC')
       
end

And in the view I have the following code:
<% cache 'index', 15.minutes.from_now do %>

<% if @flash[:notice] -%>
<div id="notice"><%= @flash[:notice] %></div>
<% end -%>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<% for post in @posts %>
  <tr>
    <td>
    <div class="entrytitle">
    <h2><%= link_to "#{post.title}",{:action => "post",:id => post.permalink}, :class=>"entrytitle" %></h2>
    </div>
    </td>
  </tr>
  <tr>
    <td valign="top">
   <div style="float: left; margin-right: 0.5em;">
    <script>
digg_url = 'http://<%=request.subdomains%>.<%=request.domain%>/peupe/post/<%=post.permalink%>';
</script>
<script src="http://digg.com/api/diggthis.js"></script>
</div>
    <%=post.post %></td>
  </tr>

  <tr>
    <td>Posted By
   
<%=post_author_link(post)%>   
    on <%=post.created_at.strftime('%B %d, %Y')%>
    <%= link_to "#{post.comments.size} Comments", {:action => "post",:id => post.permalink} %></td>
  </tr>
<tr>
    <td>
Like this post? <a href='http://del.icio.us/post?url=http://<%=request.subdomains%>.<%=request.domain%>/peupe/post/<%=post.permalink %>'><img src="/images/delicious.gif" border="0" alt="Bookmark it in delicious"> </a>
     <a href='http://technorati.com/cosmos/search.html?url=http://<%=request.subdomains%>.<%=request.domain%>/peupe/post/<%=post.permalink %>'>
     <img src="/images/technorati-16x15.png" border="0" alt="technorati"></a>
     <%=display_tags(post)%>
<br/>
<% if @blog.emailpost==1%>
<%=image_tag "icons/share.gif", :alt=>"Email this to a friend"%>
<%-else%>
<%end%>
    </td>
  </tr>
   <tr>
    <td>&nbsp;</td>
  </tr>
  <% end %>
</table>

<%-end%>   


<% unless @post_pages.item_count <= @blog.postsperpage %>

  <p id="pagination">Older posts: <%= pagination_links @post_pages, :params => { :action => @controller.action_name } %></p>

<%end%>


I've tried to get asa much information on optimisation but most people give advice based upon large applications on dedicated servers, I'm on a shared host, which I find difficult to implement. So i'm looking for advice from experienced rails users on the forum to help me tweak  part of my code. My web host already temporarily shut me down for using more then 20% CPU and said that may be my code is inefficient.

Any ideas on this would go a long way.

Re: Application speed and code optimisation

Take a look at the production log, this will tell you how much time it's taking to render the page and how much time it's spending in the database. This will give you a rough idea of what is slowing it down.

Also, make sure you aren't using normal CGI. This is incredibly slow because it reloads the entire Rails framework for each request. I think you can set this in the public/.htaccess file (if you're using Apache).

Railscasts - Free Ruby on Rails Screencasts

Re: Application speed and code optimisation

ryanb wrote:

Take a look at the production log, this will tell you how much time it's taking to render the page and how much time it's spending in the database. This will give you a rough idea of what is slowing it down.

Also, make sure you aren't using normal CGI. This is incredibly slow because it reloads the entire Rails framework for each request. I think you can set this in the public/.htaccess file (if you're using Apache).

My css and images don't load after I change the .htacess to use fastcgi. Here is my .htacess file before modification:

# General Apache options
AddHandler fastcgi-script .fcgi
AddHandler cgi-script .cgi
Options +FollowSymLinks +ExecCGI

# If you don't want Rails to look in certain directories,
# use the following rewrite rules so that Apache won't rewrite certain requests
#
# Example:
#   RewriteCond %{REQUEST_URI} ^/notrails.*
#   RewriteRule .* - [L]

# Redirect all requests not available on the filesystem to Rails
# By default the cgi dispatcher is used which is very slow
#
# For better performance replace the dispatcher with the fastcgi one
#
# Example:
#   RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
RewriteEngine On

# If your Rails application is accessed via an Alias directive,
# then you MUST also set the RewriteBase in this htaccess file.
#
# Example:
#   Alias /myrailsapp /path/to/myrailsapp/public
#   RewriteBase /myrailsapp

RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]

# In case Rails experiences terminal errors
# Instead of displaying this message you can supply a file here which will be rendered instead
#
# Example:
#   ErrorDocument 500 /500.html

ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"


Any idea why?

Re: Application speed and code optimisation

What exactly are you modifying to use fastcgi?

Railscasts - Free Ruby on Rails Screencasts

Re: Application speed and code optimisation

ryanb wrote:

What exactly are you modifying to use fastcgi?

I uncommented the following line in the .htaccess file:
#RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

I'm new to this so any help would go a long way.

Re: Application speed and code optimisation

You should leave that line commented, it's just an example. You need to change line 32 which is currently this:

RewriteRule ^(.*)$ dispatch.cgi [QSA,L]

to this

RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

Railscasts - Free Ruby on Rails Screencasts

Re: Application speed and code optimisation

ryanb wrote:

You should leave that line commented, it's just an example. You need to change line 32 which is currently this:

RewriteRule ^(.*)$ dispatch.cgi [QSA,L]

to this

RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

Okay that worked out well.
I have now downloaded the production log and looked at how long its looking to render the index page. Below is part of the log, may be you can help me understand it:

Processing PeupeController#index (for 62.8.87.164 at 2007-06-14 10:24:05) [GET]
  Session ID: d14e8ba7c71abbf5a9f4ac926c0a71a2
  Parameters: {"action"=>"index", "controller"=>"peupe"}
Rendering  within layouts/peupe
Rendering peupe/index
Completed in 0.02960 (33 reqs/sec) | Rendering: 0.02224 (75%) | DB: 0.00468 (15%) | 200 OK

Re: Application speed and code optimisation

The important part is the last line where it tells you how long it took. It looks like you are getting good performance here so I don't think the code itself is the problem. It was the cgi vs. fcgi causing the slow down.

Railscasts - Free Ruby on Rails Screencasts

Re: Application speed and code optimisation

what do you think about this speed?

Processing ProductsController#show (for 75.181.116.41 at 2007-09-19 11:41:53) [GET]
  Session ID: cd0d94a64a359853f5d1c7236132391a
  Parameters: {"action"=>"show", "id"=>"jackets", "controller"=>"products"}
Rendering  within layouts/application
Rendering products/show
Completed in 1.34759 (0 reqs/sec) | Rendering: 0.76327 (56%) | DB: 0.58238 (43%) | 200 OK [http://dev.broken-records.com/store/jackets]


Processing MainController#index (for 75.181.116.41 at 2007-09-19 11:43:36) [GET]
  Session ID: cd0d94a64a359853f5d1c7236132391a
  Parameters: {"action"=>"index", "controller"=>"main"}
Rendering main/index
Completed in 0.40314 (2 reqs/sec) | Rendering: 0.40301 (99%) | DB: 0.00000 (0%) | 200 OK [http://dev.broken-records.com/]


Processing ProductsController#show (for 75.181.116.41 at 2007-09-19 11:45:47) [GET]
  Session ID: cd0d94a64a359853f5d1c7236132391a
  Parameters: {"action"=>"show", "id"=>"all", "controller"=>"products"}
Rendering  within layouts/application
Rendering products/show
Completed in 1.74166 (0 reqs/sec) | Rendering: 1.07430 (61%) | DB: 0.66444 (38%) | 200 OK [http://dev.broken-records.com/store]

Re: Application speed and code optimisation

That's slow, you'll need to find the bottlenecks and optimize them. This is usually solved by:

- reducing the number of database queries
- adding indexes to your tables
- adding caching

If you want more detailed suggestions for your app then I recommend starting a new thread.

Railscasts - Free Ruby on Rails Screencasts