Completed
Push — develop ( fe1195...b733eb )
by Alec
05:48
created

BenchmarkReport   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 84
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

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