Test Failed
Push — main ( 27d416...4ff5eb )
by mikhail
03:21
created

ResourceUtilization   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPerformanceMetrics() 0 5 1
A stop() 0 4 1
A getExecutionTime() 0 3 1
A start() 0 3 1
A getPeakMemoryUsage() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\AnalyzeComments\Metrics;
6
7
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\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
use function memory_get_peak_usage;
9
use function microtime;
10
use function round;
11
12
final class ResourceUtilization
13
{
14
    private float $startTime;
15
16
    private float $endTime;
17
18
    private float $peakMemoryUsage;
19
20
    public function start(): void
21
    {
22
        $this->startTime = microtime(true);
23
    }
24
25
    public function stop(): void
26
    {
27
        $this->endTime = microtime(true);
28
        $this->peakMemoryUsage = memory_get_peak_usage(true);
29
    }
30
31
    public function getPerformanceMetrics(): PerformanceMetricsDTO
32
    {
33
        return new PerformanceMetricsDTO(
34
            $this->getExecutionTime(),
35
            $this->getPeakMemoryUsage(),
36
        );
37
    }
38
39
    private function getExecutionTime(): float
40
    {
41
        return round(($this->endTime - $this->startTime) * 1000, 2);
42
    }
43
44
    private function getPeakMemoryUsage(): float
45
    {
46
        return round($this->peakMemoryUsage / 1024 / 1024, 2); // Convert to MB
47
    }
48
}
49