Powered By BlogNow - Get Your Free Blog

I have moved!

My new blog is at lindsaar.net

All the old content will stay here though. But check out the new blog

¿ 26/9/2007 - How to optimize a rails site... the easy way.

OK.

I have a dog of an app.  34 second page loads not uncommon...

Yes... you hear right.  Half a MINUTE to load the page.

And no, it is not complex, no, it is not even super heavy on the database side.  It is just a long neglected, unoptimized, admin app that I use for a variety of things that was always at the bottom of my list of things to do.

So i thought I would take a few moments and speed it up.

Result:

Before SIMPLE optimization - 34 seconds

After SIMPLE optimization - 1.8 seconds.

Not a bad improvement, considering I didn't change a line of code in my app.

The trick?

Easy, make your javascript smaller, reduce the number of scripts and compress your content. Ok... this is how in detail.

First, go get Firefox or BonEcho and install the YSlow plugin from http://developer.yahoo.com/yslow/  This will give you a option at the bottom of your browser and can tell you how long it takes to download your page, go check it out and use it.  This gives you pretty much all the hints you need to speed up your page.

Then, for me, the biggest gain is reducing the number of script files you call.  This is becuase a web browser will only download 2 items from any one server at a time.  So, say you have 5-6 javascript files in the top of your application.rhtml file in /views/layouts/, This is not hard to do, especially if you include the default javascript files with the rails helper.

Well... if you have 6, this means that the browser downloads two, then the next two, then the next two.  While it is doing this, nothing on your page renders to the end user and all you get is a blank browser window.

The handling?

Well, combine it all into one file.

You can do this simply, from the command line, do the following:

# cat prototype.js >> alltogether.js
# cat next.js >> alltogether.js
# cat effects.js >> alltogether.js
# cat dragdrop.js >> alltogether.js
# cat another.js >> alltogether.js


Then go into your application.rhtml file and take out all your javascript script tags and replace them with:

script src="/javascripts/alltogether.js?1" type="text/javascript"

The ?1 on the end is the version number.

Now try loading your page, you will see an instant increase in speed.

Next, we want to strip out of that combined file all the comments and extra white space.  A nice easy rubyfied program for this is at:

http://www.crockford.com/javascript/jsmin.rb


Download this into a file called jsmin.rb.

Then, run the following:

# ruby jsmin.rb < alltogether.js > combined.js


Now, move this combined.js filed into your public/javascripts directory and replace out the script line to read:

script src="/javascripts/combined.js?1" type = "text/javascript"


Now, another reload, and it should be faster again.

Then, I went into my apache config file and added in
ExpiresDefault "access plus 1 month"

You put this inside the < Directory "/path/to/rails/public/" > < / Directory > tags

You can make this more than a month, but for me, a month is fine.

Anyway, with that in place, my load time droped from 34 seconds to 1.8 seconds.

Not too shabby.

blogLater.

Mikel
Post A Comment! :: Send to a Friend!

About Me

AKA Raasdnil, this site is about web coding, hosting and all other matters that relate to this... especially Ruby on Rails!

Links