CI框架源码学习笔记4——Benchmark.php
2024-09-03 11:59:08
我们回到Codeigniter.php上继续往下看,第一个引入的类文件是Benchmark.php,这个文件主要是提供基准测试,具体使用方法参考手册http://codeigniter.org.cn/user_guide/libraries/benchmark.html。建议小伙伴们都读一读手册,弄懂功能的使用后,再来分析代码,才会事半功倍。不多说了,下面进入正题。
测试类定义了一个数组变量public $marker = array(),他的目的主要是用来记录我们在文件中添加的测试点。
public function mark($name)
{
$this->marker[$name] = microtime(TRUE);
}
mark方法的内容很简单,就一行代码,记录测试点当前的时间戳。
public 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(TRUE);
} return number_format($this->marker[$point2] - $this->marker[$point1], $decimals);
}
elapsed_time方法三个参数,$point1和$point2是计算时间差值的测试点名称,$decimals是精确的位数。
当$point1为空时,返回字符串{elapsed_time},事实上这个字符串会被Output组件替换。
$elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
$output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output);
上面两行中是output组件中的代码,可以看到实际上获取的是total_execution_time_start和total_execution_time_end之间的时间差,其中total_execution_time_start是框架加载后的第一个时间点,而total_execution_time_end在框架中并没有定义,继续往下看知道$point2没有定义时取得为当前时间戳,所以这里的时间差实际就为系统加载运行到时间差计算出的时间。
往下看代码很简单了,$point1为空的时候返回空值,$point2为空是取测试点1到当前的时间差。
public function memory_usage()
{
return '{memory_usage}';
}
方法memory_usage用来取得当前内存的使用。
Benchmark.php文件的内容很简单,完整代码如下。
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($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
* {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.
*
* @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 '{elapsed_time}';
} 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 {memory_usage} 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 '{memory_usage}'
*/
public function memory_usage()
{
return '{memory_usage}';
} }
最新文章
- POI读取EXCEL(2007以上)
- 【完全开源】百度地图Web service API C#.NET版,带地图显示控件、导航控件、POI查找控件
- A*寻路算法
- PHP生成验证码及单实例应用
- LeetCode Sum of Two Integers
- win32自绘按钮,使用GDI+(二)
- 汉企学习4个半月的target and plan
- 随机森林与GBDT
- ubuntu vim 插件安装
- jsonp封装
- 【Unity 3D】使用 2DToolkit 插件 制作2D精灵动画
- Asp.Net 常用工具类之Office—Excel导出(4)
- 使用ssh 登录Linux 文件上传下载方法
- Redis in python
- python-django-01
- Executor, ExecutorService 和 Executors 间的不同
- [leetcode.com]算法题目 - Pascal's Triangle
- 二、K3 WISE 开发插件《 工业单据老单客户端插件事件、属性、方法》
- grafana+influxdb+telegraf监控服务器cpu,内存和硬盘
- UWP 记一次WTS 和 UCT翻车经历