1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author @fabfuel <[email protected]> |
4
|
|
|
* @created 13.11.14, 08:11 |
5
|
|
|
*/ |
6
|
|
|
namespace Fabfuel\Prophiler\Benchmark; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Benchmark |
10
|
|
|
* @package Fabfuel\Prophiler\Benchmark |
11
|
|
|
*/ |
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) |
55
|
|
|
{ |
56
|
4 |
|
$this->setName($name); |
57
|
4 |
|
$this->addMetadata($metadata); |
58
|
4 |
|
$this->setComponent($component); |
59
|
4 |
|
} |
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() |
87
|
|
|
{ |
88
|
1 |
|
return ($this->getEndTime() - $this->getStartTime()) * 1000; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return double Timestamp in microtime |
93
|
|
|
*/ |
94
|
1 |
|
public function getStartTime() |
95
|
|
|
{ |
96
|
1 |
|
return $this->startTime; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return double Timestamp in microtime |
101
|
|
|
*/ |
102
|
1 |
|
public function getEndTime() |
103
|
|
|
{ |
104
|
1 |
|
if ($this->endTime < $this->startTime) { |
105
|
1 |
|
$this->endTime = (double)microtime(true); |
106
|
1 |
|
} |
107
|
1 |
|
return $this->endTime; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Memory usage (difference between start and end memory usage) |
112
|
|
|
* |
113
|
|
|
* @return double |
114
|
|
|
*/ |
115
|
1 |
|
public function getMemoryUsage() |
116
|
|
|
{ |
117
|
1 |
|
return $this->getMemoryUsageEnd() - $this->getMemoryUsageStart(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Memory usage (difference between start and end memory usage) |
122
|
|
|
* |
123
|
|
|
* @return double |
124
|
|
|
*/ |
125
|
1 |
|
public function getMemoryUsageStart() |
126
|
|
|
{ |
127
|
1 |
|
return $this->startMemory; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Memory usage (difference between start and end memory usage) |
132
|
|
|
* |
133
|
|
|
* @return double |
134
|
|
|
*/ |
135
|
1 |
|
public function getMemoryUsageEnd() |
136
|
|
|
{ |
137
|
1 |
|
if ($this->endMemory < $this->startMemory) { |
138
|
1 |
|
$this->endMemory = (double)memory_get_usage(); |
139
|
1 |
|
} |
140
|
1 |
|
return $this->endMemory; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
1 |
|
public function getName() |
147
|
|
|
{ |
148
|
1 |
|
return $this->name; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $name |
153
|
|
|
*/ |
154
|
4 |
|
public function setName($name) |
155
|
|
|
{ |
156
|
4 |
|
$this->name = $name; |
157
|
4 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
1 |
|
public function getMetadata() |
163
|
|
|
{ |
164
|
1 |
|
return $this->metadata; |
165
|
|
|
} |
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) |
182
|
|
|
{ |
183
|
4 |
|
$this->metadata = array_merge($this->metadata, $metadata); |
184
|
4 |
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
1 |
|
public function getComponent() |
190
|
|
|
{ |
191
|
1 |
|
return $this->component; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $component |
196
|
|
|
*/ |
197
|
4 |
|
public function setComponent($component) |
198
|
|
|
{ |
199
|
4 |
|
$this->component = $component; |
200
|
4 |
|
} |
201
|
|
|
} |
202
|
|
|
|