5
private $headerWritten = false;
7
private $started = false;
17
public function __construct($name) {
21
private function getResourceUsage() {
22
$rusage = getrusage();
23
$time = $rusage['ru_utime.tv_sec'] * 1000000 + $rusage['ru_utime.tv_usec'];
24
$time += $rusage['ru_stime.tv_sec'] * 1000000 + $rusage['ru_stime.tv_usec'];
29
public function start() {
31
throw new RuntimeException("Already started.");
33
$this->startTime = microtime(true);
34
$this->stopTime = $this->startTime;
36
$rusage = getrusage();
38
$this->startUsage = $this->getResourceUsage();
39
$this->stopUsage = $this->startUsage;
40
$this->started = true;
43
public function stop($i = 1) {
44
if (!$this->started) {
45
throw new RuntimeException("Not started.");
48
$this->stopTime = microtime(true);
49
$this->stopUsage = $this->getResourceUsage();
51
$this->iterations = (int)$i;
52
$this->started = false;
55
public function writeHeader() {
56
$header = implode("\t", array(
57
'name', 'start time', 'iterations', 'duration', 'rusage',
60
$this->headerWritten = true;
63
public function write() {
68
if (!$this->headerWritten) {
72
printf("%s\t%.6f\t%d\t%.8f\t%.6f\n",
76
$this->stopTime - $this->startTime,
77
$this->stopUsage - $this->startUsage);