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

CodeIgniter框架源码学习之基准测试类--Benchmake.php

2017-07-28 11:47 567 查看
文件位置:./system/core/Benchmake.php
<?php
/**
* CodeIgniter
*
* An open source application development framework for PHP
*
* This content is released under the MIT License (MIT)
*
* Copyright (c) 2014 - 2017, British Columbia Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @package CodeIgniter
* @author EllisLab Dev Team
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
* @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
* @license http://opensource.org/licenses/MIT MIT License
* @link https://codeigniter.com * @since Version 1.0.0
* @filesource
*/
defined('BASEPATH') OR exit('No direct script access allowed');

/**
* Benchmark Class
*
* 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 EllisLab Dev Team
* @link https://codeigniter.com/user_guide/libraries/benchmark.html */
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($
4000
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
* 0.1344 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.
* 如果没有给出明确的时间点,那么会计算出整个程序运行的时间。 
* 怎么可以做到计算出整个程序的运行时间的呢?其实执行此计算的是Output组件。 
  *而调用Benchmark::elapsed_time();(无参数)的时候,实质上先返回的并不是 
* 整个程序运行的时间,也不可能做到,实质返回的是一个0.1344标签,然后 

* Output在处理输出的时候,再计算出整个程序运行时间,因为处理输出阶段程序可以视 

 * 为处于最后阶段,于是可以近似计算出总时间,然后把输出中的0.1344替换掉。 
   * 下面的memory_usage()原理相同。 
*
* @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 '0.1344';
}

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 1.57MB 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 '1.57MB'
*/
public function memory_usage()
{
return '1.57MB';
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息