PHP Classes

Benchmark Class

Recommend this page to a friend!

      Benchmark Class  >  All threads  >  Benchmark Class  >  (Un) Subscribe thread alerts  
Subject:Benchmark Class
Summary:I just put some error checking in it.
Messages:11
Author:Vinay Jeurkar
Date:2010-12-09 03:55:47
Update:2010-12-10 12:02:43
 
  1 - 10   11 - 11  

  1. Benchmark Class   Reply   Report abuse  
Picture of Vinay Jeurkar Vinay Jeurkar - 2010-12-09 03:55:59
Hello,

Thanks for posting the benchmark class....
I have added some error checking in it. Because when people will use this with large amount of code they might get some errors.

/* *********************************************** */
<?php
/*Author: David Constantine Kurushin
zend.com/en/store/education/certifi ...
*/
Class Benchmark{

public static $stime=NULL,$etime=NULL;

public static function getm(){
return array_sum(explode(" ",microtime()));
}

public static function st(){//Benchmark::st();
self::$stime = self::getm();
self::$etime = NULL;
}

public static function et(){//Benchmark::et();
if( self::$stime != NULL )
self::$etime = self::getm();
else
echo "Set the start time first!!";
}

public static function result($round=10){//echo Benchmark::result(5);
if( self::$stime != NULL && self::$etime != NULL )
return number_format( (self::$etime - self::$stime) , $round, '.', '');
else
echo "One of the start or end time is not set!!";
}

public static function help(){//Benchmark::help();
echo "
<u><b>Methods list of Benchmark class</b></u><br/>
<b>static function getm();</b><br/>Returns current micro time <br/>
<b>public function st();</b><br/>Starts the timer<br/>
<b>public function et();</b><br/>Stops the timer<br/>
<b>public function result(\$round=10);</b><br/>
Show result, round means how much numbers after dot '.'<br/>
";
}

}

Benchmark::st();
Benchmark::et();
echo " <b>" . Benchmark::result(5) . "</b> sec. ";

// Wrong ways
// 1.
Benchmark::et(); //Set the start time first!!

// 2.
Benchmark::st();
echo " <b>" . Benchmark::result(5) . "</b> sec. "; //One of the start or end time is not set!!

?>

/* ******************************************************** */

  2. Re: Benchmark Class   Reply   Report abuse  
Picture of David Constantine David Constantine - 2010-12-09 12:27:45 - In reply to message 1 from Vinay Jeurkar
Think about it like a stopper!
You push the button to start, and than push to stop, and than see the result...

It is not right to see how much time elapsed if you even not started the timer...

or to talk about end time, where there is no start time

We can throw an Exception in all this cases, but really there is no point to use this Class in other way as in example...

Like in real life -

You first start timer, than stop it and see how much time elapsed =-)

But I will think about your words , maybe I will add some Exception try catch, for people who don't catch what is the point of this class

  3. Re: Benchmark Class   Reply   Report abuse  
Picture of David Constantine David Constantine - 2010-12-09 13:33:43 - In reply to message 1 from Vinay Jeurkar
I updated the class, make sure you donwload the new version, all problems you pointed on have been solved

  4. Re: Benchmark Class   Reply   Report abuse  
Picture of Vinay Jeurkar Vinay Jeurkar - 2010-12-10 01:12:19 - In reply to message 1 from Vinay Jeurkar
hello,

Thank you for considering my request. In the real world, this is a simple stopper concept. But people used make mistakes, maybe noobs like me.

> If someone calls result() when only start time is set, u set endtime there and return the result.

Thats a good use case.

Is there any way to find how much memory php variables or the current execution is holding at any random point while execution of the PHP file?

Regards
- Vinay

  5. Re: Benchmark Class   Reply   Report abuse  
Picture of David Constantine David Constantine - 2010-12-10 06:26:53 - In reply to message 4 from Vinay Jeurkar
There is a way... But I am not sure exactly how it goes...

Suggest you to look on the wordpress script, and see how they show you the memory in mb that the script consume

  6. Re: Benchmark Class   Reply   Report abuse  
Picture of Vinay Jeurkar Vinay Jeurkar - 2010-12-10 07:44:50 - In reply to message 5 from David Constantine
I will look for that and will let u know if i find something.

Thank you

  7. Re: Benchmark Class   Reply   Report abuse  
Picture of David Constantine David Constantine - 2010-12-10 10:15:28 - In reply to message 6 from Vinay Jeurkar
you can use this code

<?php

echo memory_get_usage();

echo '<br />';

$a = str_repeat('hello', 100000);

echo '<br />';

echo memory_get_usage();

echo '<br />';

echo memory_get_peak_usage();

?>

it shows the info in bytes...

I consider to add it to my class, cause it is also part of benchmark and its important

  8. Re: Benchmark Class   Reply   Report abuse  
Picture of Vinay Jeurkar Vinay Jeurkar - 2010-12-10 10:42:43 - In reply to message 7 from David Constantine
Thank you for sharing. Looking forward to the updated Benchmark class

  9. Re: Benchmark Class   Reply   Report abuse  
Picture of David Constantine David Constantine - 2010-12-10 10:54:31 - In reply to message 8 from Vinay Jeurkar
It was updated...

added some encapsulation also for the stopTimer
cause user dont need to use it at all
cause when you use showTimer , it first stops and than show...

  10. Re: Benchmark Class   Reply   Report abuse  
Picture of Vinay Jeurkar Vinay Jeurkar - 2010-12-10 11:53:26 - In reply to message 9 from David Constantine

/*******************************/

case 'mb': $result = round(memory_get_usage()/1048576.2,$round);

case 'kb': $result = round(memory_get_usage()/1024.2, $round);

/*******************************/

I know 1024 bytes makes 1 KB and 1048576 bytes makes 1 MB.

I didnt understand why you used '.2' in the end of the values '1048576.2' and '1024.2'.


 
  1 - 10   11 - 11