1
|
|
|
<?php |
2
|
|
|
namespace ivol\tests\Helper; |
3
|
|
|
|
4
|
|
|
use ivol\EventDispatcher\AfterExecuteEvent; |
5
|
|
|
use ivol\EventDispatcher\BeforeExecuteEvent; |
6
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
7
|
|
|
|
8
|
|
|
class TimeProfilerSubscriber implements EventSubscriberInterface |
9
|
|
|
{ |
10
|
|
|
/** @var float */ |
11
|
|
|
private $startTime = -1; |
12
|
|
|
/** @var float */ |
13
|
|
|
private $profiledTime = -1; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Returns an array of event names this subscriber wants to listen to. |
17
|
|
|
* |
18
|
|
|
* The array keys are event names and the value can be: |
19
|
|
|
* |
20
|
|
|
* * The method name to call (priority defaults to 0) |
21
|
|
|
* * An array composed of the method name to call and the priority |
22
|
|
|
* * An array of arrays composed of the method names to call and respective |
23
|
|
|
* priorities, or 0 if unset |
24
|
|
|
* |
25
|
|
|
* For instance: |
26
|
|
|
* |
27
|
|
|
* * array('eventName' => 'methodName') |
28
|
|
|
* * array('eventName' => array('methodName', $priority)) |
29
|
|
|
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))) |
30
|
|
|
* |
31
|
|
|
* @return array The event names to listen to |
32
|
|
|
*/ |
33
|
|
|
public static function getSubscribedEvents() |
34
|
|
|
{ |
35
|
|
|
return array( |
36
|
|
|
BeforeExecuteEvent::EVENT_NAME => 'start', |
37
|
|
|
AfterExecuteEvent::EVENT_NAME => 'stop' |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function start() |
42
|
|
|
{ |
43
|
|
|
$this->startTime = microtime(true); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function stop() |
47
|
|
|
{ |
48
|
|
|
$this->profiledTime = microtime(true) - $this->startTime; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getProfiledTimestamp() |
52
|
|
|
{ |
53
|
|
|
if ($this->profiledTime < 0) { |
54
|
|
|
throw new \RuntimeException('Profiling not started yet'); |
55
|
|
|
} |
56
|
|
|
return $this->profiledTime; |
57
|
|
|
} |
58
|
|
|
} |