1 | <?php |
||
12 | class Benchmark implements BenchmarkInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var string Unique identifier |
||
16 | */ |
||
17 | protected $name; |
||
18 | |||
19 | /** |
||
20 | * @var double Starting time |
||
21 | */ |
||
22 | protected $startTime = 0.0; |
||
23 | |||
24 | /** |
||
25 | * @var double Starting memory usage |
||
26 | */ |
||
27 | protected $startMemory = 0.0; |
||
28 | |||
29 | /** |
||
30 | * @var double Ending time |
||
31 | */ |
||
32 | protected $endTime = 0.0; |
||
33 | |||
34 | /** |
||
35 | * @var double Ending memory usage |
||
36 | */ |
||
37 | protected $endMemory = 0.0; |
||
38 | |||
39 | /** |
||
40 | * @var array Custom metadata regarding this benchmark |
||
41 | */ |
||
42 | protected $metadata = []; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $component; |
||
48 | |||
49 | /** |
||
50 | * @param string $name Unique identifier like e.g. Class::Method (\Foobar\MyClass::doSomething) |
||
51 | * @param array $metadata Additional metadata |
||
52 | * @param string $component Name of the component which triggered the benchmark, e.g. "App", "Database" |
||
53 | */ |
||
54 | 4 | public function __construct($name, array $metadata = [], $component = null) |
|
60 | |||
61 | /** |
||
62 | * Start the benchmark |
||
63 | * |
||
64 | * @return void |
||
65 | */ |
||
66 | 1 | public function start() |
|
67 | { |
||
68 | 1 | $this->startTime = (double)microtime(true); |
|
69 | 1 | $this->startMemory = (double)memory_get_usage(); |
|
70 | 1 | } |
|
71 | |||
72 | /** |
||
73 | * Stop the benchmark |
||
74 | * |
||
75 | * @return void |
||
76 | */ |
||
77 | 1 | public function stop() |
|
78 | { |
||
79 | 1 | $this->endTime = (double)microtime(true); |
|
80 | 1 | $this->endMemory = (double)memory_get_usage(); |
|
81 | 1 | } |
|
82 | |||
83 | /** |
||
84 | * @return double Duration in milliseconds |
||
85 | */ |
||
86 | 1 | public function getDuration() |
|
90 | |||
91 | /** |
||
92 | * @return double Timestamp in microtime |
||
93 | */ |
||
94 | 1 | public function getStartTime() |
|
98 | |||
99 | /** |
||
100 | * @return double Timestamp in microtime |
||
101 | */ |
||
102 | 1 | public function getEndTime() |
|
109 | |||
110 | /** |
||
111 | * Memory usage (difference between start and end memory usage) |
||
112 | * |
||
113 | * @return double |
||
114 | */ |
||
115 | 1 | public function getMemoryUsage() |
|
119 | |||
120 | /** |
||
121 | * Memory usage (difference between start and end memory usage) |
||
122 | * |
||
123 | * @return double |
||
124 | */ |
||
125 | 1 | public function getMemoryUsageStart() |
|
129 | |||
130 | /** |
||
131 | * Memory usage (difference between start and end memory usage) |
||
132 | * |
||
133 | * @return double |
||
134 | */ |
||
135 | 1 | public function getMemoryUsageEnd() |
|
142 | |||
143 | /** |
||
144 | * @return string |
||
145 | */ |
||
146 | 1 | public function getName() |
|
150 | |||
151 | /** |
||
152 | * @param string $name |
||
153 | */ |
||
154 | 4 | public function setName($name) |
|
158 | |||
159 | /** |
||
160 | * @return array |
||
161 | */ |
||
162 | 1 | public function getMetadata() |
|
166 | |||
167 | /** |
||
168 | * @param string $key Metadata key to receive |
||
169 | * @return mixed Custom metadata value |
||
170 | */ |
||
171 | 1 | public function getMetadataValue($key = null) |
|
172 | { |
||
173 | 1 | return array_key_exists($key, $this->metadata) ? $this->metadata[$key] : null; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * Add interesting metadata to the benchmark |
||
178 | * |
||
179 | * @param array $metadata Additional metadata |
||
180 | */ |
||
181 | 4 | public function addMetadata(array $metadata) |
|
185 | |||
186 | /** |
||
187 | * @return string |
||
188 | */ |
||
189 | 1 | public function getComponent() |
|
193 | |||
194 | /** |
||
195 | * @param string $component |
||
196 | */ |
||
197 | 4 | public function setComponent($component) |
|
201 | } |
||
202 |