Completed
Push — master ( 6a7da5...1827cf )
by Alec
08:13 queued 02:32
created

BenchmarkReportFormatter::average()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Tools\Reports\Formatters;
6
7
use AlecRabbit\Pretty;
8
use AlecRabbit\Tools\Internal\BenchmarkFunction;
9
use AlecRabbit\Tools\Reports\BenchmarkReport;
10
use function AlecRabbit\typeOf;
11
12
class BenchmarkReportFormatter extends Formatter
13
{
14
    /** @var BenchmarkReport */
15
    protected $report;
16
17
    /**
18
     * {@inheritdoc}
19
     */
20 2
    public function getString(): string
21
    {
22 2
        $profilerReport = (string)$this->report->getProfiler()->getReport();
23 2
        $r = 'Benchmark:' . PHP_EOL;
24 2
        $withException = '';
25
        /** @var BenchmarkFunction $function */
26 2
        foreach ($this->report->getFunctions() as $name => $function) {
27
//            dump($function);
28 2
            $br = $function->getBenchmarkRelative();
29 2
            $types = $this->extractArguments($function->getArgs());
30
31 2
            if ($br) {
32 2
                $relative = $br->getRelative();
33 2
                $average = $br->getAverage();
34 2
                $rank = $br->getRank();
35
                $r .=
36 2
                    sprintf(
37 2
                        '%s. %s (%s) %s(%s) %s %s',
38 2
                        (string)$rank,
39 2
                        $this->average($average),
40 2
                        $this->relativePercent($relative),
41 2
                        $function->humanReadableName(),
42 2
                        implode(', ', $types),
43 2
                        $function->comment(),
44 2
                        PHP_EOL
45
                    );
46 2
                $result = $function->getResult();
47
                $r .=
48 2
                    sprintf(
49 2
                        '%s(%s) %s',
50 2
                        typeOf($result),
51 2
                        var_export($result, true),
52 2
                        PHP_EOL
53
                    );
54 1
            } elseif ($e = $function->getException()) {
55 1
                $withException .= sprintf(
56 1
                    '%s(%s) %s %s %s',
57 1
                    $function->humanReadableName(),
58 1
                    implode(', ', $types),
59 1
                    $function->comment(),
60 1
                    $e->getMessage(),
61 1
                    PHP_EOL
62
                );
63
            } else {
64
                // @codeCoverageIgnoreStart
65
                // this should never be thrown otherwise something is terribly wrong
66
                throw new \RuntimeException('BenchmarkFunction has no BenchmarkRelative nor Exception object.');
67
                // @codeCoverageIgnoreEnd
68
            }
69
        }
70
        return
71 2
            $r . PHP_EOL .
72 2
            (empty($withException) ? '' : 'Exceptions:' . PHP_EOL . $withException) . PHP_EOL .
73 2
            $profilerReport;
74
    }
75
76
    /**
77
     * @param array $arguments
78
     * @return array
79
     */
80 2
    protected function extractArguments(array $arguments): array
81
    {
82 2
        $types = [];
83 2
        if (!empty($arguments)) {
84 1
            foreach ($arguments as $argument) {
85 1
                $types[] = typeOf($argument);
86
            }
87
        }
88 2
        return $types;
89
    }
90
91
    /**
92
     * @param float $average
93
     * @return string
94
     */
95 2
    protected function average(float $average): string
96
    {
97 2
        return str_pad(
98 2
            Pretty::time($average),
99 2
            8,
100 2
            ' ',
101 2
            STR_PAD_LEFT
102
        );
103
    }
104
105
    /**
106
     * @param float $relative
107
     * @return string
108
     */
109 2
    protected function relativePercent(float $relative): string
110
    {
111 2
        return str_pad(
112 2
            Pretty::percent($relative),
113 2
            7,
114 2
            ' ',
115 2
            STR_PAD_LEFT
116
        );
117
    }
118
}
119