1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Darkilliant\ProcessBundle\Console; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Console\Helper\Helper; |
8
|
|
|
use Symfony\Component\Console\Helper\ProgressBar as SymfonyProgressBar; |
9
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @internal |
14
|
|
|
* Class ProgressBar |
15
|
|
|
*/ |
16
|
|
|
class ProgressBar |
17
|
|
|
{ |
18
|
|
|
const INTERVAL_MONITORING = [1, 10, 20]; |
19
|
|
|
/** @var OutputInterface */ |
20
|
|
|
private $output; |
21
|
|
|
/** @var SymfonyProgressBar */ |
22
|
|
|
private $progressBar; |
23
|
|
|
|
24
|
|
|
private $timelineMemory = []; |
25
|
|
|
private $intervalMemory = [-1, -1, -1]; |
26
|
|
|
private $maxMemory; |
27
|
|
|
|
28
|
|
|
private $timelineItemPerSecond = []; |
29
|
|
|
private $intervalItemPerSecond = [-1, -1, -1]; |
30
|
|
|
private $minItemPerSecond; |
31
|
|
|
|
32
|
|
|
private $lastTimeUpdated; |
33
|
|
|
private $timeElapsed = 0; |
34
|
|
|
private $previousItemCount = 0; |
35
|
|
|
|
36
|
4 |
|
public function setOutput($output) |
37
|
|
|
{ |
38
|
4 |
|
$this->output = $output; |
39
|
4 |
|
} |
40
|
|
|
|
41
|
4 |
|
public function create($size, $title = 'Progression', $maxMemory = 30, $minItemPerSecond = 50) |
42
|
|
|
{ |
43
|
4 |
|
$this->init(); |
44
|
4 |
|
$this->maxMemory = $maxMemory; |
45
|
4 |
|
$this->minItemPerSecond = $minItemPerSecond; |
46
|
|
|
|
47
|
4 |
|
$this->progressBar = |
48
|
4 |
|
($this->output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) |
49
|
1 |
|
? new SymfonyProgressBar(new NullOutput(), $size) |
50
|
3 |
|
: new SymfonyProgressBar($this->output, $size); |
51
|
4 |
|
SymfonyProgressBar::setPlaceholderFormatterDefinition('memory', [$this, 'renderRightbar']); |
52
|
4 |
|
$this->progressBar->setFormat(" \033[44;37m %title:-37s% \033[0m\n %current%/%max% %bar% %percent:3s%%\n 🏁 %remaining:-10s% %memory:37s%"); |
53
|
4 |
|
$this->progressBar->setBarCharacter($done = "\033[32m=\033[0m"); |
54
|
4 |
|
$this->progressBar->setEmptyBarCharacter($empty = "\033[31m \033[0m"); |
55
|
4 |
|
$this->progressBar->setProgressCharacter($progress = "\033[32m>\033[0m"); |
56
|
4 |
|
$this->progressBar->setBarWidth(400); |
57
|
4 |
|
$this->progressBar->setMessage($title, 'title'); |
58
|
4 |
|
$this->progressBar->start(); |
59
|
4 |
|
} |
60
|
|
|
|
61
|
3 |
|
public function renderRightbar(SymfonyProgressBar $bar) |
|
|
|
|
62
|
|
|
{ |
63
|
3 |
|
return sprintf( |
64
|
3 |
|
'MEMORY %s / ITEMS %s', |
65
|
3 |
|
implode(' ', $this->intervalMemory), |
66
|
3 |
|
implode(' ', $this->intervalItemPerSecond) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
public function setProgress($progress) |
71
|
|
|
{ |
72
|
2 |
|
if ($this->isUpdate()) { |
73
|
1 |
|
$this->updateMemory(); |
74
|
1 |
|
$this->updateItemPerSecond(); |
75
|
1 |
|
$this->progressBar->setProgress($progress); |
76
|
|
|
} |
77
|
2 |
|
} |
78
|
|
|
|
79
|
|
|
public function advance() |
80
|
|
|
{ |
81
|
|
|
if ($this->isUpdate()) { |
82
|
|
|
$this->updateMemory(); |
83
|
|
|
$this->updateItemPerSecond(); |
84
|
|
|
$this->progressBar->advance(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function finish() |
89
|
|
|
{ |
90
|
|
|
$this->progressBar->finish(); |
91
|
|
|
|
92
|
|
|
$this->itemCount = []; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
4 |
|
private function init() |
96
|
|
|
{ |
97
|
4 |
|
$this->timelineMemory = []; |
98
|
4 |
|
$this->intervalMemory = [-1, -1, -1]; |
99
|
|
|
|
100
|
4 |
|
$this->timelineItemPerSecond = []; |
101
|
4 |
|
$this->intervalItemPerSecond = [-1, -1, -1]; |
102
|
|
|
|
103
|
4 |
|
$this->lastTimeUpdated = time(); |
104
|
4 |
|
$this->previousItemCount = 0; |
105
|
4 |
|
} |
106
|
|
|
|
107
|
2 |
|
private function isUpdate() |
108
|
|
|
{ |
109
|
2 |
|
$time = time(); |
110
|
|
|
|
111
|
2 |
|
if ($time > $this->lastTimeUpdated) { |
112
|
1 |
|
$this->timeElapsed = |
113
|
1 |
|
(null === $this->lastTimeUpdated) |
114
|
|
|
? 1 |
115
|
1 |
|
: $time - $this->lastTimeUpdated; |
116
|
1 |
|
$this->lastTimeUpdated = $time; |
117
|
|
|
|
118
|
1 |
|
return true; |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
private function updateMemory() |
125
|
|
|
{ |
126
|
1 |
|
$time = time(); |
127
|
|
|
|
128
|
1 |
|
if (count($this->timelineMemory) > self::INTERVAL_MONITORING[2]) { |
129
|
1 |
|
$this->timelineMemory = array_slice($this->timelineMemory, 1, null, true); |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
$this->timelineMemory[$time] = $now = memory_get_usage(true); |
133
|
|
|
|
134
|
1 |
|
$this->intervalMemory[0] = $this->formatMemory( |
135
|
1 |
|
$this->timelineMemory[$time - self::INTERVAL_MONITORING[0]] ?? $now, |
136
|
1 |
|
self::INTERVAL_MONITORING[0] |
137
|
|
|
); |
138
|
1 |
|
$this->intervalMemory[1] = $this->formatMemory( |
139
|
1 |
|
$this->timelineMemory[$time - self::INTERVAL_MONITORING[1]] ?? -1, |
140
|
1 |
|
self::INTERVAL_MONITORING[1] |
141
|
|
|
); |
142
|
1 |
|
$this->intervalMemory[2] = $this->formatMemory( |
143
|
1 |
|
$this->timelineMemory[$time - self::INTERVAL_MONITORING[2]] ?? -1, |
144
|
1 |
|
self::INTERVAL_MONITORING[2] |
145
|
|
|
); |
146
|
1 |
|
} |
147
|
|
|
|
148
|
1 |
|
private function updateItemPerSecond() |
149
|
|
|
{ |
150
|
1 |
|
$time = time(); |
151
|
|
|
|
152
|
1 |
|
if (count($this->timelineItemPerSecond) > self::INTERVAL_MONITORING[2]) { |
153
|
1 |
|
$this->timelineItemPerSecond = array_slice($this->timelineItemPerSecond, 1, null, true); |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
$this->timelineItemPerSecond[$time] = $now = ($this->progressBar->getProgress() - $this->previousItemCount) / $this->timeElapsed; |
157
|
1 |
|
$this->previousItemCount = $this->progressBar->getProgress(); |
158
|
|
|
|
159
|
1 |
|
$this->intervalItemPerSecond[0] = $this->formatItemsCount( |
160
|
1 |
|
$this->timelineItemPerSecond[$time - self::INTERVAL_MONITORING[0]] ?? $now, |
161
|
1 |
|
self::INTERVAL_MONITORING[0] |
162
|
|
|
); |
163
|
1 |
|
$this->intervalItemPerSecond[1] = $this->formatItemsCount( |
164
|
1 |
|
$this->timelineItemPerSecond[$time - self::INTERVAL_MONITORING[1]] ?? -1, |
165
|
1 |
|
self::INTERVAL_MONITORING[1] |
166
|
|
|
); |
167
|
1 |
|
$this->intervalItemPerSecond[2] = $this->formatItemsCount( |
168
|
1 |
|
$this->timelineItemPerSecond[$time - self::INTERVAL_MONITORING[2]] ?? -1, |
169
|
1 |
|
self::INTERVAL_MONITORING[2] |
170
|
|
|
); |
171
|
1 |
|
} |
172
|
|
|
|
173
|
1 |
|
private function formatMemory($memory, $time) |
174
|
|
|
{ |
175
|
1 |
|
$colors = (($memory / 1000) > $this->maxMemory * 1000) ? '41;37' : '44;37'; |
176
|
1 |
|
$message = $time.'s = '.Helper::formatMemory($memory); |
177
|
|
|
|
178
|
1 |
|
return "\033[".$colors.'m '.$message." \033[0m"; |
179
|
|
|
} |
180
|
|
|
|
181
|
1 |
|
private function formatItemsCount($itemsCount, $time) |
182
|
|
|
{ |
183
|
1 |
|
$colors = ($itemsCount < $this->minItemPerSecond) ? '41;37' : '44;37'; |
184
|
1 |
|
$message = $time.'s = '.$itemsCount; |
185
|
|
|
|
186
|
1 |
|
return "\033[".$colors.'m '.$message."/s \033[0m"; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.