Passed
Push — main ( 28fe2f...804823 )
by mikhail
03:26
created

PerformanceMonitor::start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\Metrics;
6
7
use SavinMikhail\CommentsDensity\DTO\Output\PerformanceMetricsDTO;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...t\PerformanceMetricsDTO was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
use function memory_get_peak_usage;
10
use function microtime;
11
use function round;
12
13
final class PerformanceMonitor
14
{
15
    private float $startTime;
16
    private float $endTime;
17
    private float $peakMemoryUsage;
18
19
    public function start(): void
20
    {
21
        $this->startTime = microtime(true);
22
    }
23
24
    public function stop(): void
25
    {
26
        $this->endTime = microtime(true);
27
        $this->peakMemoryUsage = memory_get_peak_usage(true);
28
    }
29
30
    private function getExecutionTime(): float
31
    {
32
        return round(($this->endTime - $this->startTime) * 1000, 2);
33
    }
34
35
    private function getPeakMemoryUsage(): float
36
    {
37
        return round($this->peakMemoryUsage / 1024 / 1024, 2); // Convert to MB
38
    }
39
40
    public function getPerformanceMetrics(): PerformanceMetricsDTO
41
    {
42
        return new PerformanceMetricsDTO(
43
            $this->getExecutionTime(),
44
            $this->getPeakMemoryUsage()
45
        );
46
    }
47
}
48