CI框架源码学习笔记4——Benchmark.php
我们回到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}';
} }
CI框架源码学习笔记4——Benchmark.php的更多相关文章
- CI框架源码学习笔记1——index.php
做php开发一年多了,陆陆续续用过tp/ci/yii框架,一直停留在只会使用的层面上,关于框架内部的结构实际上是不甚了解的.为了深入的学习,决定把CI框架的源码从头到尾的学习一下, 主要因为CI框架工 ...
- CI框架源码学习笔记7——Utf8.php
愉快的清明节假期结束了,继续回到CI框架学习.这一节我们来看看Utf8.php文件,它主要是用来做utf8编码,废话不多说,上代码. class CI_Utf8 { /** * Class const ...
- CI框架源码学习笔记5——Hooks.php
接着Benchmark.php往下看,下一个引入的文件是Hooks.php,我们称之为钩子.它的目的是在不改变核心文件的基础上,来修改框架的内部运作流程.具体使用方法参见手册http://codeig ...
- CI框架源码学习笔记2——Common.php
上一节我们最后说到了CodeIgniter.php,可是这一节的标题是Common.php,有的朋友可能会觉得很奇怪.事实上,CodeIgniter.php其实包含了ci框架启动的整个流程. 里面引入 ...
- CI框架源码学习笔记6——Config.php
接着上一节往下,我们这一节来看看配置类Config.php,对应手册内容http://codeigniter.org.cn/user_guide/libraries/config.html. clas ...
- CI框架源码学习笔记3——Log.php
上一节说完了Common.php,然而跟代码打交道总是免不了日志记录,所以这一节我们说说Log.php文件. 先看看类里面的几个属性, protected $_log_path; 日志路径 prot ...
- CI框架源码阅读笔记5 基准测试 BenchMark.php
上一篇博客(CI框架源码阅读笔记4 引导文件CodeIgniter.php)中,我们已经看到:CI中核心流程的核心功能都是由不同的组件来完成的.这些组件类似于一个一个单独的模块,不同的模块完成不同的功 ...
- CI框架源码阅读笔记4 引导文件CodeIgniter.php
到了这里,终于进入CI框架的核心了.既然是“引导”文件,那么就是对用户的请求.参数等做相应的导向,让用户请求和数据流按照正确的线路各就各位.例如,用户的请求url: http://you.host.c ...
- CI框架源码阅读笔记3 全局函数Common.php
从本篇开始,将深入CI框架的内部,一步步去探索这个框架的实现.结构和设计. Common.php文件定义了一系列的全局函数(一般来说,全局函数具有最高的加载优先权,因此大多数的框架中BootStrap ...
随机推荐
- 删除pool error的解决方法
标签(空格分隔): ceph,ceph运维,pool 问题描述: 删除pool的时候提示下面的错误: [root@node3 ~]# ceph osd pool delete ecpool ecpoo ...
- 01-19asp.net基础--网站登录及验证
第一步: 1)首先使用“CodeSmith”将Examinee类实体化,并生成实体类连接数据库的方法,存在解决方案下的“App_Code”文件夹下. 修改一下连接某个数据库: private SqlC ...
- Delphi Cookie
Cookie IdHTTP1.CookieManager.AddCookies(); IdHTTP1.Post(); IdHTTP1.Get('http://1.1.1.1:9000/'); for ...
- 修改linux内核启动logo及显示位置
转载于:http://blog.chinaunix.net/uid-28458801-id-3484269.html 在此基础上我又添加了我的一些不同的地方,仅供参考 内核版本: 2.6.35.3 l ...
- windows系统中启动应用需要的端口被别的程序占用
开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在任务管理器中选&qu ...
- Android中SQLite查询date类型字段出现有返回但是为错误值的情况
出现该情况的原因是因为查询精度与数据库中存储精度不相同造成的,例如,查询精度为 YYYY-MM-DD 但是存储精度为 YYYY-MM-DD HH:MM:SS,就会出现该错误. 更改查询精度为YYYY- ...
- fragment界面交互实操(步骤)
首先,新建一个继承了fragment类的类,在oncreateview方法中,使用方法的参数inflater,用其inflater.inflate(R.layout.fragment1,contain ...
- strcmp()比较函数和strcasecmp()和strnatcmp()
strcmp()的函数原型如下() int strcmp(string str1,string str2) 该函数需要两个进行比较的参数字符串,如果这两个字符串相等,该函数就返回0,如果按字典顺序st ...
- Luogu 3759 [TJOI2017]不勤劳的图书管理员
再也不作死写FhqTreap作内层树了,卡的不如暴力呜呜呜…… 题意翻译:给一个序列,每个下标包含两个属性$a$和$v$,求第一个属性与下标形成的所有逆序对的第二个属性和,给出$m$个交换两个下标的操 ...
- MySQL中的时间问题
MySQL 获得当前日期时间 函数 获得当前日期+时间(date + time)函数:now() mysql> select now(); +---------------------+ | n ...