Andrey Andreev | 2fbbfe3 | 2012-01-07 18:37:15 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 2fbbfe3 | 2012-01-07 18:37:15 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 2fbbfe3 | 2012-01-07 18:37:15 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
Andrey Andreev | 2fbbfe3 | 2012-01-07 18:37:15 +0200 | [diff] [blame] | 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 29 | * Benchmark Class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 30 | * |
| 31 | * This class enables you to mark points and calculate the time difference |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 32 | * between them. Memory consumption can also be displayed. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 33 | * |
| 34 | * @package CodeIgniter |
| 35 | * @subpackage Libraries |
| 36 | * @category Libraries |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 37 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 38 | * @link http://codeigniter.com/user_guide/libraries/benchmark.html |
| 39 | */ |
| 40 | class CI_Benchmark { |
| 41 | |
root | 83e7a8e | 2011-08-14 19:55:57 +0200 | [diff] [blame] | 42 | /** |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 43 | * List of all benchmark markers |
root | 83e7a8e | 2011-08-14 19:55:57 +0200 | [diff] [blame] | 44 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 45 | * @var array |
root | 83e7a8e | 2011-08-14 19:55:57 +0200 | [diff] [blame] | 46 | */ |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 47 | public $marker = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Set a benchmark marker |
| 51 | * |
| 52 | * Multiple calls to this function can be made so that several |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 53 | * execution points can be timed. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 54 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 55 | * @param string $name Marker name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 56 | * @return void |
| 57 | */ |
Andrey Andreev | 2fbbfe3 | 2012-01-07 18:37:15 +0200 | [diff] [blame] | 58 | public function mark($name) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 59 | { |
dixy | ab3cab5 | 2012-04-21 00:58:00 +0200 | [diff] [blame] | 60 | $this->marker[$name] = microtime(TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // -------------------------------------------------------------------- |
| 64 | |
| 65 | /** |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 66 | * Elapsed time |
| 67 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 68 | * Calculates the time difference between two marked points. |
| 69 | * |
| 70 | * If the first parameter is empty this function instead returns the |
| 71 | * {elapsed_time} pseudo-variable. This permits the full system |
| 72 | * execution time to be shown in a template. The output class will |
| 73 | * swap the real value for this variable. |
| 74 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 75 | * @param string $point1 A particular marked point |
| 76 | * @param string $point2 A particular marked point |
| 77 | * @param int $decimals Number of decimal places |
| 78 | * |
| 79 | * @return string Calculated elapsed time on success, |
| 80 | * an '{elapsed_string}' if $point1 is empty |
| 81 | * or an empty string if $point1 is not found. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | */ |
Andrey Andreev | 2fbbfe3 | 2012-01-07 18:37:15 +0200 | [diff] [blame] | 83 | public function elapsed_time($point1 = '', $point2 = '', $decimals = 4) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | { |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 85 | if ($point1 === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | { |
| 87 | return '{elapsed_time}'; |
| 88 | } |
| 89 | |
| 90 | if ( ! isset($this->marker[$point1])) |
| 91 | { |
| 92 | return ''; |
| 93 | } |
| 94 | |
| 95 | if ( ! isset($this->marker[$point2])) |
| 96 | { |
dixy | ab3cab5 | 2012-04-21 00:58:00 +0200 | [diff] [blame] | 97 | $this->marker[$point2] = microtime(TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 98 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 99 | |
dixy | ab3cab5 | 2012-04-21 00:58:00 +0200 | [diff] [blame] | 100 | return number_format($this->marker[$point2] - $this->marker[$point1], $decimals); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 101 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 102 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 103 | // -------------------------------------------------------------------- |
| 104 | |
| 105 | /** |
| 106 | * Memory Usage |
| 107 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 108 | * Simply returns the {memory_usage} marker. |
| 109 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | * This permits it to be put it anywhere in a template |
| 111 | * without the memory being calculated until the end. |
| 112 | * The output class will swap the real value for this variable. |
| 113 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame^] | 114 | * @return string '{memory_usage}' |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | */ |
Andrey Andreev | 2fbbfe3 | 2012-01-07 18:37:15 +0200 | [diff] [blame] | 116 | public function memory_usage() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | { |
| 118 | return '{memory_usage}'; |
| 119 | } |
| 120 | |
| 121 | } |
| 122 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | /* End of file Benchmark.php */ |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 124 | /* Location: ./system/core/Benchmark.php */ |