2019-12-18 16:07:09 -05:00
|
|
|
<?php
|
2014-08-08 15:41:59 -04:00
|
|
|
/**
|
|
|
|
* CodeIgniter
|
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* An open source application development framework for PHP
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* This content is released under the MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 - 2019, British Columbia Institute of Technology
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @package CodeIgniter
|
|
|
|
* @author EllisLab Dev Team
|
|
|
|
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
|
|
|
|
* @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
|
|
|
|
* @license https://opensource.org/licenses/MIT MIT License
|
|
|
|
* @link https://codeigniter.com
|
|
|
|
* @since Version 1.0.0
|
2014-08-08 15:41:59 -04:00
|
|
|
* @filesource
|
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
2014-08-08 15:41:59 -04:00
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Benchmark Class
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
|
|
|
* This class enables you to mark points and calculate the time difference
|
2019-12-18 16:07:09 -05:00
|
|
|
* between them. Memory consumption can also be displayed.
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
|
|
|
* @package CodeIgniter
|
|
|
|
* @subpackage Libraries
|
|
|
|
* @category Libraries
|
2019-12-18 16:07:09 -05:00
|
|
|
* @author EllisLab Dev Team
|
|
|
|
* @link https://codeigniter.com/user_guide/libraries/benchmark.html
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
|
|
|
class CI_Benchmark {
|
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* List of all benchmark markers
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @var array
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public $marker = array();
|
2014-08-08 15:41:59 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a benchmark marker
|
|
|
|
*
|
|
|
|
* Multiple calls to this function can be made so that several
|
2019-12-18 16:07:09 -05:00
|
|
|
* execution points can be timed.
|
2014-08-08 15:41:59 -04:00
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @param string $name Marker name
|
2014-08-08 15:41:59 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public function mark($name)
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
$this->marker[$name] = microtime(TRUE);
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2019-12-18 16:07:09 -05:00
|
|
|
* Elapsed time
|
|
|
|
*
|
2014-08-08 15:41:59 -04:00
|
|
|
* 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.
|
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @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.
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
if ($point1 === '')
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
|
|
|
return '{elapsed_time}';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! isset($this->marker[$point1]))
|
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! isset($this->marker[$point2]))
|
|
|
|
{
|
2019-12-18 16:07:09 -05:00
|
|
|
$this->marker[$point2] = microtime(TRUE);
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
2019-12-18 16:07:09 -05:00
|
|
|
return number_format($this->marker[$point2] - $this->marker[$point1], $decimals);
|
2014-08-08 15:41:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Memory Usage
|
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* Simply returns the {memory_usage} marker.
|
|
|
|
*
|
2014-08-08 15:41:59 -04:00
|
|
|
* 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.
|
|
|
|
*
|
2019-12-18 16:07:09 -05:00
|
|
|
* @return string '{memory_usage}'
|
2014-08-08 15:41:59 -04:00
|
|
|
*/
|
2019-12-18 16:07:09 -05:00
|
|
|
public function memory_usage()
|
2014-08-08 15:41:59 -04:00
|
|
|
{
|
|
|
|
return '{memory_usage}';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|