|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Collector\Console; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\Console\Event\ConsoleCommandEvent; |
|
8
|
|
|
use Symfony\Component\Console\Event\ConsoleErrorEvent; |
|
9
|
|
|
use Symfony\Component\Console\Event\ConsoleTerminateEvent; |
|
10
|
|
|
use Yiisoft\Yii\Console\Event\ApplicationShutdown; |
|
11
|
|
|
use Yiisoft\Yii\Console\Event\ApplicationStartup; |
|
12
|
|
|
use Yiisoft\Yii\Debug\Collector\CollectorTrait; |
|
13
|
|
|
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface; |
|
14
|
|
|
use Yiisoft\Yii\Debug\Collector\TimelineCollector; |
|
15
|
|
|
|
|
16
|
|
|
final class ConsoleAppInfoCollector implements SummaryCollectorInterface |
|
17
|
|
|
{ |
|
18
|
|
|
use CollectorTrait; |
|
19
|
|
|
|
|
20
|
|
|
private float $applicationProcessingTimeStarted = 0; |
|
21
|
|
|
private float $applicationProcessingTimeStopped = 0; |
|
22
|
|
|
private float $requestProcessingTimeStarted = 0; |
|
23
|
|
|
private float $requestProcessingTimeStopped = 0; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct( |
|
26
|
|
|
private readonly TimelineCollector $timelineCollector |
|
27
|
|
|
) { |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function getCollected(): array |
|
31
|
|
|
{ |
|
32
|
|
|
if (!$this->isActive()) { |
|
33
|
|
|
return []; |
|
34
|
|
|
} |
|
35
|
|
|
return [ |
|
36
|
|
|
'applicationProcessingTime' => $this->applicationProcessingTimeStopped - $this->applicationProcessingTimeStarted, |
|
37
|
|
|
'preloadTime' => $this->applicationProcessingTimeStarted - $this->requestProcessingTimeStarted, |
|
38
|
|
|
'applicationEmit' => $this->applicationProcessingTimeStopped - $this->requestProcessingTimeStopped, |
|
39
|
|
|
'requestProcessingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted, |
|
40
|
|
|
'memoryPeakUsage' => memory_get_peak_usage(), |
|
41
|
|
|
'memoryUsage' => memory_get_usage(), |
|
42
|
|
|
]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function collect(object $event): void |
|
46
|
|
|
{ |
|
47
|
|
|
if (!$this->isActive()) { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if ($event instanceof ApplicationStartup) { |
|
52
|
|
|
$this->applicationProcessingTimeStarted = microtime(true); |
|
53
|
|
|
} elseif ($event instanceof ConsoleCommandEvent) { |
|
54
|
|
|
$this->requestProcessingTimeStarted = microtime(true); |
|
55
|
|
|
} elseif ($event instanceof ConsoleErrorEvent) { |
|
56
|
|
|
/** |
|
57
|
|
|
* If we receive this event, then {@see ConsoleCommandEvent} hasn't received and won't. |
|
58
|
|
|
* So {@see requestProcessingTimeStarted} equals to 0 now and better to set it at least with application startup time. |
|
59
|
|
|
*/ |
|
60
|
|
|
$this->requestProcessingTimeStarted = $this->applicationProcessingTimeStarted; |
|
61
|
|
|
$this->requestProcessingTimeStopped = microtime(true); |
|
62
|
|
|
} elseif ($event instanceof ConsoleTerminateEvent) { |
|
63
|
|
|
$this->requestProcessingTimeStopped = microtime(true); |
|
64
|
|
|
} elseif ($event instanceof ApplicationShutdown) { |
|
65
|
|
|
$this->applicationProcessingTimeStopped = microtime(true); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->timelineCollector->collect($this, spl_object_id($event)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getSummary(): array |
|
72
|
|
|
{ |
|
73
|
|
|
if (!$this->isActive()) { |
|
74
|
|
|
return []; |
|
75
|
|
|
} |
|
76
|
|
|
return [ |
|
77
|
|
|
'console' => [ |
|
78
|
|
|
'php' => [ |
|
79
|
|
|
'version' => PHP_VERSION, |
|
80
|
|
|
], |
|
81
|
|
|
'request' => [ |
|
82
|
|
|
'startTime' => $this->requestProcessingTimeStarted, |
|
83
|
|
|
'processingTime' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted, |
|
84
|
|
|
], |
|
85
|
|
|
'memory' => [ |
|
86
|
|
|
'peakUsage' => memory_get_peak_usage(), |
|
87
|
|
|
], |
|
88
|
|
|
], |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function reset(): void |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
|
|
$this->applicationProcessingTimeStarted = 0; |
|
95
|
|
|
$this->applicationProcessingTimeStopped = 0; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.