PHP code execution time - php

How to find my code execution time?
I am using the below snippet.
I am not happy with that?
list ($msec, $sec) = explode(' ', microtime());
$microtime = (float)$msec + (float)$sec;

I have created this simple class for that:
class timer
{
private $start_time = NULL;
private $end_time = NULL;
private function getmicrotime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function start()
{
$this->start_time = $this->getmicrotime();
}
function stop()
{
$this->end_time = $this->getmicrotime();
}
function result()
{
if (is_null($this->start_time))
{
exit('Timer: start method not called !');
return false;
}
else if (is_null($this->end_time))
{
exit('Timer: stop method not called !');
return false;
}
return round(($this->end_time - $this->start_time), 4);
}
# an alias of result function
function time()
{
$this->result();
}
}
To time scripts, you can put it to use like:
$timer = new timer();
$timer->start();
// your code now
$timer->stop();
// show result now
echo $timer->result();

I'm using following class to determine time elapsed:
class StopWatch {
private static $total;
public static function start() {
self::$total = microtime(true);
}
public static function elapsed() {
return microtime(true) - self::$total;
}
}
You just have to call StopWatch::start at the beginning and StopWatch::elapsed whenever you want to know how much time elapsed from the start.

You can use the parameter of microtime :
$microtime = microtime(true);

If you want to profile your script, use XDebug + {K,Win}Cachegrind, these are free tools that provide a visual map of your code's execution.

If you want the actual processor timings:
$rUsage = getrusage();
echo 'User time = '.sprintf('%.4f',($rUsage['ru_utime.tv_sec'] * 1e6 + $rUsage['ru_utime.tv_usec']) / 1e6).' seconds';
echo 'system time = '.sprintf('%.4f',($rUsage['ru_stime.tv_sec'] * 1e6 + $rUsage['ru_stime.tv_usec']) / 1e6).' seconds';

In LINUX:
For one simple code:
time php -r "echo 'hello';"
output:
hello
real 0m0.095s
user 0m0.033s
sys 0m0.055s
For a php file:
time php hello.php
output:
hello
real 0m0.073s
user 0m0.025s
sys 0m0.047s

Thanks for sharing your information.
The below one also LITTLE BIT near of my solution.
function microtime_float() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();
SOURCE CODE
$time_end = microtime_float();
$time = round($time_end - $time_start, 4);
echo "Last uncached content render took $time seconds";

If running Unix, take a look at the time command:

<?php
// Randomize sleeping time
usleep(mt_rand(100, 10000));
// As of PHP 5.4.0, REQUEST_TIME_FLOAT is available in the $_SERVER superglobal array.
// It contains the timestamp of the start of the request with microsecond precision.
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
echo "Did nothing in $time seconds\n";
?>

Related

PHPRedis and Locking

I got the following two functions that I use to lock a Redis key. I am trying to prevent concurrency execution of a block of code using Redis. So what I do is use the following functions in such a way that they prevent the execution of the same code by different threads.
lockRedisKey("ABC");
CODE THAT I DON'T WANT TO RUN CONCURRENTLY!
unlockRedisKey("ABC");
Unfortunately, it doesn't seem to work and causes an infinitely loop at lockRedisKey() until exit_time is reached. What could be wrong?
static public function lockRedisKey($key, $value = "true") {
$redis = RedisClient::getInstance();
$time = microtime(true);
$exit_time = $time + 10;
$sleep = 10000;
do {
// Lock Redis with PX and NX
$lock = $redis->setnx("lock:" . $key, $value);
if ($lock == 1) {
$redis->expire("lock:" . $key, "10");
return true;
}
usleep($sleep);
} while (microtime(true) < $exit_time);
return false;
}
static public function unlockRedisKey($key) {
$redis = RedisClient::getInstance();
$redis->del("lock:" . $key);
}
I'm aware that I might be facing deadlocks, so I decided to use transactions, but I continue to face the issue.
static public function lockRedisKey($key, $value = "true") {
$redis = RedisClient::getInstance();
$time = microtime(true);
$exit_time = $time + 10;
$sleep = 10000;
do {
// Lock Redis with PX and NX
$redis->multi();
$redis->set('lock:' . $key, $value, array('nx', 'ex' => 10));
$ret = $redis->exec();
if ($ret[0] == true) {
return true;
}
usleep($sleep);
} while (microtime(true) < $exit_time);
return false;
}
static public function unlockRedisKey($key) {
$redis = RedisClient::getInstance();
$redis->multi();
$redis->del("lock:" . $key);
$redis->exec();
}
The locking is working fine. It's just the code between the locking that is crashing and causing the lock not to be released :-)
First of all, PHP isn't async script language as JS f.e. You haven't any control on the script execution ordering, so it means you can't run code that below lockRedisKey() call in concurent way.
I assume you're not completely understand what usleep function is actually doing, it's just delay process and not postpone it, so it just blocks process for $sleep time and after this continue script execution. So you're not performing concurrent write by calling your script.

Versatile format for date validations?

I am looking for a versatile method for validating dates.
What I want, is some way of achieving validation rules such as:
First monday of month
monday or friday
Fourth day of month
Is there anything that can handle such requirements? Or does anyone have any pointers as to how i could achieve this?
Edit: I appreciate the answers, but heres what i ended up using for now:
Thanks for all the answers. What i actually ended up using (At least till i find the perfect solution) is roughly this function:
function isDateValid($rule,$date){
$DATE = (int) $date;
/* Make it safe for eval */
preg_match_all('/:\w:|&&|\|\||<|=|>|!|\(|\)|\d+/i',$rule,$filteredRule);
$rule = implode('',$filteredRule[0]);
//replace rule notations
$f = function($str){
return "date('$str[1]',\$DATE)";
};
//Or if php < 5.3: $f = create_function('$str', 'return "date(\'$str[1]\',\$DATE)";');
$rule = preg_replace_callback('/:(\w):/i', $f, $rule);
//Evaluate expression
return #eval("return $rule;");
}
//Example of usage testing first of month:
var_dump( isDateValid(':d:==1',time()) );
This lets me handle all the requirements i have in, well PHP code. The people who use this is very limited (<5 persons) and most of them have some degree of php experience. Now i can make rules such as:
first monday of month : :w:==1 && :d: <= 7
monday or friday : :w:==1 || :w:==5
Fourth day of month : :d: == 4
I would appreciate an even better more flexible solution or feedback regarding security (Can this be injected with mallicious php code?)
I would define a set of rules something like:
$ordinals = array("First", "Second"...);
$days = array("Monday", "Tuesday"...);
$rules = array(
array("%1 %2 of month", $ordinals, $days),
array("%1 or %1", $days),
etc
)
Then, do a foreach(rule) and generate all possible allowed strings
$alloweds = array();
foreach($rules as $rule){
$str = $rule[0];
for($i = 1; $i < sizeof($rule); $i++){
for($a = 0; $a < sizeof($rule[$i]); $a++){
$alloweds[] = str_replace($str, "%$i", $rule[$i][$a]);
}
}
}
Then do a foreach(allowed string) compare to what we have
foreach($alloweds as $allowed){
if(preg_match($allowed, $input){
//its valid
}
}
you could set up your rules to be quite complicated - however its worth noting that the more complex they are the longer this will take - in its current form its clearly an exponential time algorithm, but its a starting point.
Not the most versatile solution, but this actually does EXACTLY what you want to do:
Class:
<?php
class validate {
private static $month;
private static $year;
private static $day;
private static $day_literal;
function __construct() {
self::$year = date("Y");
self::$month = date("m");
self::$day = date("d");
self::$day_literal = date("l");
}
public function firstInMonth($required,$day,$month,$year) {
$firstday;
for ($i = 1; $i < 31; $i++) {
if (checkdate($month,$i,$year)) {
if (trim(strtolower(date("l",mktime(0,0,0,$month,$i,$year)))) == trim(strtolower(trim($required)))) {
$firstday = $i;
break;
}
else {
continue;
}
}
}
if (strtotime(date("Y-m-d",mktime(0,0,0,$month,$i,$year))) == strtotime(date("Y-m-d",mktime(0,0,0,$month,$day,$year)))) {
return "{$year}/{$month}/{$day} is the first {$required} of the month.";
}
else {
return "Nope.";
}
}
public function checkDayLiteral($literal,$day,$month,$year) {
if (trim(strtolower(date("l",mktime(0,0,0,$month,$day,$year)))) == trim(strtolower($literal))) {
return true;
}
else {
return false;
}
}
public function dayPosition($day,$month,$year) {
return date("jS",mktime(0,0,0,$month,$day,$year));
}
}
?>
I don't know what's the purpose of your checking, so I've also implemented the construct method to compile the "today" values, which are NOT used in the code. If you need them, feel free to call them using self::
How to use:
<?php
$validate = new validate;
echo $validate->firstInMonth("Monday",2,9,2013);
echo "<br />";
echo $validate->checkDayLiteral("Monday",2,9,2013);
echo "<br />";
echo $validate->dayPosition(2,9,2013);
?>
Change the class according to WHAT you need to get (I mean: edit the returns I've set).
In this case, the above code returns:
2013/9/2 is the first Monday of the month.
1
2nd
while this:
echo $validate->firstInMonth("Tuesday",2,9,2013);
echo "<br />";
echo $validate->checkDayLiteral("Wednesday",15,9,2013);
echo "<br />";
echo $validate->dayPosition(24,9,2013);
returns this:
Nope.
24th
Of course, this is not as versatile as whatever you want, but this works.
Hope this helps.
If it does not do exactly what you want I'm sorry, just take this as an idea, I think it's pretty clear how you can get whatever information you want from this code.

strtotime function for hours more than 24

Hello I am trying to compare a time during with Hours:Minutes:Seconds format using strtotime function but there is a probleme if Hours is more than 24
PHP Code:
$time_duration_1 = '102:44:18';
$time_duration_2 = '87:42:19';
if(strtotime($time_duration_1) > strtotime($time_duration_2))
{
return true;
}else{
return false;
}
Is there any handy function for that kind of situation? or shall I do the checking manually hard coded?
Thanks in advance
Make a function to calculate your hours, minutes and seconds:
function formatHours($time){
$date = explode(':', $time);
return ($date[0]*60*60)+($date[1]*60)+$date[2];
}
if(formatHours($time_duration_1) > formatHours($time_duration_2)){
// ......
The way I'd solve that would be with mktime:
function getTimeFromString($time){
$time = explode(':', $time);
return mktime($time[0], $time[1], $time[2]);
}
if(getTimeFromString($time_duration_1) > getTimeFromString($time_duration_2)){

English to Time

Does anyone know of a good class / library to convert English representations of time into timestamps?
The goal is to convert natural language phrases such as "ten years from now" and "three weeks" and "in 10 minutes" and working out a best match unix timestamp for them.
I have hacked up some pretty poor and untested code to get going on it, but I am sure there are great parsers out there for calendars and such.
private function timeparse($timestring)
{
$candidate = #strtotime($timestring);
if ($candidate > time()) return $candidate; // Let php have a bash at it
//$thisyear = date("Y");
if (strpos($timestring, "min") !== false) // Context is minutes
{
$nummins = preg_replace("/\D/", "", $timestring);
$candidate = #strtotime("now +$nummins minutes");
return $candidate;
}
if (strpos($timestring, "hou") !== false) // Context is hours
{
$numhours = preg_replace("/\D/", "", $timestring);
$candidate = #strtotime("now +$numhours hours");
return $candidate;
}
if (strpos($timestring, "day") !== false) // Context is days
{
$numdays = preg_replace("/\D/", "", $timestring);
$candidate = #strtotime("now +$numdays days");
return $candidate;
}
if (strpos($timestring, "year") !== false) // Context is years (2 years)
{
$numyears = preg_replace("/\D/", "", $timestring);
$candidate = #strtotime("now +$numyears years");
return $candidate;
}
if (strlen($timestring) < 5) // 10th || 2nd (or probably a number)
{
$day = preg_replace("/\D/", "", $timestring);
if ($day > 0)
{
$month = date("m");
$year = date("y");
return strtotime("$month/$day/$year");
}
else
{
return false;
}
}
return false; // No can do.
}
Use the DateTime class.
e.g.:
$string='four days ago';
$d=date_create($string);
$d->getTimestamp();
ETA:
which you could extend:
class myDateTime extends DateTime {
static $defined_expressions=array(...);
function __construct($expression=NULL) {
if ($exp=$this->translate($expression)) {
parent::__construct($exp);
}
}
function translate($exp) {
//check to see if strtotime errors or not
//if it errors, check if $exp matches a pattern in self::$defined_expressions
return $exp, modified $exp or false
}
}
Sometime ago I had come across http://www.timeapi.org which converts natural language queries into time. It is an API though.
The ruby source code is on github. If need be, I guess you could try to port it to PHP.
Just got a notification from PHPClasses, with one of the runner-ups of the monthly innovation award: Text to Timestamp
You could try that...
Cocoa's and GNUStep's NSDateFormatter are able to handle such time representations. The GNUStep version is open-source.

Tracking the script execution time in PHP

PHP must track the amount of CPU time a particular script has used in order to enforce the max_execution_time limit.
Is there a way to get access to this inside of the script? I'd like to include some logging with my tests about how much CPU was burnt in the actual PHP (the time is not incremented when the script is sitting and waiting for the database).
I am using a Linux box.
If all you need is the wall-clock time, rather than the CPU execution time, then it is simple to calculate:
//place this before any script you want to calculate time
$time_start = microtime(true);
//sample script
for($i=0; $i<1000; $i++){
//do anything
}
$time_end = microtime(true);
//dividing with 60 will give the execution time in minutes otherwise seconds
$execution_time = ($time_end - $time_start)/60;
//execution time of the script
echo '<b>Total Execution Time:</b> '.$execution_time.' Mins';
// if you get weird results, use number_format((float) $execution_time, 10)
Note that this will include the time that PHP is sat waiting for external resources such as disks or databases, which is not used for max_execution_time.
On unixoid systems (and in php 7+ on Windows as well), you can use getrusage, like:
// Script start
$rustart = getrusage();
// Code ...
// Script end
function rutime($ru, $rus, $index) {
return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
- ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
}
$ru = getrusage();
echo "This process used " . rutime($ru, $rustart, "utime") .
" ms for its computations\n";
echo "It spent " . rutime($ru, $rustart, "stime") .
" ms in system calls\n";
Note that you don't need to calculate a difference if you are spawning a php instance for every test.
Shorter version of talal7860's answer
<?php
// At start of script
$time_start = microtime(true);
// Anywhere else in the script
echo 'Total execution time in seconds: ' . (microtime(true) - $time_start);
As pointed out, this is 'wallclock time' not 'cpu time'
<?php
// Randomize sleeping time
usleep(mt_rand(100, 10000));
// As of PHP 5.4.0, REQUEST_TIME_FLOAT is available in the $_SERVER superglobal array.
// It contains the timestamp of the start of the request with microsecond precision.
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
echo "Did nothing in $time seconds\n";
?>
I created an ExecutionTime class out of phihag answer that you can use out of box:
class ExecutionTime
{
private $startTime;
private $endTime;
public function start() {
$this->startTime = getrusage();
}
public function end() {
$this->endTime = getrusage();
}
private function runTime($ru, $rus, $index) {
return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
- ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
}
public function __toString() {
return "This process used " . $this->runTime($this->endTime, $this->startTime, "utime") .
" ms for its computations\nIt spent " . $this->runTime($this->endTime, $this->startTime, "stime") .
" ms in system calls\n";
}
}
Usage:
$executionTime = new ExecutionTime();
$executionTime->start();
// Code
$executionTime->end();
echo $executionTime;
Note: In PHP 5, the getrusage function only works in Unix-oid systems. Since PHP 7, it also works on Windows.
It is going to be prettier if you format the seconds output like:
echo "Process took ". number_format(microtime(true) - $start, 2). " seconds.";
will print
Process took 6.45 seconds.
This is much better than
Process took 6.4518549156189 seconds.
Gringod at developerfusion.com gives this good answer:
<!-- put this at the top of the page -->
<?php
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
;?>
<!-- put other code and html in here -->
<!-- put this code at the bottom of the page -->
<?php
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "This page was created in ".$totaltime." seconds";
;?>
From (http://www.developerfusion.com/code/2058/determine-execution-time-in-php/)
To show minutes and seconds you can use:
$startTime = microtime(true);
$endTime = microtime(true);
$diff = round($endTime - $startTime);
$minutes = floor($diff / 60); // Only minutes
$seconds = $diff % 60; // Remaining seconds, using modulo operator
echo "script execution time: minutes:$minutes, seconds:$seconds"; // Value in seconds
The cheapest and dirtiest way to do it is simply make microtime() calls at places in your code you want to benchmark. Do it right before and right after database queries and it's simple to remove those durations from the rest of your script execution time.
A hint: your PHP execution time is rarely going to be the thing that makes your script timeout. If a script times out it's almost always going to be a call to an external resource.
PHP microtime documentation:
http://us.php.net/microtime
I think you should look at xdebug. The profiling options will give you a head start toward knowing many process related items.
http://www.xdebug.org/
$_SERVER['REQUEST_TIME']
check out that too. i.e.
...
// your codes running
...
echo (time() - $_SERVER['REQUEST_TIME']);
when there is closure functionality in PHP, why not we get benefit out of it.
function startTime(){
$startTime = microtime(true);
return function () use ($startTime){
return microtime(true) - $startTime;
};
}
Now with the help of the above function, we can track time like this
$stopTime = startTime();
//some code block or line
$elapsedTime = $stopTime();
Every call to startTime function will initiate a separate time tracker. So you can initiate as many as you want and can stop them wherever you want them.
Small script that print, centered in bottom of the page, the script execution that started at server call with microsecond precision.
So as not to distort the result and to be 100% compatible with content in page, I used, to write the result on the page, a browser-side native javascript snippet.
//Uncomment the line below to test with 2 seconds
//usleep(2000000);
$prec = 5; // numbers after comma
$time = number_format(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'], $prec, '.', '');
echo "<script>
if(!tI) {
var tI=document.createElement('div');
tI.style.fontSize='8px';
tI.style.marginBottom='5px';
tI.style.position='absolute';
tI.style.bottom='0px';
tI.style.textAlign='center';
tI.style.width='98%';
document.body.appendChild(tI);
}
tI.innerHTML='$time';
</script>";
Another approach is to make the snippet as small as possible, and style it with a class in your stylesheet.
Replace the echo ...; part with the following:
echo "<script>if(!tI){var tI=document.createElement('div');tI.className='ldtme';document.body.appendChild(tI);}tI.innerHTML='$time';</script>";
In your CSS create and fill the .ldtme{...} class.
I wrote a function that check remaining execution time.
Warning: Execution time counting is different on Windows and on Linux platform.
/**
* Check if more that `$miliseconds` ms remains
* to error `PHP Fatal error: Maximum execution time exceeded`
*
* #param int $miliseconds
* #return bool
*/
function isRemainingMaxExecutionTimeBiggerThan($miliseconds = 5000) {
$max_execution_time = ini_get('max_execution_time');
if ($max_execution_time === 0) {
// No script time limitation
return true;
}
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// On Windows: The real time is measured.
$spendMiliseconds = (microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]) * 1000;
} else {
// On Linux: Any time spent on activity that happens outside the execution
// of the script such as system calls using system(), stream operations
// database queries, etc. is not included.
// #see http://php.net/manual/en/function.set-time-limit.php
$resourceUsages = getrusage();
$spendMiliseconds = $resourceUsages['ru_utime.tv_sec'] * 1000 + $resourceUsages['ru_utime.tv_usec'] / 1000;
}
$remainingMiliseconds = $max_execution_time * 1000 - $spendMiliseconds;
return ($remainingMiliseconds >= $miliseconds);
}
Using:
while (true) {
// so something
if (!isRemainingMaxExecutionTimeBiggerThan(5000)) {
// Time to die.
// Safely close DB and done the iteration.
}
}
Further expanding on Hamid's answer, I wrote a helper class that can be started and stopped repeatedly (for profiling inside a loop).
class ExecutionTime
{
private $startTime;
private $endTime;
private $compTime = 0;
private $sysTime = 0;
public function Start() {
$this->startTime = getrusage();
}
public function End() {
$this->endTime = getrusage();
$this->compTime += $this->runTime($this->endTime, $this->startTime, "utime");
$this->systemTime += $this->runTime($this->endTime, $this->startTime, "stime");
}
private function runTime($ru, $rus, $index) {
return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000)) - ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
}
public function __toString() {
return "This process used " . $this->compTime . " ms for its computations\n" . "It spent " . $this->systemTime . " ms in system calls\n";
}
}
You may only want to know the execution time of parts of your script. The most flexible way to time parts or an entire script is to create 3 simple functions (procedural code given here but you could turn it into a class by putting class timer{} around it and making a couple of tweaks). This code works, just copy and paste and run:
$tstart = 0;
$tend = 0;
function timer_starts()
{
global $tstart;
$tstart=microtime(true); ;
}
function timer_ends()
{
global $tend;
$tend=microtime(true); ;
}
function timer_calc()
{
global $tstart,$tend;
return (round($tend - $tstart,2));
}
timer_starts();
file_get_contents('http://google.com');
timer_ends();
print('It took '.timer_calc().' seconds to retrieve the google page');
Just to contribute to this conversation:
what happens if the measurement targets two points A and B in different php files?
what if we need different measurements like time based, code execution duration, external resource access duration?
what if we need to organize our measurements in categories where every one has a different starting point?
As you suspect we need some global variables to be accessed by a class object or a static method: I choose the 2nd approach and here it is:
namespace g3;
class Utils {
public function __construct() {}
public static $UtilsDtStart = [];
public static $UtilsDtStats = [];
public static function dt() {
global $UtilsDtStart, $UtilsDtStats;
$obj = new \stdClass();
$obj->start = function(int $ndx = 0) use (&$UtilsDtStart) {
$UtilsDtStart[$ndx] = \microtime(true) * 1000;
};
$obj->codeStart = function(int $ndx = 0) use (&$UtilsDtStart) {
$use = \getrusage();
$UtilsDtStart[$ndx] = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000);
};
$obj->resourceStart = function(int $ndx = 0) use (&$UtilsDtStart) {
$use = \getrusage();
$UtilsDtStart[$ndx] = $use["ru_stime.tv_usec"] / 1000;
};
$obj->end = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
$t = #$UtilsDtStart[$ndx];
if($t === null)
return false;
$end = \microtime(true) * 1000;
$dt = $end - $t;
$UtilsDtStats[$ndx][] = $dt;
return $dt;
};
$obj->codeEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
$t = #$UtilsDtStart[$ndx];
if($t === null)
return false;
$use = \getrusage();
$dt = ($use["ru_utime.tv_sec"] * 1000) + ($use["ru_utime.tv_usec"] / 1000) - $t;
$UtilsDtStats[$ndx][] = $dt;
return $dt;
};
$obj->resourceEnd = function(int $ndx = 0) use (&$UtilsDtStart, &$UtilsDtStats) {
$t = #$UtilsDtStart[$ndx];
if($t === null)
return false;
$use = \getrusage();
$dt = ($use["ru_stime.tv_usec"] / 1000) - $t;
$UtilsDtStats[$ndx][] = $dt;
return $dt;
};
$obj->stats = function(int $ndx = 0) use (&$UtilsDtStats) {
$s = #$UtilsDtStats[$ndx];
if($s !== null)
$s = \array_slice($s, 0);
else
$s = false;
return $s;
};
$obj->statsLength = function() use (&$UtilsDtStats) {
return \count($UtilsDtStats);
};
return $obj;
}
}
Now all you have is to call the method that belongs to the specific category with the index that denotes it's unique group:
File A
------
\call_user_func_array(\g3\Utils::dt()->start, [0]); // point A
...
File B
------
$dt = \call_user_func_array(\g3\Utils::dt()->end, [0]); // point B
Value $dt contains the milliseconds of wall clock duration between points A and B.
To estimate the time took for php code to run:
File A
------
\call_user_func_array(\g3\Utils::dt()->codeStart, [1]); // point A
...
File B
------
$dt = \call_user_func_array(\g3\Utils::dt()->codeEnd, [1]); // point B
Notice how we changed the index that we pass at the methods.
The code is based on the closure effect that happens when we return an object/function from a function (see that \g3\Utils::dt() repeated at the examples).
I tested with php unit and between different test methods at the same test file it behaves fine so far!
Hope that helps someone!
As an alternative you can just put this line in your code blocks and check php logs, for really slow functions it's pretty useful:
trigger_error("Task done at ". strftime('%H:%m:%S', time()), E_USER_NOTICE);
For serious debugging use XDebug + Cachegrind, see https://blog.nexcess.net/2011/01/29/diagnosing-slow-php-execution-with-xdebug-and-kcachegrind/
There are several way of doing this, listed here. But each have their own pro's and con's. And (in my opinion) the readability of all longer answers are terrible.
So I decided to put it all together in one answer, that is easily usable and readable.
Usage
$start = get_timers();
for( $i = 0; $i < 100000; $i++ ){
// Code to check
}
$end = get_timers();
display_timer_statistics( $start, $end );
Function definitions
function display_timer_statistics( $start_timers, $end_timers ){
// Settings
$key_width = '100px';
$decimals = 4;
$decimals_wallclock = $decimals;
$decimals_request_time_float = $decimals;
// Variables
$start_resource_usage_timer = $start_timers[0];
$start_wallclock = $start_timers[1];
$end_resource_usage_timer = $end_timers[0];
$end_wallclock = $end_timers[1];
// # User time
// Add seconds and microseconds for the start/end, and subtract from another
$end_user_time_seconds = $end_resource_usage_timer["ru_utime.tv_sec"]*1000;
$end_user_time_microseconds = intval($end_resource_usage_timer["ru_utime.tv_usec"]/1000);
$start_user_time_seconds = $start_resource_usage_timer["ru_utime.tv_sec"]*1000;
$start_user_time_microseconds = intval($start_resource_usage_timer["ru_utime.tv_usec"]/1000);
$total_user_time = ($end_user_time_seconds + $end_user_time_microseconds) - ($start_user_time_seconds + $start_user_time_microseconds);
// # System time
// Add seconds and microseconds for the start/end, and subtract from another
$end_system_time_seconds = $end_resource_usage_timer["ru_stime.tv_sec"]*1000;
$end_system_time_microseconds = intval($end_resource_usage_timer["ru_stime.tv_usec"]/1000);
$start_system_time_seconds = $start_resource_usage_timer["ru_stime.tv_sec"]*1000;
$start_system_time_microseconds = intval($start_resource_usage_timer["ru_stime.tv_usec"]/1000);
$total_system_time = ($end_system_time_seconds + $end_system_time_microseconds) - ($start_system_time_seconds + $start_system_time_microseconds);
// Wallclock
$total_wallclock_time = number_format( ( $end_wallclock - $start_wallclock), $decimals_wallclock );
// Server request_time_float
$request_time_float = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
$request_time_float = number_format( $request_time_float, $decimals_request_time_float );
// Print
$span_start = "<span style='width: $key_width; display: inline-block;'>";
$span_end = "</span>";
$output = "# RUNTIME AND TIMERS " . PHP_EOL ;
$output .= PHP_EOL;
$output .= $span_start . $total_user_time . $span_end . " User time (utime)" . PHP_EOL;
$output .= $span_start . $total_system_time . $span_end . " System time (stime)" . PHP_EOL;
$output .= PHP_EOL;
$output .= $span_start . $total_wallclock_time . $span_end . " Wallclock" . PHP_EOL;
$output .= PHP_EOL;
$output .= $span_start . $request_time_float . $span_end . " REQUEST_TIME_FLOAT" . PHP_EOL . PHP_EOL . PHP_EOL;
echo nl2br( $output );
}
function get_timers(){
return [ getrusage(), microtime( true ) ];
}
Glossary
All gotten from PHP docs for getrusage
Wallclock = How long it takes
ru = Resource usage
utime = User time used
stime = System time used
tv_sec = In seconds.
tv_usec = In microseconds.
tv = ?? Dunno
return microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];

Categories