Initial Kohana install
[speedfreak] / Server / system / core / Benchmark.php
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3  * Simple benchmarking.
4  *
5  * $Id: Benchmark.php 4149 2009-04-01 13:32:50Z Shadowhand $
6  *
7  * @package    Core
8  * @author     Kohana Team
9  * @copyright  (c) 2007 Kohana Team
10  * @license    http://kohanaphp.com/license.html
11  */
12 final class Benchmark {
13
14         // Benchmark timestamps
15         private static $marks;
16
17         /**
18          * Set a benchmark start point.
19          *
20          * @param   string  benchmark name
21          * @return  void
22          */
23         public static function start($name)
24         {
25                 if ( ! isset(self::$marks[$name]))
26                 {
27                         self::$marks[$name] = array();
28                 }
29
30                 $mark = array
31                 (
32                         'start'        => microtime(TRUE),
33                         'stop'         => FALSE,
34                         'memory_start' => self::memory_usage(),
35                         'memory_stop'  => FALSE
36                 );
37
38                 array_unshift(self::$marks[$name], $mark);
39         }
40
41         /**
42          * Set a benchmark stop point.
43          *
44          * @param   string  benchmark name
45          * @return  void
46          */
47         public static function stop($name)
48         {
49                 if (isset(self::$marks[$name]) AND self::$marks[$name][0]['stop'] === FALSE)
50                 {
51                         self::$marks[$name][0]['stop'] = microtime(TRUE);
52                         self::$marks[$name][0]['memory_stop'] = self::memory_usage();
53                 }
54         }
55
56         /**
57          * Get the elapsed time between a start and stop.
58          *
59          * @param   string   benchmark name, TRUE for all
60          * @param   integer  number of decimal places to count to
61          * @return  array
62          */
63         public static function get($name, $decimals = 4)
64         {
65                 if ($name === TRUE)
66                 {
67                         $times = array();
68                         $names = array_keys(self::$marks);
69
70                         foreach ($names as $name)
71                         {
72                                 // Get each mark recursively
73                                 $times[$name] = self::get($name, $decimals);
74                         }
75
76                         // Return the array
77                         return $times;
78                 }
79
80                 if ( ! isset(self::$marks[$name]))
81                         return FALSE;
82
83                 if (self::$marks[$name][0]['stop'] === FALSE)
84                 {
85                         // Stop the benchmark to prevent mis-matched results
86                         self::stop($name);
87                 }
88
89                 // Return a string version of the time between the start and stop points
90                 // Properly reading a float requires using number_format or sprintf
91                 $time = $memory = 0;
92                 for ($i = 0; $i < count(self::$marks[$name]); $i++)
93                 {
94                         $time += self::$marks[$name][$i]['stop'] - self::$marks[$name][$i]['start'];
95                         $memory += self::$marks[$name][$i]['memory_stop'] - self::$marks[$name][$i]['memory_start'];
96                 }
97
98                 return array
99                 (
100                         'time'   => number_format($time, $decimals),
101                         'memory' => $memory,
102                         'count'  => count(self::$marks[$name])
103                 );
104         }
105
106         /**
107          * Returns the current memory usage. This is only possible if the
108          * memory_get_usage function is supported in PHP.
109          *
110          * @return  integer
111          */
112         private static function memory_usage()
113         {
114                 static $func;
115
116                 if ($func === NULL)
117                 {
118                         // Test if memory usage can be seen
119                         $func = function_exists('memory_get_usage');
120                 }
121
122                 return $func ? memory_get_usage() : 0;
123         }
124
125 } // End Benchmark