Ajax call really slow...

Hey Guys,

I'm fairly new to Rails, so my guess is that I'm just doing this
wrong. I have a site that I'm building that uses ajax to update a
list of items depending on what tab a user clicks. Unfortunately, it
isn't live yet so I can't send a link directly to the site, but I have
it all working using straight HTML/JS/XML. Where the problems start
is when I try to use Rails to generate the update.

At first I tried to use RJS to push a partial template that loops
through each item to generate the html block that I need to push to
the list. I figured this would be easy and straight forward, but even
though I only have 2 items in the DB currently (will eventually be
hundreds) the list was taking 2+ seconds to update even locally. I
even tried changing my technique to use :update on a link_to_remote
instead of the RJS template with no increase performance.

Next I tried leaving the javascript that I was using before in place,
but used a partial template to build the xml file that was requested.
This seemed to help the performance, but I still wasn't able to
decrease the load time to an acceptable amount.

Lastly, I'm trying to use an rxml template to build the xml instead of
looping within a partial template:

xml.items do
  for p in @products
    xml.item(:tags => p.tags, :id => p.id) do
      xml.subtitle(truncate(p.description, 100))
      xml.summary(truncate(p.description, 100))
      xml.postedby("Lance 43", :id=>"12343")
      xml.images do
        xml.image(:id=>1) do
      xml.links do
        xml.link("link 1", :id=>"1", :href=>"http://www.link1.com")
        xml.link("link 2", :id=>"2", :href=>"http://www.link2.com")
      xml.comments do
        xml.comment(:id=>"1") do
          xml.postedby("Ross C.", :id=>"123")
          xml.content("This item works well elit, sed diam nonummy
nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Ut wisi enim ad minim veniam, quis")
        xml.comment(:id=>"2") do
          xml.postedby("Peter J.", :id=>"356")
          xml.content("Made some fixes to the wording and did some
other cleaning up. Man people sure spell bad. We should get a grade 10
class to go through this as a challenge.")

Most of the above is still static as I haven't updated the database
schema yet to include a lot of the fields. I want to try to get this
up and running properly before I go any further on other development.

This seems to get an initial load time of about a second, but then it
caches and goes blazingly fast. I'm currently on Dreamhost, and am
wondering if the slowness may be a factor of the host and not the

Please guys, any help would be GREATLY appreciated as I'm getting
close to ditching rails for this project all together and moving back
to PHP.


What kind of server are you running this on? 2+ seconds sounds like some
flavor of CGI.

Matt G-2 wrote:

It's a standard Dreamhost package so Apache with FastCGI and MySQL
4.x. Do you think it is a problem with the host then? I'm loath to
try a new host in case it is just a simple code issue. I seem to be
able to do much more complex queries in a fraction of a second using
PHP on the same server.


Host shouldn't be a problem. What do your logs tell you about where the time
is going. Can you tail your production.log and watch as you hit it? You're
describing "hello world" Ajax that's taking waaaaay too long. There has to
be something more.

Matt G-2 wrote:

Here's the latest from my logs. It seems to be a little faster right
now, but I figure that's due to a lack of traffic on the hosting
server, or with my connection:

Completed in 0.05424 (18 reqs/sec) | Rendering: 0.01482 (27%) | DB:
0.00679 (12%) | 200 OK

Sorry, I don't have much experience looking through the logs, so I'm
not sure if those are high or on par with what they should be.
Looking at firebug, the "GET" for the XML can take between 4000+ to
9000+ms when I first hit it.

Not blindingly fast, but not alarmingly slow. While these log entries are not
precise, they suggest a far faster turnaround of a request than what you are
experiencing. Is your initial pageload fast? Do static pages load fast?
Doesn't this seem like latency?

Matt G-2 wrote:

That's the kicker. When I leave this as a static page loading static
XML files (whith 10 or more entries instead of the 2 that are being
generated) the calls are almost instant. The page loads with no
slowness, and the tabs switch really fast. Blazingly fast when
compared to the Rails render.

Can you notice anyting that I'm doing with my code that'd be causing
this slowness? The code that I first posted is in the callxml.rxml
file, and the following is in the controller:

  def callxml
    @order_by = 'overall_score desc'
    @products = Product.find(:all, :order=>@order_by, :limit => '3')

At this point, I'm thinking of trying a host with a money back clause
to try if they are any better with the sped for generating the XML.
If it does show improvement, I'll just keep my site on that host and
continue my development. Otherwise I'll cancel it and convert
everything to PHP and call 'er a day.


Can you paste your .htaccess?

I betcha it says:

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

To use FCGI, you need:

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

If this is the case, then problem solved, but you should consider using
Apache to balance mongrels or a proxy setup that will allow you to use
mongrel_cluster (pound or pen).

Matt G-2 wrote:

Ok, I've made the change to the htaccess file, and that looks to have
sped things up considerably. I'm going to let a couple of others test
it out to see if they notice an improvement as well.

Unfortunately, I don't think I have enough control over the server to
setup mongrel on Dreamhost, but if you know a way, I'd love to try it

Thank you for all of your help! I'll let you know if I'm still having
any issues.