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 ...
随机推荐
- 实验吧CTF题库-编程(部分)
百米 3秒提交答案,数字是随机变化的 利用Python脚本解题 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import ...
- 简单的触发黑名单阻断演示 control+c
#include "stdafx.h"#include <signal.h>#include <windows.h> #include <iostre ...
- Windows下.svn文件夹的最简易删除方法(附linux)
如果想删除Windows下的.svn文件夹,通过手动删除的渠道是最麻烦的,因为每个文件夹下面都存在这样的文件.下面是一个好办法:建立一个文本文件,取名为kill-svn-folders.reg(扩展名 ...
- 01-16委托Func
在类中编写方法: 在主函数中调用函数: 效果图:
- python之Dict和set类型
Dict就是一种key:value的表格: >>> d = { 'Adam':95, 'Lisa':85, 'Bart':59, 'Paul':75 } >>> p ...
- div+css学习笔记一(转)
div+css学习笔记一 (2011-05-12 07:32:08) 标签: div css 居中 背景图片 ie6 ie7 margin 杂谈 分类: 网页制作 1.IE6中用了float之后导致m ...
- HDU 6395(2018多校第7场1010)Sequence
不久前做过POJ3070,所以知道这题要用矩阵快速幂优化,但是这个题的递推公式中有一项⌊p/n⌋,场上就不会了... 下来才知道要用分块矩阵快速幂,因为⌊p/n⌋最多有2√p块,可以对每一块使用快速幂 ...
- 在html中打开PDF
<object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="990" heig ...
- echarts柱状图每个柱子显示不同颜色,并且能够实现点击每种颜色影藏对应柱子的功能
---------------------------------------------------------代码区---------------------------------------- ...
- Linux共享对象之编译参数 -fPIC
转载自:https://www.cnblogs.com/cswuyg/p/3830703.html 在Linux系统中,动态链接文件称为动态共享对象(DSO,Dynamic Shared Ob ...