Completed
Push — develop ( 35365a...6b618f )
by Alec
03:18
created

BenchmarkReport   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 104
ccs 48
cts 48
cp 1
rs 10
c 0
b 0
f 0
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A computeRelatives() 0 20 3
A computeAverages() 0 10 3
A getFunctionObject() 0 3 1
A __construct() 0 5 1
A percent() 0 4 1
A __toString() 0 26 4
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\BenchmarkedFunction;
12
use AlecRabbit\Tools\Reports\Base\Report;
13
use AlecRabbit\Tools\Timer;
14
use AlecRabbit\Tools\Traits\BenchmarkFields;
15
use function AlecRabbit\brackets;
16
use function AlecRabbit\format_time;
17
use function AlecRabbit\typeOf;
18
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...
19
use const AlecRabbit\Constants\BRACKETS_PARENTHESES;
0 ignored issues
show
Bug introduced by
The constant AlecRabbit\Constants\BRACKETS_PARENTHESES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
20
21
class BenchmarkReport extends Report
22
{
23
    use BenchmarkFields;
24
25
    /**
26
     * BenchmarkReport constructor.
27
     * @param Benchmark $reportable
28
     */
29 2
    public function __construct(Benchmark $reportable)
30
    {
31 2
        $this->profiler = $reportable->getProfiler();
32 2
        $this->functions = $reportable->getFunctions();
33 2
        $this->iteration = $reportable->getIteration();
34 2
    }
35
36
    /**
37
     * @return string
38
     */
39 1
    public function __toString(): string
40
    {
41 1
        $r = (string)$this->getProfiler()->report();
42 1
        $r .= 'Benchmark:' . PHP_EOL;
43 1
        foreach ($this->computeRelatives() as $indexName => $result) {
44 1
            $function = $this->getFunctionObject($indexName);
45 1
            $arguments = $function->getArgs();
46 1
            $types = [];
47 1
            if (!empty($arguments)) {
48 1
                foreach ($arguments as $argument) {
49 1
                    $types[] = typeOf($argument);
50
                }
51
            }
52 1
            $r .= sprintf(
53 1
                '+%s [%s] %s(%s) %s %s %s',
54 1
                $result,
55 1
                $function->getIndex(),
56 1
                $function->getName(),
57 1
                implode(', ', $types),
58 1
                $function->getComment(),
59 1
                $this->getIteration(),
60 1
                PHP_EOL
61
            );
62
        }
63
        return
64 1
            $r;
65
    }
66
67
    /**
68
     * @return array
69
     */
70 1
    private function computeRelatives(): array
71
    {
72 1
        $averages = $this->computeAverages(
73 1
            $this->profiler->getTimers()
74
        );
75
76 1
        $min = min($averages);
77
78 1
        $relatives = [];
79 1
        foreach ($averages as $name => $average) {
80 1
            $relatives[$name] = $average / $min;
81
        }
82 1
        asort($relatives);
83
84 1
        foreach ($relatives as $name => $relative) {
85 1
            $relatives[$name] =
86 1
                $this->percent((float)$relative - 1) . ' ' .
87 1
                brackets(format_time($averages[$name]), BRACKETS_PARENTHESES);
88
        }
89 1
        return $relatives;
90
    }
91
92
    /**
93
     * @param array $timers
94
     * @return array
95
     */
96 1
    private function computeAverages(array $timers): array
97
    {
98 1
        $averages = [];
99
        /** @var Timer $timer */
100 1
        foreach ($timers as $timer) {
101 1
            if (DEFAULT_NAME !== $name = $timer->getName()) {
102 1
                $averages[$name] = $timer->getAverageValue();
103
            }
104
        }
105 1
        return $averages;
106
    }
107
108
    /**
109
     * @param float $relative
110
     * @return string
111
     */
112 1
    private function percent(float $relative): string
113
    {
114
        return
115 1
            number_format($relative * 100, 1) . '%';
116
    }
117
118
    /**
119
     * @param string $name
120
     * @return BenchmarkedFunction
121
     */
122 1
    private function getFunctionObject(string $name): BenchmarkedFunction
123
    {
124 1
        return $this->functions[$name];
125
    }
126
}
127