Passed
Push — develop ( d04ffa...d2ca78 )
by Alec
03:35
created

BenchmarkReport   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 90
ccs 32
cts 34
cp 0.9412
rs 10
c 0
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getRelatives() 0 3 1
A computeRelatives() 0 18 4
A computeAverages() 0 14 4
A getFunctionObject() 0 3 1
A __construct() 0 11 1
A getFunctionObjects() 0 3 1
1
<?php
2
/**
3
 * User: alec
4
 * Date: 29.11.18
5
 * Time: 20:56
6
 */
7
8
namespace AlecRabbit\Tools\Reports;
9
10
use AlecRabbit\Tools\Benchmark;
11
use AlecRabbit\Tools\Internal\BenchmarkFunction;
12
use AlecRabbit\Tools\Internal\BenchmarkRelative;
13
use AlecRabbit\Tools\Reports\Base\Report;
14
use AlecRabbit\Tools\Timer;
15
use AlecRabbit\Tools\Traits\BenchmarkFields;
16
use const \AlecRabbit\Constants\Traits\DEFAULT_NAME;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\Constants\Traits\DEFAULT_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
17
18
class BenchmarkReport extends Report
19
{
20
    use BenchmarkFields;
21
22
    /** @var array */
23
    protected $relatives;
24
25
    /**
26
     * BenchmarkReport constructor.
27
     * @param Benchmark $benchmark
28
     */
29 4
    public function __construct(Benchmark $benchmark)
30
    {
31 4
        $this->profiler = $benchmark->getProfiler();
32 4
        $this->functions = $benchmark->getFunctions();
33 4
        $this->totalIterations = $benchmark->getTotalIterations();
34 4
        $this->withResults = $benchmark->isWithResults();
35 4
        $this->exceptionMessages = $benchmark->getExceptionMessages();
36 4
        $this->exceptions = $benchmark->getExceptions();
37 4
        $this->relatives = $this->computeRelatives();
38
39 4
        parent::__construct();
40 4
    }
41
42
    /**
43
     * @return array
44
     */
45 4
    private function computeRelatives(): array
46
    {
47 4
        $averages = $this->computeAverages($this->profiler->getTimers());
48 4
        $relatives = [];
49 4
        if (!empty($averages)) {
50 2
            $min = min($averages);
51
52 2
            foreach ($averages as $name => $average) {
53 2
                $relatives[$name] = $average / $min;
54
            }
55 2
            asort($relatives);
56
57
            /** @var  float|int $relative */
58 2
            foreach ($relatives as $name => $relative) {
59 2
                $relatives[$name] = new BenchmarkRelative((float)$relative - 1, $averages[$name]);
60
            }
61
        }
62 4
        return $relatives;
63
    }
64
65
    /**
66
     * @param array $timers
67
     * @return array
68
     */
69 4
    private function computeAverages(array $timers): array
70
    {
71 4
        $averages = [];
72
        /** @var Timer $timer */
73 4
        foreach ($timers as $timer) {
74 4
            if (DEFAULT_NAME !== $name = $timer->getName()) {
75
                try {
76 2
                    $averages[$name] = $timer->getAverageValue();
77 4
                } catch (\Throwable $e) {
78
                    // no further action
79
                }
80
            }
81
        }
82 4
        return $averages;
83
    }
84
85
    /**
86
     * @param string $name
87
     * @return BenchmarkFunction
88
     */
89 2
    public function getFunctionObject(string $name): BenchmarkFunction
90
    {
91 2
        return $this->functions[$name];
92
    }
93
94
    /**
95
     * @return array
96
     */
97
    public function getFunctionObjects(): array
98
    {
99
        return $this->functions;
100
    }
101
102
    /**
103
     * @return array
104
     */
105 3
    public function getRelatives(): array
106
    {
107 3
        return $this->relatives;
108
    }
109
}
110