標籤:分析 沒有 _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