~ufirst/phpredis/igbinary

« back to all changes in this revision

Viewing changes to benchmark/bench.php

  • Committer: Michael Ruoss
  • Date: 2015-03-18 14:12:19 UTC
  • Revision ID: git-v1:c591bdc61de714458440ff13454bc51d94ab5f3d
new build info structure

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
 
3
 
class Bench {
4
 
        private $name;
5
 
        private $headerWritten = false;
6
 
 
7
 
        private $started = false;
8
 
 
9
 
        private $startTime;
10
 
        private $stopTime;
11
 
 
12
 
        private $startUsage;
13
 
        private $stopUsage;
14
 
 
15
 
        private $iterations;
16
 
 
17
 
        public function __construct($name) {
18
 
                $this->name = $name;
19
 
        }
20
 
 
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'];
25
 
 
26
 
                return $time;
27
 
        }
28
 
 
29
 
        public function start() {
30
 
                if ($this->started) {
31
 
                        throw new RuntimeException("Already started.");
32
 
                }
33
 
                $this->startTime = microtime(true);
34
 
                $this->stopTime = $this->startTime;
35
 
 
36
 
                $rusage = getrusage();
37
 
 
38
 
                $this->startUsage = $this->getResourceUsage();
39
 
                $this->stopUsage = $this->startUsage;
40
 
                $this->started = true;
41
 
        }
42
 
 
43
 
        public function stop($i = 1) {
44
 
                if (!$this->started) {
45
 
                        throw new RuntimeException("Not started.");
46
 
                }
47
 
 
48
 
                $this->stopTime = microtime(true);
49
 
                $this->stopUsage = $this->getResourceUsage();
50
 
 
51
 
                $this->iterations = (int)$i;
52
 
                $this->started = false;
53
 
        }
54
 
 
55
 
        public function writeHeader() {
56
 
                $header = implode("\t", array(
57
 
                        'name', 'start time', 'iterations', 'duration', 'rusage',
58
 
                ));
59
 
                echo $header, "\n";
60
 
                $this->headerWritten = true;
61
 
        }
62
 
 
63
 
        public function write() {
64
 
                if ($this->started) {
65
 
                        $this->stop();
66
 
                }
67
 
 
68
 
                if (!$this->headerWritten) {
69
 
                        $this->writeHeader();
70
 
                }
71
 
 
72
 
                printf("%s\t%.6f\t%d\t%.8f\t%.6f\n",
73
 
                        $this->name,
74
 
                        $this->startTime,
75
 
                        $this->iterations,
76
 
                        $this->stopTime - $this->startTime,
77
 
                        $this->stopUsage - $this->startUsage);
78
 
        }
79
 
}
80
 
 
81