Passed
Push — master ( eaec8c...7391a5 )
by Rustam
04:57
created

WebAppInfoCollector::getCollected()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 1
rs 10
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
    private float $requestProcessingTimeStopped = 0;
22
    private float $routeMatchTime = 0;
23
24 1
    public function __construct(ProfilerInterface $profiler)
25
    {
26 1
        $this->profiler = $profiler;
27 1
    }
28
29 1
    public function getCollected(): array
30
    {
31
        return [
32 1
            'application_processing_time' => $this->applicationProcessingTimeStopped - $this->applicationProcessingTimeStarted,
33 1
            'application_preload' => $this->requestProcessingTimeStarted - $this->applicationProcessingTimeStarted,
34 1
            'request_processing_time' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
35 1
            'application_emit' => $this->applicationProcessingTimeStopped - $this->requestProcessingTimeStopped,
36 1
            'memory_peak_usage' => memory_get_peak_usage(),
37 1
            'memory_usage' => memory_get_usage(),
38
        ];
39
    }
40
41 1
    public function collect(object $event): void
42
    {
43 1
        if (!is_object($event) || !$this->isActive()) {
44
            return;
45
        }
46
47 1
        if ($event instanceof BeforeRequest) {
48 1
            $this->requestProcessingTimeStarted = microtime(true);
49 1
            $this->applicationProcessingTimeStarted = $event->getRequest()->getAttribute(
50 1
                'applicationStartTime',
51 1
                $this->requestProcessingTimeStarted
52
            );
53 1
        } elseif ($event instanceof AfterRequest) {
54 1
            $this->requestProcessingTimeStopped = microtime(true);
55 1
            $message = $this->profiler->findMessages('Matching route')[0] ?? null;
56 1
            if ($message !== null) {
57 1
                $this->routeMatchTime = $message->context('duration', 0);
58
            }
59
        } elseif ($event instanceof AfterEmit) {
60
            $this->applicationProcessingTimeStopped = microtime(true);
61
        }
62 1
    }
63
64 1
    public function getIndexData(): array
65
    {
66
        return [
67 1
            'time' => $this->requestProcessingTimeStopped - $this->requestProcessingTimeStarted,
68 1
            'memory' => memory_get_peak_usage(),
69 1
            'timestamp' => $this->requestProcessingTimeStarted,
70 1
            'routeTime' => $this->routeMatchTime,
71
        ];
72
    }
73
74 1
    private function reset(): void
0 ignored issues
show
Unused Code introduced by
The method reset() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
75
    {
76 1
        $this->applicationProcessingTimeStarted = 0;
77 1
        $this->applicationProcessingTimeStopped = 0;
78 1
        $this->requestProcessingTimeStarted = 0;
79 1
        $this->requestProcessingTimeStopped = 0;
80 1
    }
81
}
82