Topic: First page viewed is slow

We have an RoR app, and it runs fine.  It is a production application.  We have dual quad-core Pentium servers with 16GB of ram.  One of the slowest pages is the first one you see.  All it is is a login page, but it takes 5-6 seconds to load.

Can anyone explain this?

Does Rails normally load a lot of stuff when a user first hits the server?

What should I look at first?

Thanks,

Nick

Re: First page viewed is slow

You should check your production log file and see if it is indeed Rails that is rendering it slowly.

If it is, how often is your application being accessed? It could be booting up the whole application each time if nobody has used it in a hour or so.

What type of server software are you running? E.g. Thin, Mongrel, WEBrick, ModRails, FCGI etc.

ThinkRefresh - Free Rails Techniques Screencasts

Re: First page viewed is slow

I had a similar thing when I was on shared-hosting.  If no-one accessed the site for an hour it'd take 10-20 seconds to 'boot-up' when you first loaded it.

Re: First page viewed is slow

Are you using Passenger?

Re: First page viewed is slow

I am using Passenger and the initial app page load is fairly slow 4-6 seconds.  Once it loads initially the app runs fine.  This is on a very lightly loaded server and the app is only used by one or two users at a time. (But the initial page load delay is still annoying)

I have set the life time to 4 hours or so so once it starts it stays running and loads very quickly.

I know with fastcgi I used before you could start cgi servers when Apache started so initial load was never a problem.  Is there a way to do something similar with Passenger (mod_rails)?

Any other solutions?

Richard

Re: First page viewed is slow

I had a similar problem with Passenger on Dreamhost. Eventually I just cached the first page and that seemed to solve the problem

Re: First page viewed is slow

This is because passenger has to start up a rails process. You can change this behavior by changing a variable in your apache configuration, only trouble is I'm not sure which one!

Re: First page viewed is slow

PassengerPoolIdleTime seems to be the parameter.  Check out:
http://www.modrails.com/documentation/U … _passenger

I'm going to try this on my install right now as I experience the same thing (slow initial load).

Re: First page viewed is slow

avitus wrote:

I had a similar problem with Passenger on Dreamhost. Eventually I just cached the first page and that seemed to solve the problem

Same problem on Dreamhost.

Did you manually cache it? By that I mean make it a static page or use Rails caching?  I don't know Rails very well, but would think that something still has to start up and caching wouldn't help.

If you used Rails caching, where did you set it.

I might look at PassengerPoolIdleTime but since my site may goes days without being accessed I'm afraid Dreamhost would shut something down anyway.

TIA.

Last edited by MtnBiker (2009-03-15 18:10:48)

Re: First page viewed is slow

Does anyone have any experience improving the speed of the rails process startup time (just loading script/console)?  I see significant disparities between the startup times of some of our applications, and would love to find out exactly what is slowing the process down.

Re: First page viewed is slow

If you have lots of plugins and dependencies it will take longer. I think if you use rails 2.3 that it will load faster because it only loads what it needs when it needs it.

Re: First page viewed is slow

MtnBiker wrote:
avitus wrote:

I had a similar problem with Passenger on Dreamhost. Eventually I just cached the first page and that seemed to solve the problem

Same problem on Dreamhost.

Did you manually cache it? By that I mean make it a static page or use Rails caching?  I don't know Rails very well, but would think that something still has to start up and caching wouldn't help.

If you used Rails caching, where did you set it.

I might look at PassengerPoolIdleTime but since my site may goes days without being accessed I'm afraid Dreamhost would shut something down anyway.

TIA.

You could set up a cron job to access a (rails hosted page) every 5 minutes.  Then your server would always be fresh.

Re: First page viewed is slow

I'm having this same problem - excruciatingly slow loading times on the first page. I'm using passenger on DreamHost. Seems like there's three solutions:

1. Adjust the PassengerPoolIdleTime so that the application stays running. (I don't have access to this setting since I'm not on a private server.)

2. Cache the public pages so the application essentially doesn't even need to run at all. (I can't do this cause I have a lot of dynamic content (stock tickers, upcoming events, etc.) on those pages.)

3. Set up a cron job to visit the page at regular interval in order to keep the server fresh (like rogerdpack suggests).

I think a cron job would work for me, but I'm very new to Unix and having some trouble getting it set up / knowing how to check if it's working properly.

I understand that the cron job will need to be set up on the server eventually, but right now I'm just trying to make it work on my computer. Basically, I'd like to have my computer visit the site every five minutes. 

Here's what I've done so far:

1. accessed the crontab file: pico crontab -e
2. added this line: 0,5,10,15,20,25,30,35,40,45,50,55 * * * * open http://domain_name.com/
3. saved and exited

Am I missing something? Is there a simple way to tell if this is working?

Cory.

Re: First page viewed is slow

I figured this out. Here's how to set up a cronjob on a shared DreamHost server.

1. connect to the server via SSH
2. type: crontab -e (this will allow you to edit your cron jobs using the default editor (vim))
3. type: i (this will toggle you into "insert" mode (you'll see "- insert -" appear on the bottom of the screen) so that you can insert text into the file)
4. paste this line of code into the crontab file:
     0,5,10,15,20,25,30,35,40,45,50,55 * * * * wget http://your-domain.com/
5. hit the escape key (this will take you out of "insert" mode and return you to "command" mode)
6. type: :wq (that means write and quit)
7. click enter and you're done.

This is super easy and it works great. It's much better than going through the tedium and limitations of page caching. (Also, I emailed DH to check it this is violation of their policy on running background processes. They said it's cool.)

Cory.

Re: First page viewed is slow

You can clean up that cron job by using:

*/5 * * * * wget site.com

This will make it run every 5 minutes.

Re: First page viewed is slow

I did the wget method for a couple days and failed to remember that wget actually retrieves the page, so my home directory was filled with a couple thousand index.html pages.  I changed my cron to "curl -s http://www.domain.com > /dev/null" to "ping" the domain if you will, although you could "> /dev/null" with wget as well and avoid the buildup of pages.

Re: First page viewed is slow

I wanted to attempt to revive this thread instead of posting a new topic.  I am running a rails 2.3.8 app (Apache+Passenger) on a gig VPS with one or two users.  I have noticed that after the app is unaccessed for awhile, the time to get to the login page is 35+ seconds.  I viewed the production.log while this was happening and discovered that the login page was rendered in sub-second time, but the page was not received by the client until much later.  Where is the bottleneck? In Apache, Passenger, the VPS? Any ideas/advice?

Thanks.

Re: First page viewed is slow

I believe this is Passenger at work.  Take a look at the options for passenger configuration and I think you will find that if there is no action for a configurable time the active 'thread' will be deleted and when a new request comes in the whole works will have to be reloaded.

Re: First page viewed is slow

Thanks Norm for you reply.  A clarification though - would Passenger be suspect even though the production log says the login page was rendered in sub-second time.  I have watched the log while this is taking place and the login page is said to be rendered, but I don't get an html page on the client until 35+ seconds later (I used curl to request the page to eliminate any browser issues).

Re: First page viewed is slow

I do not know.  I know it takes a while for the app to crank up under passenger but that depends on system speed etc.  Can you run the same basic environment using passenger on your development system locally to eliminate external influences?  I have an old (real old) system on my local net that I use just to test the setup under passenger etc. as close as it can be to my deployed system so I can isolate issues like you mention.