forked from phadej/igbinary
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathbench.php
More file actions
83 lines (65 loc) · 1.68 KB
/
Copy pathbench.php
File metadata and controls
83 lines (65 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'stderr');
class Bench {
private $name;
private $headerWritten = false;
private $started = false;
private $startTime;
private $stopTime;
private $startUsage;
private $stopUsage;
private $iterations;
public function __construct($name) {
$this->name = $name;
}
private function getResourceUsage() {
$rusage = getrusage();
$time = $rusage['ru_utime.tv_sec'] * 1000000 + $rusage['ru_utime.tv_usec'];
$time += $rusage['ru_stime.tv_sec'] * 1000000 + $rusage['ru_stime.tv_usec'];
return $time;
}
public function start() {
if ($this->started) {
throw new RuntimeException("Already started.");
}
$this->startTime = microtime(true);
$this->stopTime = $this->startTime;
$rusage = getrusage();
$this->startUsage = $this->getResourceUsage();
$this->stopUsage = $this->startUsage;
$this->started = true;
}
public function stop($i = 1) {
if (!$this->started) {
throw new RuntimeException("Not started.");
}
$this->stopTime = microtime(true);
$this->stopUsage = $this->getResourceUsage();
$this->iterations = (int)$i;
$this->started = false;
}
public function writeHeader() {
$header = implode("\t", array(
'name', 'start time', 'iterations', 'duration', 'rusage', 'current memory'
));
echo $header, "\n";
$this->headerWritten = true;
}
public function write() {
if ($this->started) {
$this->stop();
}
if (!$this->headerWritten) {
$this->writeHeader();
}
printf("%s\t%.6f\t%d\t%.8f\t%.6f\t%dKB\n",
$this->name,
$this->startTime,
$this->iterations,
$this->stopTime - $this->startTime,
$this->stopUsage - $this->startUsage,
memory_get_usage() / 1024
);
}
}