blob: feedbf5708a98ea4664519444c95e839867357b2 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Code Igniter Benchmark Class
20 *
21 * This class enables you to mark points and calculate the time difference
22 * between them. Memory consumption can also be displayed.
23 *
24 * @package CodeIgniter
25 * @subpackage Libraries
26 * @category Libraries
27 * @author Rick Ellis
28 * @link http://www.codeigniter.com/user_guide/libraries/benchmark.html
29 */
30class CI_Benchmark {
31
32 var $marker = array();
33
adminb0dd10f2006-08-25 17:25:49 +000034
35 // --------------------------------------------------------------------
36
37 /**
38 * Set a benchmark marker
39 *
40 * Multiple calls to this function can be made so that several
41 * execution points can be timed
42 *
43 * @access public
44 * @param string $name name of the marker
45 * @return void
46 */
47 function mark($name)
48 {
49 $this->marker[$name] = microtime();
50 }
51 // END mark()
52
53 // --------------------------------------------------------------------
54
55 /**
56 * Calculates the time difference between two marked points.
57 *
58 * If the first parameter is empty this function instead returns the
59 * {elapsed_time} pseudo-variable. This permits the the full system
60 * execution time to be shown in a template. The output class will
61 * swap the real value for this variable.
62 *
63 * @access public
64 * @param string a paricular marked point
65 * @param string a paricular marked point
66 * @param integer the number of decimal places
67 * @return mixed
68 */
69 function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
70 {
71 if ($point1 == '')
72 {
73 return '{elapsed_time}';
74 }
75
76 if ( ! isset($this->marker[$point2]))
77 $this->marker[$point2] = microtime();
78
79 list($sm, $ss) = explode(' ', $this->marker[$point1]);
80 list($em, $es) = explode(' ', $this->marker[$point2]);
81
82 return number_format(($em + $es) - ($sm + $ss), $decimals);
83 }
84 // END elapsed_time()
85
86 // --------------------------------------------------------------------
87
88 /**
admine26611f2006-10-02 21:59:12 +000089 * Auto Profiler
90 *
91 * This function cycles through the entire array of mark points and
92 * matches any two points that are named identially (ending in "_start"
93 * and "_end" respectively). It then compiles the execution times for
94 * all points and returns it as an array
95 *
96 * @access public
97 * @return array
98 */
99 function auto_profiler()
100 {
101 $marker_keys = array_reverse(array_keys($this->marker));
102
103 $times = array();
104 foreach ($marker_keys as $val)
105 {
106 if (preg_match("/(.+?)_start/i", $val, $match))
107 {
108 if (isset($this->marker[$match[1].'_end']))
109 {
110 $times[$match[1]] = $this->elapsed_time($val, $match[1].'_end');
111 }
112 }
113 }
114
115 return $times;
116 }
117
118 // --------------------------------------------------------------------
119
120 /**
adminb0dd10f2006-08-25 17:25:49 +0000121 * Memory Usage
122 *
123 * This function returns the {memory_usage} pseudo-variable.
124 * This permits it to be put it anywhere in a template
125 * without the memory being calculated until the end.
126 * The output class will swap the real value for this variable.
127 *
128 * @access public
129 * @return string
130 */
131 function memory_usage()
132 {
133 return '{memory_usage}';
134 }
135 // END memory_usage()
136
137}
138
139// END CI_Benchmark class
140?>