php exec how to calculate time?
I'm trying to implement some kind of 'multiprocessing' in php for my task. The task is to check the status of every device in our network.
For that i decided to use looped exec
and it works. But i don't know whether it works fine or not:
$command = "php scan_part.php $i > null &";
exec($command);
This calls scan_part.php
as many times as i need, but the question is: how can i calculate time needed for all of my scan_part.php
's to be executed?
Please, help me, i'm s开发者_C百科tuck!
Use proc_open
to launch your script instead of exec
. Proc_open
lets you wait until a process is done through proc_close
, which waits until the termination of the program.
$starttime = microtime(true);
$processes = array();
// stdin, stdout, stderr- take no input, save no output
$descriptors = array(
0 => array("file", "/dev/null", 'r'),
1 => array("file", "/dev/null", 'w'),
2 => array("file", "/dev/null", 'w'));
while ($your_condition)
{
$command = "php scan_part.php $i"; // no pipe redirection, no background mark
$processes[] = proc_open($command, $descriptors, $pipes);
}
// proc_close will block until the program terminates or will return immediately
// if the program has already terminated
foreach ($processes as $process)
proc_close($process);
$endtime = microtime(true);
$delta = $endtime - $starttime;
Consider using the microtime()
function. Example #2 on this page is useful:
http://php.net/manual/en/function.microtime.php
EDIT: This approach will not work, because as pointed out in comments, the process is lauched in the background. I am unable to suggest a better approach.
Does the scan_part.php
script output anything that the calling script expects/accepts as input? If not, you can use /usr/bin/time
to calculate the run time of the scan_part script:
exec('/usr/bin/time /usr/bin/php scan_part.php &');
That returns something like this:
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 4192maxresident)k
0inputs+0outputs (0major+318minor)pagefaults 0swaps
which you can parse for the times. This would let you get execution time without having to modify scan_parts.
On the other hand, if scan_parts doesn't normally produce output, and you can/will modify it, then wrapping the contents of scan_parts with a couple microtime(true)
calls and outputting the difference would be simpler.
$start = microtime(true);
.... do the core of scan_parts here
echo microtime(true) - $start; // output the runtime here
exit();
This gives you a SINGLE time value to return, saving you the parseing overhead of the /usr/bin/time
stuff.
You can't request the time to execute on a normal operating system (it's a system call and handled by the kernel). For this you need a real time os.
But you can calculate it by record for each instance how much time it took to launch. Than calculate the average on that dataset.
microtime can give you the current time. Do this like so:
while(/** list proc */)
{
$starttime = microtime();
proc_open( /** some syntax here */);
$endtime = microtime() - $starttime;
//store time here
}
You may use the microtime(), if you want to benchmark a whole application you may also look into XDebug, it will give you detailed reports on the performance of your application.
For windows the latest version of Wampserver comes packaged with XDebug and webgrind. For linux the installation is also quite simple.
See: http://code.google.com/p/webgrind/
精彩评论