| 1 |
<?php |
| 2 |
/* Usage... |
| 3 |
if(true === WP_DEBUG){ |
| 4 |
require_once(plugin_dir_path(__DIR__).'/assets/class-execution-time.php'); |
| 5 |
$executionTime = new ExecutionTime('cf7_shortcode_request'); |
| 6 |
$executionTime->Start(); |
| 7 |
} |
| 8 |
//code |
| 9 |
if(true === WP_DEBUG){ |
| 10 |
$executionTime->End(); |
| 11 |
debug_msg($executionTime->__toString()); |
| 12 |
} |
| 13 |
|
| 14 |
|
| 15 |
*/ |
| 16 |
class ExecutionTime{ |
| 17 |
private $startTime; |
| 18 |
private $endTime; |
| 19 |
private $fn; |
| 20 |
public function __construct($fn_name){ |
| 21 |
$this->fn = $fn_name; |
| 22 |
} |
| 23 |
public function Start(){ |
| 24 |
$this->startTime = getrusage(); |
| 25 |
} |
| 26 |
|
| 27 |
public function End(){ |
| 28 |
$this->endTime = getrusage(); |
| 29 |
} |
| 30 |
|
| 31 |
private function runTime($ru, $rus, $index) { |
| 32 |
return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000)) |
| 33 |
- ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000)); |
| 34 |
} |
| 35 |
|
| 36 |
public function __toString(){ |
| 37 |
return $this->fn." used " . $this->runTime($this->endTime, $this->startTime, "utime") . |
| 38 |
" ms for its computations\nIt spent " . $this->runTime($this->endTime, $this->startTime, "stime") . |
| 39 |
" ms in system calls\n"; |
| 40 |
} |
| 41 |
} |
| 42 |
|