<?php
/**
*
* @author Benjamin Gillissen <hide@address.com>
*
* **************************************************************
Copyright (C) 2009 Benjamin Gillissen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details at:
http://www.gnu.org/copyleft/gpl.html
* **************************************************************
*/
class chrono {
private static $chronos, $start;
public function __construct(){ }
public static function init($ref){
if ( ! CORE::isphpModLoaded('bcmath') ){
if ( ! CORE::LoadphpMod('bcmath') ){
errors::raise('Missing PHP extention, ask your admin to reinstall php with the bcmath support', CORE_LOG_ALERT, 'CHRONO');
}
}
if ( isset(self::$chronos[$ref]) ){ unset(self::$chronos[$ref]); }
self::$chronos[$ref]['elapsed'] = 0;
self::reqstart();
}
public static function isinit($ref){ return isset(self::$chronos[$ref]); }
public static function reqstart(){
if ( !isset(self::$start) ){ self::$start = self::get_microtime(); }
}
public static function start($ref){
if ( ! isset(self::$chronos[$ref]) ){ return; } //no init
if ( isset(self::$chronos[$ref]['startat']) ){ return; } //already started
self::$chronos[$ref]['startat'] = self::get_microtime();
self::$chronos[$ref]['pausekey'] = CORE::hash(NULL, 5);
return self::$chronos[$ref]['pausekey'];
}
public static function pause($ref, $key=NULL){
if ( ! isset(self::$chronos[$ref]) ){ return; } //no init
if ( ! isset(self::$chronos[$ref]['startat']) ){ return; } //not started, or paused
if ( self::$chronos[$ref]['pausekey'] !== $key ){ return; } //not good context!
$elapsed = bcsub(self::get_microtime(), self::$chronos[$ref]['startat'], 4);
self::$chronos[$ref]['elapsed'] = bcadd(self::$chronos[$ref]['elapsed'], $elapsed, 4);
unset(self::$chronos[$ref]['startat'], self::$chronos[$ref]['pausekey']);
}
public static function stop($ref){
if ( ! isset(self::$chronos[$ref]) ){ return 0; } //no init
if ( isset(self::$chronos[$ref]['startat']) ){ self::pause($ref, self::$chronos[$ref]['pausekey']); }//started
return self::$chronos[$ref]['elapsed']; //in ms
}
public static function stopall(){
$tot['time'] = bcsub(self::get_microtime(), self::$start, 4);
$tot['ref'] = 'tot';
$buf=0;
foreach(self::$chronos as $ref => $val){
$b['ref'] = $ref;
$b['time'] = self::stop($ref);
if ( $b['time'] != 0 ){ $o[] = $b; }
//$buf = bcadd($b['time'], $buf, 4);
}
//$other = Array( 'ref'=> 'other', 'time' => bcsub($tot['time'], $buf, 4) );
//$o[] = $other;
$o[] = $tot;
//echo '<pre>'.print_r($o, TRUE).'</pre>';
return $o;
}
public static function get_microtime(){
list($usec, $sec) = explode(" ", microtime());
return bcadd($usec, $sec, 4);
}
}