joomla1.56 profiler.php

 windows獲得內存有點迷糊

  1. <?php
  2. /**
  3. * @version      $Id: profiler.php 10707 2008-08-21 09:52:47Z eddieajau $
  4. * @package      Joomla.Framework
  5. * @subpackage   Error
  6. * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. * @license      GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // Check to ensure this file is within the rest of the framework
  15. //查看標誌
  16. defined('JPATH_BASE'or die();
  17. /**
  18.  * Utility class to assist in the process of benchmarking the execution
  19.  * of sections of code to understand where time is being spent.
  20.  *
  21.  * @package     Joomla.Framework
  22.  * @subpackage  Error
  23.  * @since 1.0
  24.  */
  25. class JProfiler extends JObject//繼承jobject
  26. {
  27.     /**
  28.      * 
  29.      * @var int
  30.      */
  31.     var $_start = 0;
  32.     /**
  33.      *
  34.      * @var string
  35.      */
  36.     var $_prefix = '';
  37.     /**
  38.      *
  39.      * @var array
  40.      */
  41.     var $_buffer= null;
  42.     /**
  43.      * Constructor
  44.      *
  45.      * @access protected
  46.      * @param string Prefix for mark messages
  47.      */
  48.     function __construct( $prefix = '' )
  49.     {
  50.         $this->_start = $this->getmicrotime();//設置開始時間
  51.         $this->_prefix = $prefix;//設置前綴
  52.         $this->_buffer = array();//空數組
  53.     }
  54.     /**
  55.      * Returns a reference to the global Profiler object, only creating it
  56.      * if it doesn't already exist.
  57.      * 創建一個jprofiler實例,有防止重複創建的功能
  58.      * This method must be invoked as:
  59.      *      <pre>  $browser = & JProfiler::getInstance( $prefix );</pre>
  60.      *
  61.      * @access public
  62.      * @param string Prefix used to distinguish profiler objects.
  63.      * @return JProfiler  The Profiler object.
  64.      */
  65.     function &getInstance($prefix = '')
  66.     {
  67.         //joomla系統的分析的$prefix=application
  68.         static $instances;//防止重複
  69.         if (!isset($instances)) {//頭一回
  70.             $instances = array();
  71.         }
  72.         if (emptyempty($instances[$prefix])) {//無重複,創建實例
  73.             $instances[$prefix] = new JProfiler($prefix);
  74.         }
  75.         return $instances[$prefix];//返回實例
  76.     }
  77.     /**
  78.      * Output a time mark
  79.      *
  80.      * The mark is returned as text enclosed in <div> tags
  81.      * with a CSS class of 'profiler'.
  82.      *
  83.      * @access public
  84.      * @param string A label for the time mark
  85.      * @return string Mark enclosed in <div> tags
  86.      */
  87.     function mark( $label )
  88.     {
  89.         $mark   = $this->_prefix." $label: ";//加上一個標籤
  90.         //開始到現在的用了多少時間,時間度量爲毫秒
  91.         $mark   .= sprintf('%.3f'$this->getmicrotime() - $this->_start) . ' seconds';
  92.         //如果存在memory_get_usage函數,加上PHP使用的內存,這個函數返回的單位是字節
  93.         if ( function_exists('memory_get_usage') ) {
  94.             $mark   .= ', '.sprintf('%0.2f', memory_get_usage() / 1048576 ).' MB';
  95.         }
  96.         //放到_buffer中
  97.         $this->_buffer[] = $mark;
  98.         return $mark;
  99.     }
  100.     /**
  101.      * Get the current time.
  102.      * 取得現在的時間,毫秒爲單位
  103.      * @access public
  104.      * @return float The current time
  105.      */
  106.     function getmicrotime()
  107.     {
  108.         list( $usec$sec ) = explode' ', microtime() );
  109.         return ((float)$usec + (float)$sec);
  110.     }
  111.     /**
  112.      * Get information about current memory usage.
  113.      *
  114.      * @access public
  115.      * @return int The memory usage
  116.      * @link PHP_MANUAL#memory_get_usage
  117.      */
  118.     function getMemory()
  119.     {
  120.         static $isWin;
  121.         if (function_exists( 'memory_get_usage' )) {
  122.             return memory_get_usage();
  123.         } else {
  124.             // Determine if a windows server
  125.             if (is_null$isWin )) {
  126.                 $isWin = (substr(PHP_OS, 0, 3) == 'WIN');
  127.             }
  128.             // Initialize variables
  129.             $output = array();
  130.             $pid = getmypid();//進程ID
  131.             if ($isWin) {
  132.                 // Windows workaround
  133.                 @exec'tasklist /FI "PID eq ' . $pid . '" /FO LIST'$output );
  134.                 if (!isset($output[5])) {
  135.                     $output[5] = null;
  136.                 }
  137.                 return substr$output[5], strpos$output[5], ':' ) + 1 );
  138.             } else {
  139.                 @exec("ps -o rss -p $pid"$output);//ps shell命令
  140.                 return $output[1] *1024;
  141.             }
  142.         }
  143.     }
  144.     /**
  145.      * Get all profiler marks.
  146.      * 返回所有分析標誌的結果
  147.      * Returns an array of all marks created since the Profiler object
  148.      * was instantiated.  Marks are strings as per {@link JProfiler::mark()}.
  149.      *
  150.      * @access public
  151.      * @return array Array of profiler marks
  152.      */
  153.     function getBuffer() {
  154.         return $this->_buffer;
  155.     }
  156. }

發佈了31 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章