2010-08-23 58 views
1

我注意到我的網站上的AJAX調用速度很慢,看着NewRelic RPM我注意到大部分的成本來自Authlogic/find降低authlogic/ajax調用的成本

我想這是因爲rails需要每次查看用戶(即使在AJAX調用中),但147微秒似乎非常慢。

Category Segment  % Time  Avg Calls (per Txn)  Avg Exclusive Time (ms) 
Custom  Authlogic/find  77  1.0  147 
Controller LikesController#toggle 13 1.0  25 
Database SQL - SHOW 4.9  1.5  9.3 
Database SQL - OTHER  1.1  3.7  2.1 
ActiveRecord User#find 0.8  1.0  1.5 
View application.html.haml Template 0.7  0.2  1.3 
View _like.html.haml Template 0.5  0.8  0.89 
ActiveRecord ActiveRecord::SessionStore::Session#find 0.4  1.0  0.69 

這裏是我的代碼:

class LikesController < ApplicationController 
    ## commented this part out in the hopes of reducing authlogic needs, since it's simple for me to check for current_user myself. 

    # access_control do 
    # allow :admin 
    # allow logged_in, :to => [:toggle] 
    # end 
    before_filter :check_if_admin, :except => [:toggle] 

    def index 
    @likes = Like.all 
    end 

    def toggle 
    if !current_user 
     render :nothing => true and return 
    end 

    like = Like.find(:first, :conditions => "user_id = #{current_user.id} and likable_id = #{params[:likable_id]} and likable_type = '#{params[:likable_type]}'") 
    if like != nil 
     like.destroy 
     @current_user_likes_likable = false 
    else 
     like = Like.new(:likable_id => params[:likable_id], :likable_type => params[:likable_type], :user => current_user) 
     like.save 
     @current_user_likes_likable = true 
    end 
    @likable_id = params[:likable_id] 
    @likable_type = params[:likable_type] 
    #@likable = Kernel.const_get(params[:likable_type]).find(params[:likable_id]) 
    render "shared/like" 
    end 
... 

而且這裏的地方CURRENT_USER在ApplicationController中定義:

class ApplicationController < ActionController::Base 
    helper :layout 
    helper_method :current_user_session, :current_user, :current_user_is_admin 

    private 
    def current_user_session 
     return @current_user_session if defined?(@current_user_session) 
     @current_user_session = UserSession.find 
    end 
    def current_user 
     return @current_user if defined?(@current_user) 
     @current_user = current_user_session && current_user_session.record 
    end 

回答

0

您正在使用什麼數據庫?如果你的查詢運行緩慢,首先要看的是indexes on your database。我不知道find方法正在運行什麼SQL,但這是一個很好的開始。確保在列上有正確的索引(可能需要多個索引)。

0

我在另一個應用程序中有同樣的問題。我已經使用authlogic構建了一個乾淨的項目:http://github.com/josei/authlogic_rpm

Newrelic已安裝並正在運行,因此您可以快速檢查authlogic的性能。正如你所看到的,性能非常好(sqlite3大約需要50ms),所以開銷必須在別的地方(也許是一個插件)。