CI架構學習筆記4——Benchmark.php

來源:互聯網
上載者:User

標籤:分析   沒有   _for   記錄   anywhere   color   pre   int   筆記   

我們回到Codeigniter.php上繼續往下看,第一個引入的類檔案是Benchmark.php,這個檔案主要是提供基準測試,具體使用方法參考手冊http://codeigniter.org.cn/user_guide/libraries/benchmark.html。建議小夥伴們都讀一讀手冊,弄懂功能的使用後,再來分析代碼,才會事半功倍。不多說了,下面進入正題。

測試類別定義了一個陣列變數public $marker = array(),他的目的主要是用來記錄我們在檔案中添加的測試點。

  public function mark($name)    {        $this->marker[$name] = microtime(TRUE);    }

mark方法的內容很簡單,就一行代碼,記錄測試點當前的時間戳記。

  public function elapsed_time($point1 = ‘‘, $point2 = ‘‘, $decimals = 4)    {        if ($point1 === ‘‘)        {            return ‘{elapsed_time}‘;        }        if ( ! isset($this->marker[$point1]))        {            return ‘‘;        }        if ( ! isset($this->marker[$point2]))        {            $this->marker[$point2] = microtime(TRUE);        }        return number_format($this->marker[$point2] - $this->marker[$point1], $decimals);    }

elapsed_time方法三個參數,$point1和$point2是計算時間差值的測試點名稱,$decimals是精確的位元。

當$point1為空白時,返回字串{elapsed_time},事實上這個字串會被Output組件替換。

$elapsed = $BM->elapsed_time(‘total_execution_time_start‘, ‘total_execution_time_end‘);$output = str_replace(array(‘{elapsed_time}‘, ‘{memory_usage}‘), array($elapsed, $memory), $output);

上面兩行中是output組件中的代碼,可以看到實際上擷取的是total_execution_time_start和total_execution_time_end之間的時間差,其中total_execution_time_start是架構載入後的第一個時間點,而total_execution_time_end在架構中並沒有定義,繼續往下看知道$point2沒有定義時取得為目前時間戳,所以這裡的時間差實際就為系統載入運行到時間差計算出的時間。

往下看代碼很簡單了,$point1為空白的時候返回空值,$point2為空白是取測試點1到當前的時間差。

  public function memory_usage()    {        return ‘{memory_usage}‘;    }

方法memory_usage用來取得當前記憶體的使用。

 

Benchmark.php檔案的內容很簡單,完整代碼如下。

class CI_Benchmark {    /**     * List of all benchmark markers     *     * @var    array     */    public $marker = array();    /**     * Set a benchmark marker     *     * Multiple calls to this function can be made so that several     * execution points can be timed.     *     * @param    string    $name    Marker name     * @return    void     */    public function mark($name)    {        $this->marker[$name] = microtime(TRUE);    }    // --------------------------------------------------------------------    /**     * Elapsed time     *     * Calculates the time difference between two marked points.     *     * If the first parameter is empty this function instead returns the     * {elapsed_time} pseudo-variable. This permits the full system     * execution time to be shown in a template. The output class will     * swap the real value for this variable.     *     * @param    string    $point1        A particular marked point     * @param    string    $point2        A particular marked point     * @param    int    $decimals    Number of decimal places     *     * @return    string    Calculated elapsed time on success,     *            an ‘{elapsed_string}‘ if $point1 is empty     *            or an empty string if $point1 is not found.     */    public function elapsed_time($point1 = ‘‘, $point2 = ‘‘, $decimals = 4)    {        if ($point1 === ‘‘)        {            return ‘{elapsed_time}‘;        }        if ( ! isset($this->marker[$point1]))        {            return ‘‘;        }        if ( ! isset($this->marker[$point2]))        {            $this->marker[$point2] = microtime(TRUE);        }        return number_format($this->marker[$point2] - $this->marker[$point1], $decimals);    }    // --------------------------------------------------------------------    /**     * Memory Usage     *     * Simply returns the {memory_usage} marker.     *     * This permits it to be put it anywhere in a template     * without the memory being calculated until the end.     * The output class will swap the real value for this variable.     *     * @return    string    ‘{memory_usage}‘     */    public function memory_usage()    {        return ‘{memory_usage}‘;    }}

 

CI架構學習筆記4——Benchmark.php

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.