0 Replies - 2092 Views - Last Post: 09 March 2012 - 11:22 AM Rate Topic: -----

#1 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Rails - increase speed of ajax requests

Posted 09 March 2012 - 11:22 AM

I'm working on a LAN based web application where speed and responsiveness is ideal. I want my web app to be just as responsive as a desktop app, but I'm having trouble because it's a database driven app.

Here's my output:
Started GET "/cute_functions/is_valid_pc_sku?pc=1&sku=1" for 192.168.0.17 at 2012-03-09 11:56:13 -0600
  Processing by CuteFunctionsController#is_valid_pc_sku? as
  Parameters: {"pc"=>"1", "sku"=>"1"}
  User Load (0.1ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  SQL (0.1ms)  SELECT COUNT(*) FROM `products` WHERE `products`.`pc` = '1' AND `products`.`sku` = '1'
  Product Load (0.1ms)  SELECT `products`.* FROM `products` WHERE `products`.`pc` = '1' AND `products`.`sku` = '1' LIMIT 1
  ProductCategory Load (0.1ms)  SELECT `product_categories`.* FROM `product_categories` WHERE `product_categories`.`Category` = 1 LIMIT 1
Completed 200 OK in [b]117ms[/b] (Views: [b]5.5ms[/b] | ActiveRecord: [b]0.3ms[/b])



Server
Platform: VM Ware 7 (Debian 64bit) >> On >> Windows 7
Web Proxy: nginx (keepalive set to 64)
rails server: thin

Client
Browser: Fire Fox (haven't tried disabling extensions or anything yet)
OS: Windows 7
Network Interface: Wireless -> 100mb LAN -> Server (could this be doing anything, I haven't tried hard lining my laptop yet either).

So what's the deal with that output, it looks like rails is being extremely fast in views and activeRecord, but then it says it's taking it 117ms total. In addition, here's the amount of time it's taking according to Fire Fox (which is why I'm prompted to action, I'd like consistently less than 150ms, ideally 100ms):

Quote





Here's what my js looks like and my ruby if you're interested:

(js)
// This function now doubles as GetProductInformation()!!!  Woohoo!
ajaxFunc.CheckIfValidPcSku = function(pc, sku){
	$.ajaxSetup({ async: false });
	
	var jqxhr = $.ajax( "/cute_functions/is_valid_pc_sku?pc=" + pc + "&sku=" + sku )  // it should say home
	    .fail(function() { alert("error sending ajax request.  Contact IT if this error persists with code: badjax"); })
	
	var jsonObj = eval('(' + jqxhr.responseText + ')');
	if (jqxhr.responseText == 'false'){
		return false;
	}
	else{
		return jsonObj;
	}
}



(ruby)
class CuteFunctionsController < ApplicationController
  .
  .
  .
# This function will return the Product details or false
  def is_valid_pc_sku?
    pc = params[:pc]
    sku = params[:sku]
    
    c = Product.where(:pc => pc, :sku => sku)
    
    if c.empty?
      render :text => "false"
    else
      #render :text => "true"
      return_hash = merge_in_taxability(c.first) # we need to return the Taxable attribute too, which is found in another table...
      render :json => return_hash, :status => :ok
    end
  end

  .
  .
  .
  
  private

  # make sure you send in the first object of an array of products, not the array...
  def merge_in_taxability(product)
    product_category = ProductCategory.where(:Category => product.Pc).first
    important_keys = {}
    important_keys['Taxable'] = product_category.attributes['Taxable']
    
    return product.attributes.merge(important_keys)
  end



It would seem though, that my ruby is efficient enough to execute within 6ms and other stuff is mysteriously bogging the system down.

Is This A Good Question/Topic? 1
  • +

Page 1 of 1