Skip to content

Instantly share code, notes, and snippets.

@arieh
Created December 11, 2010 08:53
Show Gist options
  • Save arieh/737264 to your computer and use it in GitHub Desktop.
Save arieh/737264 to your computer and use it in GitHub Desktop.
a fixed version of this class : http://www.phpclasses.org/browse/file/33699.html
<?php
/**
*Benchmark class, it's a good solution for testing your web applications and scripts!
*also it can be a permanent tool you can integrate into your systems that will indicate the performance of your app/script etc.
* Test Time and memory consumption!
* Example usage:
*
* <code>
* require('Benchmark.class.php');
* Benchmark::startTimer();
* echo Benchmark::showTimer(5) . ' sec. ';
* echo Benchmark::showMemory('kb') . ' kb' ;
* </code>
*
*@author David Constantine Kurushin
*@link http://www.zend.com/en/store/education/certification/authenticate.php/ClientCandidateID/ZEND015209/RegistrationID/238001337
*@version 10.12.2010
*@package Benchmark
*@copyright Copyright (c) 2010, David Constantine Kurushin
*/
class Benchmark{
/**
*Class Fields startTime and stopTime
* @var double $sartTime holds the starting time
* @var double $stopTime holds the ending time
*/
private $startTime , $stopTime; //no static. also no need to set null
/**
* reformats a microtime string
* @param string a microtime string
*@return full microtime
*/
private function getm($time){
return array_sum(explode(" ",$time);
}
/**
* starts the timer
*/
public function startTimer(){//Benchmark::startTimer();
$this->$startTime = microtime();
}
/**
* stops the timer
*/
private function stopTimer(){//Benchmark::stopTimer();
$this->$stopTime=microtime();
}
/**
*Calculate the time elapsed from starting to stopping in second
*@param integer $round set how much numbers you want after Zero, default is 10
*@return double the time elapsed from starting to stopping in seconds
*/
public function showTimer($round=10){
if(!$this->$startTime)
throw new LogicException('A timer must be started before a time can be measured');
else
$this->$stopTime=$this->getm();
return number_format( ($this->getm($this->$stopTime) - $this->getm($this->$startTime)) , $round, '.', '');
}
/**
*Calculate the memory that used/alocated to your script
*@param String $string you should choose the format you want, 'mb'/'kb'/'bytes' default if bytes!
*@param integer $round set how much numbers you want after Zero, default is 3
*@return double amount of memory your script consume
*/
public function showMemory($string ='bytes', $round=3){
$result=null;
switch($string){
case 'mb': $result = round(memory_get_usage()/1048576,$round);
break;
case 'kb': $result = round(memory_get_usage()/1024, $round);
break;
default: $result = memory_get_usage();
break;
}
return $result;
}
public function __toString(){
return $this->showTimer();
}
}
@arieh
Copy link
Author

arieh commented Dec 11, 2010

usage example:
startTimer(); echo $bm->showTimer(); echo $bm->showTimer(); echo (string)$bm; ?>
notice also that until you recall startTimer, you can get multiple endtimes compared to it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment