blob: f94db2721d590144109a8f0402fa72e7f306248e [file] [log] [blame]
Andrey Andreev2fbbfe32012-01-07 18:37:15 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev2fbbfe32012-01-07 18:37:15 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev2fbbfe32012-01-07 18:37:15 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Andreev2fbbfe32012-01-07 18:37:15 +020018 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030029 * Benchmark Class
Derek Allard2067d1a2008-11-13 22:59:24 +000030 *
31 * This class enables you to mark points and calculate the time difference
Andrey Andreev92ebfb62012-05-17 12:49:24 +030032 * between them. Memory consumption can also be displayed.
Derek Allard2067d1a2008-11-13 22:59:24 +000033 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/benchmark.html
39 */
40class CI_Benchmark {
41
root83e7a8e2011-08-14 19:55:57 +020042 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030043 * List of all benchmark markers
root83e7a8e2011-08-14 19:55:57 +020044 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030045 * @var array
root83e7a8e2011-08-14 19:55:57 +020046 */
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030047 public $marker = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000048
49 /**
50 * Set a benchmark marker
51 *
52 * Multiple calls to this function can be made so that several
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030053 * execution points can be timed.
Derek Allard2067d1a2008-11-13 22:59:24 +000054 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030055 * @param string $name Marker name
Derek Allard2067d1a2008-11-13 22:59:24 +000056 * @return void
57 */
Andrey Andreev2fbbfe32012-01-07 18:37:15 +020058 public function mark($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000059 {
dixyab3cab52012-04-21 00:58:00 +020060 $this->marker[$name] = microtime(TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000061 }
62
63 // --------------------------------------------------------------------
64
65 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030066 * Elapsed time
67 *
Derek Allard2067d1a2008-11-13 22:59:24 +000068 * 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 Andreev3e9d2b82012-10-27 14:28:51 +030075 * @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 Allard2067d1a2008-11-13 22:59:24 +000082 */
Andrey Andreev2fbbfe32012-01-07 18:37:15 +020083 public function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
Derek Allard2067d1a2008-11-13 22:59:24 +000084 {
Alex Bilbieed944a32012-06-02 11:07:47 +010085 if ($point1 === '')
Derek Allard2067d1a2008-11-13 22:59:24 +000086 {
87 return '{elapsed_time}';
88 }
89
90 if ( ! isset($this->marker[$point1]))
91 {
92 return '';
93 }
94
95 if ( ! isset($this->marker[$point2]))
96 {
dixyab3cab52012-04-21 00:58:00 +020097 $this->marker[$point2] = microtime(TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000098 }
Barry Mienydd671972010-10-04 16:33:58 +020099
dixyab3cab52012-04-21 00:58:00 +0200100 return number_format($this->marker[$point2] - $this->marker[$point1], $decimals);
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 }
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 // --------------------------------------------------------------------
104
105 /**
106 * Memory Usage
107 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300108 * Simply returns the {memory_usage} marker.
109 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 * 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 Andreev3e9d2b82012-10-27 14:28:51 +0300114 * @return string '{memory_usage}'
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 */
Andrey Andreev2fbbfe32012-01-07 18:37:15 +0200116 public function memory_usage()
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 {
118 return '{memory_usage}';
119 }
120
121}
122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123/* End of file Benchmark.php */
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300124/* Location: ./system/core/Benchmark.php */