- <?php
- /**
- * @version $Id: profiler.php 10707 2008-08-21 09:52:47Z eddieajau $
- * @package Joomla.Framework
- * @subpackage Error
- * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
- * @license GNU/GPL, see LICENSE.php
- * Joomla! is free software. This version may have been modified pursuant
- * to the GNU General Public License, and as distributed it includes or
- * is derivative of works licensed under the GNU General Public License or
- * other free or open source software licenses.
- * See COPYRIGHT.php for copyright notices and details.
- */
- // Check to ensure this file is within the rest of the framework
- //查看標誌
- defined('JPATH_BASE') or die();
- /**
- * Utility class to assist in the process of benchmarking the execution
- * of sections of code to understand where time is being spent.
- *
- * @package Joomla.Framework
- * @subpackage Error
- * @since 1.0
- */
- class JProfiler extends JObject//繼承jobject
- {
- /**
- *
- * @var int
- */
- var $_start = 0;
- /**
- *
- * @var string
- */
- var $_prefix = '';
- /**
- *
- * @var array
- */
- var $_buffer= null;
- /**
- * Constructor
- *
- * @access protected
- * @param string Prefix for mark messages
- */
- function __construct( $prefix = '' )
- {
- $this->_start = $this->getmicrotime();//設置開始時間
- $this->_prefix = $prefix;//設置前綴
- $this->_buffer = array();//空數組
- }
- /**
- * Returns a reference to the global Profiler object, only creating it
- * if it doesn't already exist.
- * 創建一個jprofiler實例,有防止重複創建的功能
- * This method must be invoked as:
- * <pre> $browser = & JProfiler::getInstance( $prefix );</pre>
- *
- * @access public
- * @param string Prefix used to distinguish profiler objects.
- * @return JProfiler The Profiler object.
- */
- function &getInstance($prefix = '')
- {
- //joomla系統的分析的$prefix=application
- static $instances;//防止重複
- if (!isset($instances)) {//頭一回
- $instances = array();
- }
- if (emptyempty($instances[$prefix])) {//無重複,創建實例
- $instances[$prefix] = new JProfiler($prefix);
- }
- return $instances[$prefix];//返回實例
- }
- /**
- * Output a time mark
- *
- * The mark is returned as text enclosed in <div> tags
- * with a CSS class of 'profiler'.
- *
- * @access public
- * @param string A label for the time mark
- * @return string Mark enclosed in <div> tags
- */
- function mark( $label )
- {
- $mark = $this->_prefix." $label: ";//加上一個標籤
- //開始到現在的用了多少時間,時間度量爲毫秒
- $mark .= sprintf('%.3f', $this->getmicrotime() - $this->_start) . ' seconds';
- //如果存在memory_get_usage函數,加上PHP使用的內存,這個函數返回的單位是字節
- if ( function_exists('memory_get_usage') ) {
- $mark .= ', '.sprintf('%0.2f', memory_get_usage() / 1048576 ).' MB';
- }
- //放到_buffer中
- $this->_buffer[] = $mark;
- return $mark;
- }
- /**
- * Get the current time.
- * 取得現在的時間,毫秒爲單位
- * @access public
- * @return float The current time
- */
- function getmicrotime()
- {
- list( $usec, $sec ) = explode( ' ', microtime() );
- return ((float)$usec + (float)$sec);
- }
- /**
- * Get information about current memory usage.
- *
- * @access public
- * @return int The memory usage
- * @link PHP_MANUAL#memory_get_usage
- */
- function getMemory()
- {
- static $isWin;
- if (function_exists( 'memory_get_usage' )) {
- return memory_get_usage();
- } else {
- // Determine if a windows server
- if (is_null( $isWin )) {
- $isWin = (substr(PHP_OS, 0, 3) == 'WIN');
- }
- // Initialize variables
- $output = array();
- $pid = getmypid();//進程ID
- if ($isWin) {
- // Windows workaround
- @exec( 'tasklist /FI "PID eq ' . $pid . '" /FO LIST', $output );
- if (!isset($output[5])) {
- $output[5] = null;
- }
- return substr( $output[5], strpos( $output[5], ':' ) + 1 );
- } else {
- @exec("ps -o rss -p $pid", $output);//ps shell命令
- return $output[1] *1024;
- }
- }
- }
- /**
- * Get all profiler marks.
- * 返回所有分析標誌的結果
- * Returns an array of all marks created since the Profiler object
- * was instantiated. Marks are strings as per {@link JProfiler::mark()}.
- *
- * @access public
- * @return array Array of profiler marks
- */
- function getBuffer() {
- return $this->_buffer;
- }
- }
joomla1.56 profiler.php
windows獲得內存有點迷糊
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.