zoukankan      html  css  js  c++  java
  • 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}';
        }
    
    }
  • 相关阅读:
    052-34
    052-33
    052-32
    052-31
    052-30
    052-28
    react组件之间通信
    排序算法
    点外卖
    js的各种排序算法
  • 原文地址:https://www.cnblogs.com/isuifeng/p/6602678.html
Copyright © 2011-2022 走看看