Accurate Way to Measure Execution Times of PHP Scripts

Accurate way to measure execution times of php scripts

You can use the microtime function for this. From the documentation:

microtime — Return current Unix timestamp with microseconds



If get_as_float is set to TRUE, then microtime() returns a float, which represents the current time in seconds since the Unix epoch accurate to the nearest microsecond.

Example usage:

$start = microtime(true);
while (...) {

}
$time_elapsed_secs = microtime(true) - $start;

Scripts execution time

You can do the calculation in a function and register that function for execution on shutdown with register_shutdown_function.

$time_start = microtime(true);

register_shutdown_function(function() use ($time_start) {
$time_end= microtime(true);
$dif = $time_end-$time_start;
echo "Script ran for $dif seconds\n";
});

// do a lot of work and die() suddenly and somewhere

Tracking the script execution time in PHP

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.

microtime(true) - Trouble measuring PHP script execution time

Calculations (with the parameter get_as_float as true) will give you results in seconds, according to PHP documentation.

By default, microtime() returns a string in the form "msec sec", where sec is the number of seconds since the Unix epoch (0:00:00 January 1,1970 GMT), and msec measures microseconds that have elapsed since sec and is also expressed in seconds.

If get_as_float is set to TRUE, then microtime() returns a float, which represents the current time in seconds since the Unix epoch accurate to the nearest microsecond.

For full text refer here http://php.net/manual/en/function.microtime.php

On top of that your OS is doing lots of things in between.

Is any way that some method work each 1 hour in php?

You can do this in two ways:

First one, is to create a simple script that does that, and executing it with a cron job. Plain and simple.

The other one is to create a long running script. The idea is that you create a loop, and inside that loop you call the timeout, and then execute your function. You can use reactphp/event-loop for implementing this very easily if you are using composer. Just check out the EventLoopInterface::addPeriodicTimer method.

Keep in mind that you have to keep that long running process alive with some tooling like systemd or supervisord.



Related Topics



Leave a reply



Submit