1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Collector; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Profiler\ProfilerInterface; |
8
|
|
|
use Yiisoft\Yii\Web\Event\AfterEmit; |
9
|
|
|
use Yiisoft\Yii\Web\Event\AfterRequest; |
10
|
|
|
use Yiisoft\Yii\Web\Event\BeforeRequest; |
11
|
|
|
use function is_object; |
12
|
|
|
|
13
|
|
|
final class WebAppInfoCollector implements CollectorInterface, IndexCollectorInterface |
14
|
|
|
{ |
15
|
|
|
use CollectorTrait; |
16
|
|
|
|
17
|
|
|
private ProfilerInterface $profiler; |
18
|
|
|
private float $applicationProcessingTimeStarted = 0; |
19
|
|
|
private float $applicationProcessingTimeStopped = 0; |
20
|
|
|
private float $requestProcessingTimeStarted = 0; |
21
|
1 |
|
private float $requestProcessingTimeStopped = 0; |
22
|
|
|
private float $routeMatchTime = 0; |
23
|
|
|
|
24
|
1 |
|
public function __construct(ProfilerInterface $profiler) |
25
|
1 |
|
{ |
26
|
1 |
|
$this->profiler = $profiler; |
27
|
1 |
|
} |
28
|
1 |
|
public function getCollected(): array |
29
|
1 |
|
{ |
30
|
|
|
return [ |
31
|
|
|
'application_processing_time' => $this->applicationProcessingTimeStopped - $this->applicationProcessingTimeStarted, |
32
|
|
|
'application_preload' => $this->requestProcessingTimeStarted - $this->applicationProcessingTimeStarted, |
33
|
1 |
|
'request_processing_time' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted, |
34
|
|
|
'application_emit' => $this->applicationProcessingTimeStopped - $this->requestProcessingTimeStopped, |
35
|
1 |
|
'memory_peak_usage' => memory_get_peak_usage(), |
36
|
|
|
'memory_usage' => memory_get_usage(), |
37
|
|
|
]; |
38
|
|
|
} |
39
|
1 |
|
|
40
|
1 |
|
public function collect(object $event): void |
41
|
1 |
|
{ |
42
|
1 |
|
if (!is_object($event) || !$this->isActive()) { |
43
|
1 |
|
return; |
44
|
|
|
} |
45
|
1 |
|
|
46
|
1 |
|
if ($event instanceof BeforeRequest) { |
47
|
|
|
$this->requestProcessingTimeStarted = microtime(true); |
48
|
|
|
$this->applicationProcessingTimeStarted = $event->getRequest()->getAttribute( |
49
|
|
|
'applicationStartTime', |
50
|
1 |
|
$this->requestProcessingTimeStarted |
51
|
|
|
); |
52
|
1 |
|
} elseif ($event instanceof AfterRequest) { |
53
|
|
|
$this->requestProcessingTimeStopped = microtime(true); |
54
|
|
|
[$message] = $this->profiler->findMessages('Matching route'); |
|
|
|
|
55
|
1 |
|
if ($message !== null) { |
56
|
1 |
|
$this->routeMatchTime = $message->context('duration', 0); |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
} elseif ($event instanceof AfterEmit) { |
60
|
|
|
$this->applicationProcessingTimeStopped = microtime(true); |
61
|
1 |
|
} |
62
|
|
|
} |
63
|
1 |
|
|
64
|
1 |
|
public function getIndexData(): array |
65
|
1 |
|
{ |
66
|
1 |
|
return [ |
67
|
1 |
|
'time' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted, |
68
|
|
|
'memory' => memory_get_peak_usage(), |
69
|
|
|
'timestamp' => $this->requestProcessingTimeStarted, |
70
|
|
|
'routeTime' => $this->routeMatchTime, |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function reset(): void |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$this->applicationProcessingTimeStarted = 0; |
77
|
|
|
$this->applicationProcessingTimeStopped = 0; |
78
|
|
|
$this->requestProcessingTimeStarted = 0; |
79
|
|
|
$this->requestProcessingTimeStopped = 0; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.