1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
17
|
|
|
* and is licensed under the LGPL. For more information please see |
18
|
|
|
* <http://phing.info>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Phing\Task\Optional; |
22
|
|
|
|
23
|
|
|
use Phing\Dispatch\DispatchTask; |
24
|
|
|
use Phing\Exception\BuildException; |
25
|
|
|
use Phing\Project; |
26
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Stopwatch. |
30
|
|
|
* |
31
|
|
|
* @author Siad Ardroumli <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class StopwatchTask extends DispatchTask |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Name of the timer. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $name = ''; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Category of the timer. |
44
|
|
|
* |
45
|
|
|
* @var string optional |
46
|
|
|
*/ |
47
|
|
|
private $category = ''; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Holds an instance of Stopwatch. |
51
|
|
|
* |
52
|
|
|
* @var Stopwatch |
53
|
|
|
*/ |
54
|
|
|
private static $timer; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Initialize Task. |
58
|
|
|
*/ |
59
|
2 |
|
public function init() |
60
|
|
|
{ |
61
|
2 |
|
if (!class_exists('\\Symfony\\Component\\Stopwatch\\Stopwatch')) { |
62
|
|
|
throw new BuildException('StopwatchTask requires symfony/stopwatch to be installed.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
$this->setAction('start'); |
66
|
2 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Start timer. |
70
|
|
|
*/ |
71
|
1 |
|
public function start(): void |
72
|
|
|
{ |
73
|
1 |
|
$timer = $this->getStopwatchInstance(); |
74
|
1 |
|
$timer->start($this->name, $this->category); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Stop timer. |
79
|
|
|
*/ |
80
|
1 |
|
public function stop(): void |
81
|
|
|
{ |
82
|
1 |
|
$timer = $this->getStopwatchInstance(); |
83
|
1 |
|
$event = $timer->stop($this->name); |
84
|
|
|
|
85
|
1 |
|
foreach ($event->getPeriods() as $period) { |
86
|
1 |
|
$this->log( |
87
|
1 |
|
'Starttime: ' . $period->getStartTime() . ' - Endtime: ' . $period->getEndTime() . |
88
|
1 |
|
' - Duration: ' . $period->getDuration() . ' - Memory: ' . $period->getMemory(), |
89
|
1 |
|
Project::MSG_INFO |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
$this->log('Name: ' . $this->name); |
94
|
1 |
|
$this->log('Category: ' . $event->getCategory()); |
95
|
1 |
|
$this->log('Origin: ' . $event->getOrigin()); |
96
|
1 |
|
$this->log('Start time: ' . $event->getStartTime()); |
97
|
1 |
|
$this->log('End time: ' . $event->getEndTime()); |
98
|
1 |
|
$this->log('Duration: ' . $event->getDuration()); |
99
|
1 |
|
$this->log('Memory: ' . $event->getMemory()); |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Measure lap time. |
104
|
|
|
*/ |
105
|
1 |
|
public function lap(): void |
106
|
|
|
{ |
107
|
1 |
|
$timer = $this->getStopwatchInstance(); |
108
|
1 |
|
$timer->lap($this->name); |
109
|
1 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set the name of the stopwatch. |
113
|
|
|
* |
114
|
|
|
* @param string $name the name of the stopwatch timer |
115
|
|
|
*/ |
116
|
2 |
|
public function setName(string $name): void |
117
|
|
|
{ |
118
|
2 |
|
$this->name = $name; |
119
|
2 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Set the category of the stopwatch. |
123
|
|
|
* |
124
|
|
|
* @param string $category |
125
|
|
|
*/ |
126
|
1 |
|
public function setCategory(string $category): void |
127
|
|
|
{ |
128
|
1 |
|
$this->category = $category; |
129
|
1 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* The main entry point. |
133
|
|
|
* |
134
|
|
|
* @throws BuildException |
135
|
|
|
*/ |
136
|
|
|
public function main() |
137
|
|
|
{ |
138
|
|
|
switch ($this->getAction()) { |
139
|
|
|
case 'start': |
140
|
|
|
$this->start(); |
141
|
|
|
|
142
|
|
|
break; |
143
|
|
|
|
144
|
|
|
case 'stop': |
145
|
|
|
$this->stop(); |
146
|
|
|
|
147
|
|
|
break; |
148
|
|
|
|
149
|
|
|
case 'lap': |
150
|
|
|
$this->lap(); |
151
|
|
|
|
152
|
|
|
break; |
153
|
|
|
|
154
|
|
|
default: |
155
|
|
|
throw new BuildException('action should be one of start, stop, lap.'); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get the stopwatch instance. |
161
|
|
|
* |
162
|
|
|
* @return Stopwatch |
163
|
|
|
*/ |
164
|
1 |
|
private function getStopwatchInstance(): Stopwatch |
165
|
|
|
{ |
166
|
1 |
|
if (null === self::$timer) { |
167
|
1 |
|
self::$timer = new Stopwatch(); |
168
|
|
|
} |
169
|
|
|
|
170
|
1 |
|
return self::$timer; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|