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 ...
随机推荐
- centos 虚拟机联网
在windows主机安装centos虚拟机后,遇到虚拟机连接外网问题. 解决方案:http://blog.csdn.net/pang040328/article/details/12427359 经过 ...
- HTTP-Runoob:HTTP请求头信息
ylbtech-HTTP-Runoob:HTTP请求头信息 1.返回顶部 1. HTTP 响应头信息 HTTP请求头提供了关于请求,响应或者其他的发送实体的信息. 在本章节中我们将具体来介绍HTTP响 ...
- 【转】rails 遇到 Could not find a JavaScript runtime execjs错误(ubuntu)
当我运行 $rails s 遇到下面错误 sudo apt-get install python-software-properties sudo add-apt-repository ppa:chr ...
- ManualResetEvent 用法
第一.简单介绍 ManualResetEvent 允许线程通过发信号互相通信.通常,此通信涉及一个线程在其他线程进行之前必须完成的任务.当一个线程开始一个活动(此活动必须完成后,其他线程才能开始)时, ...
- distinct可以用in代替(小技巧)
distinct可以用in代替,in的好处是直接能获取所有数据,而distunct只能获取distinct的字段,不过效率肯定高一些.
- iphone配置实用工具iPhone Configuration Utility
下载地址 http://support.apple.com/kb/DL1466 安装完毕后,在设备->控制台,可以很方便看到报错信息
- PhoneGap打Android包报错
1.下载AndroidSDK,安装 2.下载Phonegap,解压,为以后打包用 3.下载Node.js,安装 4.下载并安装Ant工具 5.配置环境变量 ANT_HOME=ANT主目录路径 PATH ...
- python paramiko 调试
#!/usr/bin/env python #-*- encoding:utf-8 -*- import paramiko transport = paramiko.Transport(('10.34 ...
- 问题:css 自动换行;结果:CSS控制文本自动换行
CSS控制文本自动换行 CSS控制文本自动换行,阅读CSS控制文本自动换行,1.你定死表格的宽度,即给表格一个宽度值(是 数值,不是百分比) 2.强制不换行div{//white-space:不换行; ...
- LNMP 1.6 常见的502问题解决
在nginx上跑discuz,先修改配置文件 cd /usr/local/nginx/conf/vhosts/ vim test.conf server { listen ; server_name ...