joomla1.56 profiler.php

来源:互联网 发布:android 申请网络权限 编辑:程序博客网 时间:2024/05/01 12:41
 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. }

原创粉丝点击