2012-01-08 131 views
1

我不知道如何解決我的應用程序的這個大性能問題。我正在使用open-uri來請求YouTube上最受歡迎的視頻,當我運行perftools時https://github.com/tmm1/perftools.rbRuby和Timeout.timeout性能問題

這表明最大的性能問題是Timeout.timeout。任何人都可以建議我如何解決這個問題?

我使用紅寶石1.8.7。

編輯:

這是從我的探查輸出

https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B4bANr--YcONZDRlMmFhZjQtYzIyOS00YjZjLWFlMGUtMTQyNzU5ZmYzZTU4&hl=en_US

+1

請出示實際的分析輸出,不只是告訴我們,這樣我們就可以看到你所看到的。 – jefflunt 2012-01-08 03:26:20

+0

它被指責超時,但塊內的代碼可能正在等待來自遠程服務器的響應。我同意需要添加輸出才能更好地理解。 – Nick 2012-01-08 03:28:55

+0

我已將我的輸出上傳到Google文檔https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B4bANr--YcONZDRlMmFhZjQtYzIyOS00YjZjLWFlMGUtMTQyNzU5ZmYzZTU4&hl=zh_CN – toy 2012-01-08 03:33:29

回答

0

超時被包裝,實際上是做的工作,以確保功能,如果服務器未能在一個響應某些時候,代碼會引發錯誤並停止執行。

我懷疑你所看到的是服務器需要一些時間來回應。您應該以某種方式查看緩存響應。

例如,使用memcached(僞)

require 'dalli' 
require 'open-uri' 

DALLI = Dalli.client.new 

class PopularVideos 
    def self.get 
    result = [] 
    unless result = DALLI.get("videos_#{Date.today.to_s}") 
     doc = open("http://youtube/url") 
     result = parse_videos(doc) # parse the doc somehow 
     DALLI.set("videos_#{Date.today.to_s}", result) 
    end 
    result 
    end 
end 

PopularVideos.get # calls your expensive parsing script once 
PopularVideos.get # gets the result from memcached for the rest of the day 
+0

另外,我注意到您正在使用Sinatra。隨着Padrino(擴展Sinatra),你會得到一些有用的緩存擴展。 – stef 2012-01-08 09:41:00

+0

非常感謝。還有什麼我應該改變的。:-) – toy 2012-01-08 11:19:06