我们回到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的更多相关文章

  1. CI框架源码学习笔记1——index.php

    做php开发一年多了,陆陆续续用过tp/ci/yii框架,一直停留在只会使用的层面上,关于框架内部的结构实际上是不甚了解的.为了深入的学习,决定把CI框架的源码从头到尾的学习一下, 主要因为CI框架工 ...

  2. CI框架源码学习笔记7——Utf8.php

    愉快的清明节假期结束了,继续回到CI框架学习.这一节我们来看看Utf8.php文件,它主要是用来做utf8编码,废话不多说,上代码. class CI_Utf8 { /** * Class const ...

  3. CI框架源码学习笔记5——Hooks.php

    接着Benchmark.php往下看,下一个引入的文件是Hooks.php,我们称之为钩子.它的目的是在不改变核心文件的基础上,来修改框架的内部运作流程.具体使用方法参见手册http://codeig ...

  4. CI框架源码学习笔记2——Common.php

    上一节我们最后说到了CodeIgniter.php,可是这一节的标题是Common.php,有的朋友可能会觉得很奇怪.事实上,CodeIgniter.php其实包含了ci框架启动的整个流程. 里面引入 ...

  5. CI框架源码学习笔记6——Config.php

    接着上一节往下,我们这一节来看看配置类Config.php,对应手册内容http://codeigniter.org.cn/user_guide/libraries/config.html. clas ...

  6. CI框架源码学习笔记3——Log.php

    上一节说完了Common.php,然而跟代码打交道总是免不了日志记录,所以这一节我们说说Log.php文件. 先看看类里面的几个属性, protected $_log_path;  日志路径 prot ...

  7. CI框架源码阅读笔记5 基准测试 BenchMark.php

    上一篇博客(CI框架源码阅读笔记4 引导文件CodeIgniter.php)中,我们已经看到:CI中核心流程的核心功能都是由不同的组件来完成的.这些组件类似于一个一个单独的模块,不同的模块完成不同的功 ...

  8. CI框架源码阅读笔记4 引导文件CodeIgniter.php

    到了这里,终于进入CI框架的核心了.既然是“引导”文件,那么就是对用户的请求.参数等做相应的导向,让用户请求和数据流按照正确的线路各就各位.例如,用户的请求url: http://you.host.c ...

  9. CI框架源码阅读笔记3 全局函数Common.php

    从本篇开始,将深入CI框架的内部,一步步去探索这个框架的实现.结构和设计. Common.php文件定义了一系列的全局函数(一般来说,全局函数具有最高的加载优先权,因此大多数的框架中BootStrap ...

随机推荐

  1. LTE230方案示意图

    普天LTE230产品及解决方案   责任编辑:耿鹏飞 2016.12.26 16:42 来源:通信世界网 LTE230 普天   一.LTE230系统特点 (一)系统背景 随着社会的快速发展,能源危机 ...

  2. Regexp:正则表达式应用——实例应用

    ylbtech-Regexp:正则表达式应用——实例应用 1. 实例应用返回顶部 1. 1.验证用户名和密码:("^[a-zA-Z]\w{5,15}$")正确格式:"[A ...

  3. java restful response 万能类

    import java.util.HashMap; import java.util.Map; public class ResponseData { private final String mes ...

  4. 单片机RS485通信接口、控制线、原理图及程序实例

    RS232 标准是诞生于 RS485 之前的,但是 RS232 有几处不足的地方: 接口的信号电平值较高,达到十几 V,使用不当容易损坏接口芯片,电平标准也与TTL 电平不兼容. 传输速率有局限,不可 ...

  5. 数据校验(2)--demo1---bai

    input_score.jsp <%@ page language="java" import="java.util.*" pageEncoding=&q ...

  6. java selenium webdriver第三讲 helloWord

    第一步:建立Maven项目 Selenium 支持 maven 工程,这会让你的工作更加简便. 用 Eclipse 建个 Maven 的工程,建成后,修改 pom.xml <dependenci ...

  7. php学习之try catch

    PHP 5 添加了类似于其它语言的异常处理模块.在 PHP 代码中所产生的异常可被 throw语句抛出并被 catch 语句捕获.(注:一定要先抛才能获取) 需要进行异常处理的代码都必须放入 try ...

  8. ORACLE——日期时间格式化参数详解 之二

    2.8 DD 指定日期在当月中第几天(范围:1-31) SQL> select to_char(sysdate,'DD YYYY-MM-DD PM hh24:mi:ss ') from dual ...

  9. 在Linux下adb连接不上android手机的终极解决方案

    转自: http://blog.csdn.net/liuqz2009/article/details/7942569 1.做android开发的过程,碰到了Linux下adb识别不了android设备 ...

  10. C语言基础问题总结

    Double 类型用 %f 接受会产生错误,应该用%lf C语言中SIN,COS的参数为弧度,角度应先转换为弧度才能计算,否则出错 三角形的判断条件,任意两边长度和大于第三边 闰年判断条件整百年为40 ...