您的位置:首页 > 编程语言 > PHP开发

php 性能测试工具

2011-05-06 13:12 302 查看
<?php 
/*
 * This class enables you to mark points and calculate the time difference
 * between them.  Memory consumption can also be displayed.
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Libraries
 * @author		ExpressionEngine Dev Team
 * @link	 http://codeigniter.com/user_guide/libraries/benchmark.html  */
class CI_Benchmark {

	var $marker = array();

	// --------------------------------------------------------------------

	/**
	 * Set a benchmark marker
	 *
	 * Multiple calls to this function can be made so that several
	 * execution points can be timed
	 *
	 * @access	public
	 * @param	string	$name	name of the marker
	 * @return	void
	 */
	function mark($name)
	{
		$this->marker[$name] = microtime();
	}

	// --------------------------------------------------------------------

	/**
	 * 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.
	 *
	 * @access	public
	 * @param	string	a particular marked point
	 * @param	string	a particular marked point
	 * @param	integer	the number of decimal places
	 * @return	mixed
	 */
	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();
		}
	
		list($sm, $ss) = explode(' ', $this->marker[$point1]);
		list($em, $es) = explode(' ', $this->marker[$point2]);

		return number_format(($em + $es) - ($sm + $ss), $decimals);
	}
 	
	// --------------------------------------------------------------------

	/**
	 * Memory Usage
	 *
	 * This function returns the {memory_usage} pseudo-variable.
	 * 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.
	 *
	 * @access	public
	 * @return	string
	 */
	function memory_usage()
	{
		return '{memory_usage}';
	}

}

// END CI_Benchmark class

/* End of file Benchmark.php */
/* Location: ./system/libraries/Benchmark.php */




一个小例子,如下:

<?php

header("Content-Type:text/html;charset=UTF-8");
if( ! defined('BASEPATH') ) {
	define ('BASEPATH', $_SERVER['DOCUMENT_ROOT'].'/news/');
}
require_once BASEPATH.'service/Json_service.php';
require_once BASEPATH.'common/Benchmark.php';

$benchmark = new CI_Benchmark();
$benchmark->mark('10');
$benchmark->mark('11');

$benchmark->mark('12');
$jsonservice = new JsonService();
$benchmark->mark('13');
$resultjson = $jsonservice->getNewsTitleJsonArray($categoryid=143746653,$index=1,$max=20,$lastpage,$lastid);
$benchmark->mark('14');
$jsonservice->clear();
//$size = strlen($resultjson);
//header("Content-Length: $size");
$benchmark->mark('15');
echo '-----------------------------------------'.'<br>';
echo $benchmark->elapsed_time('10','11',6).'<br>'; 
echo $benchmark->elapsed_time('11','12',6).'<br>';
echo $benchmark->elapsed_time('12','13',6).'<br>';
echo $benchmark->elapsed_time('13','14',6).'<br>';
echo $benchmark->elapsed_time('14','15',6).'<br>';
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: