|
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
|
2 |
|
$this->updateMemory(); |
|
74
|
2 |
|
$this->updateItemPerSecond(); |
|
75
|
2 |
|
$this->progressBar->setProgress($progress); |
|
76
|
|
|
} |
|
77
|
2 |
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
public function advance() |
|
80
|
|
|
{ |
|
81
|
1 |
|
if ($this->isUpdate()) { |
|
82
|
1 |
|
$this->updateMemory(); |
|
83
|
1 |
|
$this->updateItemPerSecond(); |
|
84
|
1 |
|
$this->progressBar->advance(); |
|
85
|
|
|
} |
|
86
|
1 |
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
public function finish() |
|
89
|
|
|
{ |
|
90
|
1 |
|
$this->progressBar->finish(); |
|
91
|
|
|
|
|
92
|
1 |
|
$this->itemCount = []; |
|
|
|
|
|
|
93
|
1 |
|
} |
|
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
|
2 |
|
$this->timeElapsed = |
|
113
|
2 |
|
(null === $this->lastTimeUpdated) |
|
114
|
|
|
? 1 |
|
115
|
2 |
|
: $time - $this->lastTimeUpdated; |
|
116
|
2 |
|
$this->lastTimeUpdated = $time; |
|
117
|
|
|
|
|
118
|
2 |
|
return true; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return false; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
2 |
|
private function updateMemory() |
|
125
|
|
|
{ |
|
126
|
2 |
|
$time = time(); |
|
127
|
|
|
|
|
128
|
2 |
|
if (count($this->timelineMemory) > self::INTERVAL_MONITORING[2]) { |
|
129
|
1 |
|
$this->timelineMemory = array_slice($this->timelineMemory, 1, null, true); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
2 |
|
$this->timelineMemory[$time] = $now = memory_get_usage(true); |
|
133
|
|
|
|
|
134
|
2 |
|
$this->intervalMemory[0] = $this->formatMemory( |
|
135
|
2 |
|
$this->timelineMemory[$time - self::INTERVAL_MONITORING[0]] ?? $now, |
|
136
|
2 |
|
self::INTERVAL_MONITORING[0] |
|
137
|
|
|
); |
|
138
|
2 |
|
$this->intervalMemory[1] = $this->formatMemory( |
|
139
|
2 |
|
$this->timelineMemory[$time - self::INTERVAL_MONITORING[1]] ?? -1, |
|
140
|
2 |
|
self::INTERVAL_MONITORING[1] |
|
141
|
|
|
); |
|
142
|
2 |
|
$this->intervalMemory[2] = $this->formatMemory( |
|
143
|
2 |
|
$this->timelineMemory[$time - self::INTERVAL_MONITORING[2]] ?? -1, |
|
144
|
2 |
|
self::INTERVAL_MONITORING[2] |
|
145
|
|
|
); |
|
146
|
2 |
|
} |
|
147
|
|
|
|
|
148
|
2 |
|
private function updateItemPerSecond() |
|
149
|
|
|
{ |
|
150
|
2 |
|
$time = time(); |
|
151
|
|
|
|
|
152
|
2 |
|
if (count($this->timelineItemPerSecond) > self::INTERVAL_MONITORING[2]) { |
|
153
|
1 |
|
$this->timelineItemPerSecond = array_slice($this->timelineItemPerSecond, 1, null, true); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
2 |
|
$this->timelineItemPerSecond[$time] = $now = ($this->progressBar->getProgress() - $this->previousItemCount) / $this->timeElapsed; |
|
157
|
2 |
|
$this->previousItemCount = $this->progressBar->getProgress(); |
|
158
|
|
|
|
|
159
|
2 |
|
$this->intervalItemPerSecond[0] = $this->formatItemsCount( |
|
160
|
2 |
|
$this->timelineItemPerSecond[$time - self::INTERVAL_MONITORING[0]] ?? $now, |
|
161
|
2 |
|
self::INTERVAL_MONITORING[0] |
|
162
|
|
|
); |
|
163
|
2 |
|
$this->intervalItemPerSecond[1] = $this->formatItemsCount( |
|
164
|
2 |
|
$this->timelineItemPerSecond[$time - self::INTERVAL_MONITORING[1]] ?? -1, |
|
165
|
2 |
|
self::INTERVAL_MONITORING[1] |
|
166
|
|
|
); |
|
167
|
2 |
|
$this->intervalItemPerSecond[2] = $this->formatItemsCount( |
|
168
|
2 |
|
$this->timelineItemPerSecond[$time - self::INTERVAL_MONITORING[2]] ?? -1, |
|
169
|
2 |
|
self::INTERVAL_MONITORING[2] |
|
170
|
|
|
); |
|
171
|
2 |
|
} |
|
172
|
|
|
|
|
173
|
2 |
|
private function formatMemory($memory, $time) |
|
174
|
|
|
{ |
|
175
|
2 |
|
$colors = (($memory / 1000) > $this->maxMemory * 1000) ? '41;37' : '44;37'; |
|
176
|
2 |
|
$message = $time.'s = '.Helper::formatMemory($memory); |
|
177
|
|
|
|
|
178
|
2 |
|
return "\033[".$colors.'m '.$message." \033[0m"; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
2 |
|
private function formatItemsCount($itemsCount, $time) |
|
182
|
|
|
{ |
|
183
|
2 |
|
$colors = ($itemsCount < $this->minItemPerSecond) ? '41;37' : '44;37'; |
|
184
|
2 |
|
$message = $time.'s = '.$itemsCount; |
|
185
|
|
|
|
|
186
|
2 |
|
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.