blob: 66e8a24cfb71cc0f0ec9775663584e515cb86f93 [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.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
adminb0dd10f2006-08-25 17:25:49 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
adminb0dd10f2006-08-25 17:25:49 +000016// ------------------------------------------------------------------------
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 * Set a benchmark marker
38 *
39 * Multiple calls to this function can be made so that several
40 * execution points can be timed
41 *
42 * @access public
43 * @param string $name name of the marker
44 * @return void
45 */
admine334c472006-10-21 19:44:22 +000046 function mark($name)
47 {
48 $this->marker[$name] = microtime();
49 }
adminb0dd10f2006-08-25 17:25:49 +000050
51 // --------------------------------------------------------------------
52
53 /**
54 * Calculates the time difference between two marked points.
55 *
admine334c472006-10-21 19:44:22 +000056 * If the first parameter is empty this function instead returns the
57 * {elapsed_time} pseudo-variable. This permits the full system
adminb0dd10f2006-08-25 17:25:49 +000058 * execution time to be shown in a template. The output class will
59 * swap the real value for this variable.
60 *
61 * @access public
admin16040622006-10-21 19:09:49 +000062 * @param string a particular marked point
63 * @param string a particular marked point
adminb0dd10f2006-08-25 17:25:49 +000064 * @param integer the number of decimal places
65 * @return mixed
66 */
admine334c472006-10-21 19:44:22 +000067 function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
68 {
69 if ($point1 == '')
70 {
adminb0dd10f2006-08-25 17:25:49 +000071 return '{elapsed_time}';
admine334c472006-10-21 19:44:22 +000072 }
73
74 if ( ! isset($this->marker[$point1]))
75 {
76 return '';
77 }
78
79 if ( ! isset($this->marker[$point2]))
80 {
81 $this->marker[$point2] = microtime();
82 }
83
84 list($sm, $ss) = explode(' ', $this->marker[$point1]);
85 list($em, $es) = explode(' ', $this->marker[$point2]);
86
87 return number_format(($em + $es) - ($sm + $ss), $decimals);
88 }
admine26611f2006-10-02 21:59:12 +000089
90 // --------------------------------------------------------------------
91
92 /**
adminb0dd10f2006-08-25 17:25:49 +000093 * Memory Usage
94 *
95 * This function returns the {memory_usage} pseudo-variable.
admine334c472006-10-21 19:44:22 +000096 * This permits it to be put it anywhere in a template
97 * without the memory being calculated until the end.
adminb0dd10f2006-08-25 17:25:49 +000098 * The output class will swap the real value for this variable.
99 *
100 * @access public
101 * @return string
102 */
103 function memory_usage()
104 {
105 return '{memory_usage}';
106 }
adminb0dd10f2006-08-25 17:25:49 +0000107
108}
109
110// END CI_Benchmark class
111?>