reports: Fix id of custom date selector
[ninja.git] / system / core / Benchmark.php
blobad9b5d30d76e70f42a80b329e20a1243fecf1775
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * Simple benchmarking.
5 * $Id: Benchmark.php 3917 2009-01-21 03:06:22Z zombor $
7 * @package Core
8 * @author Kohana Team
9 * @copyright (c) 2007 Kohana Team
10 * @license http://kohanaphp.com/license.html
12 final class Benchmark {
14 // Benchmark timestamps
15 private static $marks;
17 /**
18 * Set a benchmark start point.
20 * @param string benchmark name
21 * @return void
23 public static function start($name)
25 if ( ! isset(self::$marks[$name]))
27 self::$marks[$name] = array
29 'start' => microtime(TRUE),
30 'stop' => FALSE,
31 'memory_start' => function_exists('memory_get_usage') ? memory_get_usage() : 0,
32 'memory_stop' => FALSE
37 /**
38 * Set a benchmark stop point.
40 * @param string benchmark name
41 * @return void
43 public static function stop($name)
45 if (isset(self::$marks[$name]) AND self::$marks[$name]['stop'] === FALSE)
47 self::$marks[$name]['stop'] = microtime(TRUE);
48 self::$marks[$name]['memory_stop'] = function_exists('memory_get_usage') ? memory_get_usage() : 0;
52 /**
53 * Get the elapsed time between a start and stop.
55 * @param string benchmark name, TRUE for all
56 * @param integer number of decimal places to count to
57 * @return array
59 public static function get($name, $decimals = 4)
61 if ($name === TRUE)
63 $times = array();
64 $names = array_keys(self::$marks);
66 foreach ($names as $name)
68 // Get each mark recursively
69 $times[$name] = self::get($name, $decimals);
72 // Return the array
73 return $times;
76 if ( ! isset(self::$marks[$name]))
77 return FALSE;
79 if (self::$marks[$name]['stop'] === FALSE)
81 // Stop the benchmark to prevent mis-matched results
82 self::stop($name);
85 // Return a string version of the time between the start and stop points
86 // Properly reading a float requires using number_format or sprintf
87 return array
89 'time' => number_format(self::$marks[$name]['stop'] - self::$marks[$name]['start'], $decimals),
90 'memory' => (self::$marks[$name]['memory_stop'] - self::$marks[$name]['memory_start'])
94 } // End Benchmark