tims-todo/system/core/Benchmark.php

134 lines
3.9 KiB
PHP
Raw Permalink Normal View History

<?php
2014-08-08 15:41:59 -04:00
/**
* CodeIgniter
*
* An open source application development framework for PHP
2014-08-08 15:41:59 -04: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
*/
defined('BASEPATH') OR exit('No direct script access allowed');
2014-08-08 15:41:59 -04:00
/**
* Benchmark Class
2014-08-08 15:41:59 -04:00
*
* This class enables you to mark points and calculate the time difference
* between them. Memory consumption can also be displayed.
2014-08-08 15:41:59 -04:00
*
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author EllisLab Dev Team
* @link https://codeigniter.com/user_guide/libraries/benchmark.html
2014-08-08 15:41:59 -04:00
*/
class CI_Benchmark {
/**
* List of all benchmark markers
2014-08-08 15:41:59 -04:00
*
* @var array
2014-08-08 15:41:59 -04: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
* execution points can be timed.
2014-08-08 15:41:59 -04:00
*
* @param string $name Marker name
2014-08-08 15:41:59 -04:00
* @return void
*/
public function mark($name)
2014-08-08 15:41:59 -04:00
{
$this->marker[$name] = microtime(TRUE);
2014-08-08 15:41:59 -04: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.
*
* @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
*/
public function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
2014-08-08 15:41:59 -04: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]))
{
$this->marker[$point2] = microtime(TRUE);
2014-08-08 15:41:59 -04:00
}
return number_format($this->marker[$point2] - $this->marker[$point1], $decimals);
2014-08-08 15:41:59 -04:00
}
// --------------------------------------------------------------------
/**
* Memory Usage
*
* 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.
*
* @return string '{memory_usage}'
2014-08-08 15:41:59 -04:00
*/
public function memory_usage()
2014-08-08 15:41:59 -04:00
{
return '{memory_usage}';
}
}