2010-01-29 53 views
0

我在做一個應用程序,我保持一定的性能廣泛的調查和我已經建立了一個簡單的解決方案來跟蹤請求的執行時間,但我無法找到的信息來驗證,如果這將是令人滿意的準確。是析構函數來標記腳本的執行結束正確的地方?

這似乎已抓獲一些有用的信息,我已經消除了一些性能問題,結果,但我也看到了一些令人困惑的條目,讓我懷疑的記錄執行時間的準確性。

我應該在每次調用腳本的末尾添加一個明確的方法調用,以紀念其執行結束或正在使用的析構函數足夠好這個(而整潔)的方法呢?

在請求的腳本的頂部調用代碼:

if(file_exists('../../../bench')) 
{ 
    require('../../../includes/classes/Bench.php'); 
    $Bench = new Bench; 
} 

這裏是類定義(減少清晰度):

require_once('Class.php'); 

class Bench extends Class 
{ 
    protected $page_log = 'bench-pages.log'; 
    protected $page_time_end; 
    protected $page_time_start; 

    public function __construct() 
    { 
     $this->set_page_time_start(); 
    } 

    public function __destruct() 
    { 
     $this->set_page_time_end(); 
     $this->add_page_record(); 
    } 

    public function add_page_record() 
    { 
     $line = $this->page_time_end - $this->page_time_start.','. 
       base64_encode(serialize($_SERVER)).','. 
       base64_encode(serialize($_GET)).','. 
       base64_encode(serialize($_POST)).','. 
       base64_encode(serialize($_SESSION))."\n"; 

     $fh = fopen(APP_ROOT . '/' . $this->page_log, 'a'); 
     fwrite($fh, $line); 
    } 

    public function set_page_time_end() 
    { 
     $this->page_time_end = microtime(true); 
    } 

    public function set_page_time_start() 
    { 
     $this->page_time_start = microtime(true); 
    } 
} 

回答

1

你需要做的是用Xdebug。一旦你設置它,並沒有改變你的代碼什麼得到什麼了多長時間全覆蓋它是非常簡單的。

+0

謝謝檢查,埃裏克,但我特別避免要求輪廓戰略的建議(這是足以覆蓋其他地方)。我的問題是關於PHP卸載與請求服務相關的對象的時間。 (爲了記錄,我們已經使用Xdebug。) – 2010-01-29 23:46:29