blob: e80ee54ddde5fc5f7ad70a90c28d1c3862e0a287 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030030 * Benchmark Class
Derek Allard2067d1a2008-11-13 22:59:24 +000031 *
32 * This class enables you to mark points and calculate the time difference
Andrey Andreev92ebfb62012-05-17 12:49:24 +030033 * between them. Memory consumption can also be displayed.
Derek Allard2067d1a2008-11-13 22:59:24 +000034 *
35 * @package CodeIgniter
36 * @subpackage Libraries
37 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/libraries/benchmark.html
40 */
41class CI_Benchmark {
42
root83e7a8e2011-08-14 19:55:57 +020043 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030044 * List of all benchmark markers
root83e7a8e2011-08-14 19:55:57 +020045 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030046 * @var array
root83e7a8e2011-08-14 19:55:57 +020047 */
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030048 public $marker = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000049
50 /**
51 * Set a benchmark marker
52 *
53 * Multiple calls to this function can be made so that several
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030054 * execution points can be timed.
Derek Allard2067d1a2008-11-13 22:59:24 +000055 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030056 * @param string $name Marker name
Derek Allard2067d1a2008-11-13 22:59:24 +000057 * @return void
58 */
Andrey Andreev2fbbfe32012-01-07 18:37:15 +020059 public function mark($name)
Derek Allard2067d1a2008-11-13 22:59:24 +000060 {
dixyab3cab52012-04-21 00:58:00 +020061 $this->marker[$name] = microtime(TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000062 }
63
64 // --------------------------------------------------------------------
65
66 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030067 * Elapsed time
68 *
Derek Allard2067d1a2008-11-13 22:59:24 +000069 * Calculates the time difference between two marked points.
70 *
71 * If the first parameter is empty this function instead returns the
72 * {elapsed_time} pseudo-variable. This permits the full system
73 * execution time to be shown in a template. The output class will
74 * swap the real value for this variable.
75 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030076 * @param string $point1 A particular marked point
77 * @param string $point2 A particular marked point
78 * @param int $decimals Number of decimal places
79 *
80 * @return string Calculated elapsed time on success,
81 * an '{elapsed_string}' if $point1 is empty
82 * or an empty string if $point1 is not found.
Derek Allard2067d1a2008-11-13 22:59:24 +000083 */
Andrey Andreev2fbbfe32012-01-07 18:37:15 +020084 public function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
Derek Allard2067d1a2008-11-13 22:59:24 +000085 {
Alex Bilbieed944a32012-06-02 11:07:47 +010086 if ($point1 === '')
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
88 return '{elapsed_time}';
89 }
90
91 if ( ! isset($this->marker[$point1]))
92 {
93 return '';
94 }
95
96 if ( ! isset($this->marker[$point2]))
97 {
dixyab3cab52012-04-21 00:58:00 +020098 $this->marker[$point2] = microtime(TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000099 }
Barry Mienydd671972010-10-04 16:33:58 +0200100
dixyab3cab52012-04-21 00:58:00 +0200101 return number_format($this->marker[$point2] - $this->marker[$point1], $decimals);
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 }
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 // --------------------------------------------------------------------
105
106 /**
107 * Memory Usage
108 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300109 * Simply returns the {memory_usage} marker.
110 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 * This permits it to be put it anywhere in a template
112 * without the memory being calculated until the end.
113 * The output class will swap the real value for this variable.
114 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300115 * @return string '{memory_usage}'
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 */
Andrey Andreev2fbbfe32012-01-07 18:37:15 +0200117 public function memory_usage()
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
119 return '{memory_usage}';
120 }
121
122}
123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124/* End of file Benchmark.php */
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300125/* Location: ./system/core/Benchmark.php */