KumbiaPHP beta2-dev
Framework PHP en español
benchmark.php
Ir a la documentación de este archivo.
00001 <?php
00025 final class Benchmark
00026 {
00032     private static $_benchmark;
00033     private static $_avgload = 0;
00039     public static function start_clock ($name)
00040     {
00041         if (! isset(self::$_benchmark[$name])) {
00042             self::$_benchmark[$name] = array('start_time' => microtime() , 'final_time' => 0 , 'memory_start' => memory_get_usage() , 'memory_stop' => 0 , 'time_execution' => 0);
00043         }
00044     }
00051     private static function _stop_clock ($name)
00052     {
00053         if (isset(self::$_benchmark[$name])) {
00054             if (PHP_OS == 'Linux') {
00055                 $load = sys_getloadavg();
00056             } else {
00057                 $load = 0;
00058             }
00059             self::$_avgload = $load[0];
00060             self::$_benchmark[$name]['memory_stop'] = memory_get_usage();
00061             self::$_benchmark[$name]['final_time'] = microtime();
00062             list ($sm, $ss) = explode(' ', self::$_benchmark[$name]['start_time']);
00063             list ($em, $es) = explode(' ', self::$_benchmark[$name]['final_time']);
00064             self::$_benchmark[$name]['time_execution'] = number_format(($em + $es) - ($sm + $ss), 4);
00065             return self::$_benchmark[$name]['time_execution'];
00066         }
00067     }
00073     public static function memory_usage ($name)
00074     {
00075         if (self::$_benchmark[$name]) {
00076             self::$_benchmark[$name]['memory_usage'] = number_format((self::$_benchmark[$name]['memory_stop'] - self::$_benchmark[$name]['memory_start']) / 1048576, 2);
00077             return self::$_benchmark[$name]['memory_usage'];
00078         } else {
00079             throw new KumbiaException("No existe el Benchmark para el nombre: '$name', especificado \n");
00080         }
00081     }
00087     public static function time_execution ($name)
00088     {
00089         if (isset(self::$_benchmark[$name])) {
00090             return self::_stop_clock($name);
00091         } else {
00092             throw new KumbiaException("No existe el Benchmark para el nombre: $name, especificado \n");
00093         }
00094     }
00099     public static function test ($func, $loops)
00100     {
00101         self::start_clock($func);
00102         ob_start();
00103         for ($i = 1; $i <= $loops; $i ++) {
00104             eval($func);
00105         }
00106         ob_end_flush();
00107         $time = self::time_execution($func);
00108         echo '** Funcion: ', $func;
00109         echo $loops, ' veces';
00110         echo ' Tiempo: ', $time;
00111     }
00112 }
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Enumeraciones