c - In PHP, how to measure the time a request spent utilizing hard disk? -
is there way measure amount/length of time web request spends doing i/o?
by that, mean actual amount of time current thread (or it's delegation) has spent utilizing hardware.
as measuring cpu time, there's function getrusage()
it:
<?php $a = getrusage(); f(); $b = getrusage(); $sec = ($b['ru_utime.tv_sec'] - $a['ru_utime.tv_sec']) + ($b['ru_stime.tv_sec'] - $a['ru_stime.tv_sec']); echo 'it took ~' . $sec . 's of cpu time.'; function f(){ for($x=0;$x<120000000;++$x){ // busy loop } }
is there such function measuring i/o?
sample usage of said function:
<?php $credits_remaining = 8500; $a = time_spent_on_hard_disk_so_far(); get_file_from_hard_disk($credits_remaining); // limit request $credits_remaining number of seconds $b = time_spent_on_hard_disk_so_far(); $time_spent = $b - $a; echo "time spent on hard disk: ", $time_spent; $credits_remaining -= $time_spent;
(a c solution fine, long can call php without suffering severe overheads.)
in particular scenario, i'd microtime[stamp] before start operation(s), difference microtime[stamp], store results in array metadata explains metric refers to, , store information in data store, somewhere (database, txt file, session, etc.).
it's best (easiest) way track kind of information , have code maintain portability across numerous operating environments.
/** * micro time * * returns current (epoch) time micro seconds. * * @param [in] $elapsed %ref% of difference since last execution * @return | float time micro seconds epoch */ function microtime(&$elapsed = null) { static $previous = null; list($micro, $epoch) = explode(" ", microtime()); $now = round(floatval($epoch.substr($micro, 1)), 3); if ($previous) { $elapsed = round($now - $previous,3); } $previous = $now; return $now; }
Comments
Post a Comment