Failed Conditions
Pull Request — 0.3 (#20)
by jean
06:00
created

StatCliDumper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 95
ccs 0
cts 68
cp 0
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A showBar() 0 8 1
A showBarClass() 0 12 2
C dump() 0 67 10
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: darkilliant
5
 * Date: 6/24/18
6
 * Time: 11:14 AM.
7
 */
8
9
namespace Darkilliant\ProcessBundle\StatDumper;
10
11
use Symfony\Component\Console\Style\SymfonyStyle;
12
13
class StatCliDumper
14
{
15
    public function dump(array $stats, array $total, $zoom = false, SymfonyStyle $outputHelper)
16
    {
17
        $headers = ['class', '01 BEST', '02 BEST', '03 BEST', '01 BAD', '02 BAD', '03 BAD', 'TOTAL RUN', 'TOTAL WAIT'];
18
        $data = [];
19
        $globals = array_combine(array_keys($stats), array_column($stats, 'global'));
20
        $maxGlobal = max($globals);
21
22
        foreach ($stats as $class => $values) {
23
            $path = explode('\\', $class);
24
            $class = array_pop($path);
25
            $times = array_replace($values['best_times'], $values['bad_times']);
26
            foreach ($times as $i => &$time) {
27
                $time = (int) $time;
28
                $time = ($time > 100) ? '<error>'.$time.' ms</error>' : $time.' ms';
29
                if ($zoom) {
30
                    $time .= PHP_EOL.($i + 1).PHP_EOL;
31
                }
32
            }
33
            $global = $values['global'];
34
            if ($global === $maxGlobal) {
35
                $global = sprintf('<error>%d ms</error>', (int) $values['global']);
36
            } else {
37
                $global = ((int) $values['global']).' ms';
38
            }
39
            $times[] = $global;
40
            $times[] = ((int) $values['global_wait']).' ms';
41
            $data[] = array_merge([$values['position'].'. ('.$values['tendance'].') '.$class.' ('.number_format($values['count_iteration']).'x)'], $times);
42
        }
43
44
        $outputHelper->table($headers, $data);
45
46
        $waits = array_combine(array_keys($stats), array_column($stats, 'global_wait'));
47
        $maxWait = max($waits);
48
49
        foreach ($waits as $i => $ms) {
50
            $waits[$i] = (int) $waits[$i];
51
        }
52
53
        $reference = $maxWait / 100;
54
55
        foreach ($waits as $class => $ms) {
56
            if (0 !== $ms) {
57
                $countPercents = (int) ($ms / $reference);
58
59
                if ($countPercents < 1) {
60
                    $countPercents = 1;
61
                }
62
                echo $this->showBar($countPercents);
63
                echo $this->showBar($countPercents);
64
                echo $this->showBar($countPercents);
65
                echo $this->showBar($countPercents);
66
                echo $this->showBar($countPercents);
67
                echo $this->showBar($countPercents);
68
                echo $this->showBar($countPercents);
69
                echo $this->showBar($countPercents);
70
                echo $this->showBar($countPercents);
71
                echo $this->showBarClass($class, $countPercents);
72
                echo $this->showBar($countPercents);
73
                echo $this->showBar($countPercents);
74
                echo $this->showBar($countPercents);
75
                echo $this->showBar($countPercents);
76
                echo $this->showBar($countPercents);
77
                echo $this->showBar($countPercents);
78
                echo $this->showBar($countPercents);
79
                echo $this->showBar($countPercents);
80
                echo $this->showBar($countPercents);
81
                echo $this->showBar($countPercents);
82
            }
83
        }
84
    }
85
86
    public function showBar($countPercents)
87
    {
88
        $bar = str_repeat(' ', 10);
89
        $bar .= str_repeat(' ', (100 - $countPercents) / 2);
90
        $bar .= "\e[32m\e[42m".str_repeat('#', $countPercents)."\e[0m";
91
        $bar .= PHP_EOL;
92
93
        return $bar;
94
    }
95
96
    public function showBarClass($class, $countPercents)
97
    {
98
        $size = strlen($class);
99
        $bar = str_repeat(' ', 10);
100
        $bar .= str_repeat(' ', (100 - $countPercents) / 2);
101
        $bar .= "\e[42m".$class."\e[0m";
102
        if ($countPercents > $size) {
103
            $bar .= "\e[32m\e[42m".str_repeat('#', $countPercents - $size)."\e[0m";
104
        }
105
        $bar .= PHP_EOL;
106
107
        return $bar;
108
    }
109
}
110