Skip to content

Commit

Permalink
Added two new features and cleaned up the code a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
veloper committed Oct 28, 2010
1 parent 28f0fa0 commit 511a694
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions class.Bench.php
Expand Up @@ -233,21 +233,37 @@ public static function getElaspedSinceLastMark() {
}

/**
* Get the time (in seconds) elapsed from the start() to...
* If stop() has been called
* to stop
* else
* to the current microtime.
* Get the time elapsed (in seconds) based on context and/or parameters.
*
* getElapsed()
* if[stop() has been called] -- Time (in seconds() between start() and stop()
* else -- Time (in seconds) between start() and the getElapsed() call.
*
* getElapsed("from_mark_id", "to_mark_id") - Time (in seconds) between marks.
*
* @param mixed;
* @param mixed;
* @return mixed; float, false on error.
*/
public static function getElapsed() {
public static function getElapsed($from_mark_id = null, $to_mark_id = null) {
$microtime = microtime(true);
$elapsed = false;
if(self::$start === null) {
self::logError('Please call '.__CLASS__.'::start() before calling '.__CLASS__.'::getElapsed()');
return false;
}
$minuend = (self::$stop !== null) ? self::$stop : microtime(true);
return $minuend - self::$start;
if(!$from_mark_id && !$to_mark_id) {
$minuend = (self::$stop !== null) ? self::$stop : $microtime;
$elapsed = $minuend - self::$start;
} else {
if (($mark_from = self::getMarkById($from_mark_id)) && ($mark_to = self::getMarkById($to_mark_id))) {
$elapsed = abs($mark_to['microtime'] - $mark_from['microtime']);
} else {
if(!$mark_from) self::logError(__CLASS__.'::getElapsed(): A mark with the id of "'.$from_mark_id.'" does not exist.');
if(!$mark_to) self::logError(__CLASS__.'::getElapsed(): A mark with the id of "'.$to_mark_id.'" does not exist.');
}
}
return $elapsed;
}

/**
Expand All @@ -265,10 +281,10 @@ public static function getStats() {
if(count(self::getMarks())) {
// Average Time (in seconds) Between Marks
$stats['mark_average'] = self::getMarkAverage();
// The Longest Mark
$stats['mark_longest'] = self::getLongestMark();
// The Shortest Mark
$stats['mark_shortest'] = self::getShortestMark();
// The Longest Mark
$stats['mark_longest'] = self::getLongestMark();
}
// Start Microtime
$stats['start'] = self::$start;
Expand All @@ -279,6 +295,17 @@ public static function getStats() {
return $stats;
}

/**
* Dumps Stats, Marks, and Errors then (by default) kills the script.
*
* @param bool; if true die() -- else output.
* @return void;
*/
public static function dump($die = true) {
var_dump(array('STATISTICS'=>self::getStats(), 'MARKS'=>self::getMarks(), 'ERRORS'=>self::getErrors()));
if($die) die();
}

/**
* Returns true if any errors occurred.
*
Expand Down Expand Up @@ -306,5 +333,5 @@ public static function getErrors() {
protected static function logError($error) {
self::$errors[] = $error;
error_log(__CLASS__.': '.$error);
}
}
}

0 comments on commit 511a694

Please sign in to comment.