2011-02-17 132 views

回答

1

你可以寫一個裝飾:

import datetime 
import cherrypy 

def request_timer(f, *args, **kwargs): 
    def _request_time(f, *args, **kwargs): 
     begin = datetime.datetime.now() 
     response = f(*args, **kwargs) 
     end = datetime.datetime.now() 
     print cherrypy.log('time took for request %s' % (end - begin)) 
     return response 
    return _request_time(f, *args, **kwargs) 

class Root(object): 

    @request_timer 
    def index(*args, **kwargs): 
     pass 
    index.exposed = True 

日誌信息會去要麼屏幕或您的error_log文件,這取決於您的設置。

+0

我理解這個代碼,它的時間有多長,執行公開方法的方式。我想從CherryPy服務器接收請求的位置開始計時,直到它發送響應。我目前正在查看CherryPy源wsgiserver下`HTTPConnection`類的`communicate()`方法。 – luis 2011-02-18 04:53:33

1

這取決於你想要測量什麼。如果您只想要處理頁面處理程序邏輯,請使用代碼Y. H. Wong發佈。如果你想爲客戶看到它測量的總時間,使用類似Apache ab

$ python myproject.py & 
$ ab -n 1000 -c 10 http://localhost:8080/myapp 
+0

這也是我打算使用的方法之一。我實際上試圖查看請求在堆棧的每個部分需要多長時間(網絡,CherryPy,Cheetah,db等) – luis 2011-02-18 04:55:25