10000 [ProgressBar] Default to time based frequency which is more performant · symfony/symfony@bf3cca3 · GitHub
[go: up one dir, main page]

Skip to content

Commit bf3cca3

Browse files
committed
[ProgressBar] Default to time based frequency which is more performant
1 parent f96753b commit bf3cca3

File tree

2 files changed

+79
-6
lines changed

2 files changed

+79
-6
lines changed

src/Symfony/Component/Console/Helper/ProgressBar.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ final class ProgressBar
3131
private $progressChar = '>';
3232
private $format;
3333
private $internalFormat;
34-
private $redrawFreq = 1;
34+
private $redrawFreq;
35+
private $lastWriteTime;
3536
private $output;
3637
private $step = 0;
3738
private $max;
@@ -64,9 +65,6 @@ public function __construct(OutputInterface $output, int $max = 0)
6465
if (!$this->output->isDecorated()) {
6566
// disable overwrite when output does not support ANSI codes.
6667
$this->overwrite = false;
67-
68-
// set a reasonable redraw frequency so output isn't flooded
69-
$this->setRedrawFrequency($max / 10);
7068
}
7169

7270
$this->startTime = time();
@@ -287,10 +285,21 @@ public function setProgress(int $step)
287285
$step = 0;
288286
}
289287

290-
$prevPeriod = (int) ($this->step / $this->redrawFreq);
291-
$currPeriod = (int) ($step / $this->redrawFreq);
288+
$prevStep = $this->step;
292289
$this->step = $step;
293290
$this->percent = $this->max ? (float) $this->step / $this->max : 0;
291+
292+
if (null === $this->redrawFreq) {
293+
if (microtime(true) - $this->lastWriteTime >= .05 || $this->max === $step) {
294+
$this->display();
295+
}
296+
297+
return;
298+
}
299+
300+
$prevPeriod = (int) ($prevStep / $this->redrawFreq);
301+
$currPeriod = (int) ($step / $this->redrawFreq);
302+
294303
if ($prevPeriod !== $currPeriod || $this->max === $step) {
295304
$this->display();
296305
}
@@ -398,6 +407,7 @@ private function overwrite(string $message): void
398407
}
399408

400409
$this->firstRun = false;
410+
$this->lastWriteTime = microtime(true);
401411

402412
$this->output->write($message);
403413
}

src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
*/
2424
class ProgressBarTest extends TestCase
2525
{
26+
const TEN_MS = 100001;
27+
2628
public function testMultipleStart()
2729
{
2830
$bar = new ProgressBar($output = $this->getOutputStream());
2931
$bar->start();
32+
usleep(self::TEN_MS);
3033
$bar->advance();
3134
$bar->start();
3235

@@ -43,6 +46,7 @@ public function testAdvance()
4346
{
4447
$bar = new ProgressBar($output = $this->getOutputStream());
4548
$bar->start();
49+
usleep(self::TEN_MS);
4650
$bar->advance();
4751

4852
rewind($output->getStream());
@@ -57,6 +61,7 @@ public function testAdvanceWithStep()
5761
{
5862
$bar = new ProgressBar($output = $this->getOutputStream());
5963
$bar->start();
64+
usleep(self::TEN_MS);
6065
$bar->advance(5);
6166

6267
rewind($output->getStream());
@@ -71,7 +76,9 @@ public function testAdvanceMultipleTimes()
7176
{
7277
$bar = new ProgressBar($output = $this->getOutputStream());
7378
$bar->start();
79+
usleep(self::TEN_MS);
7480
$bar->advance(3);
81+
usleep(self::TEN_MS);
7582
$bar->advance(2);
7683

7784
rewind($output->getStream());
@@ -103,8 +110,11 @@ public function testRegress()
103110
{
104111
$bar = new ProgressBar($output = $this->getOutputStream());
105112
$bar->start();
113+
usleep(self::TEN_MS);
106114
$bar->advance();
115+
usleep(self::TEN_MS);
107116
$bar->advance();
117+
usleep(self::TEN_MS);
108118
$bar->advance(-1);
109119

110120
rewind($output->getStream());
@@ -121,8 +131,11 @@ public function testRegressWithStep()
121131
{
122132
$bar = new ProgressBar($output = $this->getOutputStream());
123133
$bar->start();
134+
usleep(self::TEN_MS);
124135
$bar->advance(4);
136+
usleep(self::TEN_MS);
125137
$bar->advance(4);
138+
usleep(self::TEN_MS);
126139
$bar->advance(-2);
127140

128141
rewind($output->getStream());
@@ -139,9 +152,13 @@ public function testRegressMultipleTimes()
139152
{
140153
$bar = new ProgressBar($output = $this->getOutputStream());
141154
$bar->start();
155+
usleep(self::TEN_MS);
142156
$bar->advance(3);
157+
usleep(self::TEN_MS);
143158
$bar->advance(3);
159+
usleep(self::TEN_MS);
144160
$bar->advance(-1);
161+
usleep(self::TEN_MS);
145162
$bar->advance(-2);
146163

147164
rewind($output->getStream());
@@ -160,6 +177,7 @@ public function testRegressBelowMin()
160177
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
161178
$bar->setProgress(1);
162179
$bar->advance(-1);
180+
usleep(self::TEN_MS);
163181
$bar->advance(-1);
164182

165183
rewind($output->getStream());
@@ -200,6 +218,7 @@ public function testFormat()
200218
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
201219
$bar->setFormat('normal');
202220
$bar->start();
221+
usleep(self::TEN_MS);
203222
$bar->advance(10);
204223
$bar->finish();
205224

@@ -210,6 +229,7 @@ public function testFormat()
210229
$bar = new ProgressBar($output = $this->getOutputStream());
211230
$bar->setFormat('normal');
212231
$bar->start(10);
232+
usleep(self::TEN_MS);
213233
$bar->advance(10);
214234
$bar->finish();
215235

@@ -226,6 +246,7 @@ public function testCustomizations()
226246
$bar->setProgressCharacter('/');
227247
$bar->setFormat(' %current%/%max% [%bar%] %percent:3s%%');
228248
$bar->start();
249+
usleep(self::TEN_MS);
229250
$bar->advance();
230251

231252
rewind($output->getStream());
@@ -277,7 +298,9 @@ public function testPercent()
277298
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
278299
$bar->start();
279300
$bar->display();
301+
usleep(self::TEN_MS);
280302
$bar->advance();
303+
usleep(self::TEN_MS);
281304
$bar->advance();
282305

283306
rewind($output->getStream());
@@ -296,7 +319,9 @@ public function testOverwriteWithShorterLine()
296319
$bar->setFormat(' %current%/%max% [%bar%] %percent:3s%%');
297320
$bar->start();
298321
$bar->display();
322+
usleep(self::TEN_MS);
299323
$bar->advance();
324+
usleep(self::TEN_MS);
300325

301326
// set shorter format
302327
$bar->setFormat(' %current%/%max% [%bar%]');
@@ -321,7 +346,9 @@ public function testOverwriteWithSectionOutput()
321346
$bar = new ProgressBar($output, 50);
322347
$bar->start();
323348
$bar->display();
349+
usleep(self::TEN_MS);
324350
$bar->advance();
351+
usleep(self::TEN_MS);
325352
$bar->advance();
326353

327354
rewind($output->getStream());
@@ -346,8 +373,10 @@ public function testOverwriteMultipleProgressBarsWithSectionOutputs()
346373

347374
$progress->start();
348375
$progress2->start();
376+
usleep(self::TEN_MS);
349377

350378
$progress2->advance();
379+
usleep(self::TEN_MS);
351380
$progress->advance();
352381

353382
rewind($stream->getStream());
@@ -378,8 +407,10 @@ public function testMultipleSectionsWithCustomFormat()
378407

379408
$progress->start();
380409
$progress2->start();
410+
usleep(self::TEN_MS);
381411

382412
$progress->advance();
413+
usleep(self::TEN_MS);
383414
$progress2->advance();
384415

385416
rewind($stream->getStream());
@@ -399,6 +430,7 @@ public function testStartWithMax()
399430
$bar = new ProgressBar($output = $this->getOutputStream());
400431
$bar->setFormat('%current%/%max% [%bar%]');
401432
$bar->start(50);
433+
usleep(self::TEN_MS);
402434
$bar->advance();
403435

404436
rewind($output->getStream());
@@ -414,8 +446,11 @@ public function testSetCurrentProgress()
414446
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
415447
$bar->start();
416448
$bar->display();
449+
usleep(self::TEN_MS);
417450
$bar->advance();
451+
usleep(self::TEN_MS);
418452
$bar->setProgress(15);
453+
usleep(self::TEN_MS);
419454
$bar->setProgress(25);
420455

421456
rewind($output->getStream());
@@ -490,6 +525,7 @@ public function testMultiByteSupport()
490525
{
491526
$bar = new ProgressBar($output = $this->getOutputStream());
492527
$bar->start();
528+
usleep(self::TEN_MS);
493529
$bar->setBarCharacter('');
494530
$bar->advance(3);
495531

@@ -505,6 +541,7 @@ public function testClear()
505541
{
506542
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
507543
$bar->start();
544+
usleep(self::TEN_MS);
508545
$bar->setProgress(25);
509546
$bar->clear();
510547

@@ -521,8 +558,11 @@ public function testPercentNotHundredBeforeComplete()
521558
{
522559
$bar = new ProgressBar($output = $this->getOutputStream(), 200);
523560
$bar->start();
561+
usleep(self::TEN_MS);
524562
$bar->display();
563+
usleep(self::TEN_MS);
525564
$bar->advance(199);
565+
usleep(self::TEN_MS);
526566
$bar->advance();
527567

528568
rewind($output->getStream());
@@ -541,6 +581,7 @@ public function testNonDecoratedOutput()
541581
$bar->start();
542582

543583
for ($i = 0; $i < 200; ++$i) {
584+
usleep(self::TEN_MS / 20);
544585
$bar->advance();
545586
}
546587

@@ -567,9 +608,12 @@ public function testNonDecoratedOutputWithClear()
567608
{
568609
$bar = new ProgressBar($output = $this->getOutputStream(false), 50);
569610
$bar->start();
611+
usleep(self::TEN_MS);
570612
$bar->setProgress(25);
613+
usleep(self::TEN_MS);
571614
$bar->clear();
572615
$bar->setProgress(50);
616+
usleep(self::TEN_MS);
573617
$bar->finish();
574618

575619
rewind($output->getStream());
@@ -585,6 +629,7 @@ public function testNonDecoratedOutputWithoutMax()
585629
{
586630
$bar = new ProgressBar($output = $this->getOutputStream(false));
587631
$bar->start();
632+
usleep(self::TEN_MS);
588633
$bar->advance();
589634

590635
rewind($output->getStream());
@@ -604,21 +649,27 @@ public function testParallelBars()
604649
$bar3 = new ProgressBar($output);
605650

606651
$bar1->start();
652+
usleep(self::TEN_MS);
607653
$output->write("\n");
608654
$bar2->start();
655+
usleep(self::TEN_MS);
609656
$output->write("\n");
610657
$bar3->start();
658+
usleep(self::TEN_MS);
611659

612660
for ($i = 1; $i <= 3; ++$i) {
613661
// up two lines
614662
$output->write("\033[2A");
615663
if ($i <= 2) {
616664
$bar1->advance();
665+
usleep(self::TEN_MS);
617666
}
618667
$output->write("\n");
619668
$bar2->advance();
669+
usleep(self::TEN_MS);
620670
$output->write("\n");
621671
$bar3->advance();
672+
usleep(self::TEN_MS);
622673
}
623674
$output->write("\033[2A");
624675
$output->write("\n");
@@ -660,8 +711,11 @@ public function testWithoutMax()
660711

661712
$bar = new ProgressBar($output);
662713
$bar->start();
714+
usleep(self::TEN_MS);
663715
$bar->advance();
716+
usleep(self::TEN_MS);
664717
$bar->advance();
718+
usleep(self::TEN_MS);
665719
$bar->advance();
666720
$bar->finish();
667721

@@ -681,11 +735,15 @@ public function testSettingMaxStepsDuringProgressing()
681735
$output = $this->getOutputStream();
682736
$bar = new ProgressBar($output);
683737
$bar->start();
738+
usleep(self::TEN_MS);
684739
$bar->setProgress(2);
740+
usleep(self::TEN_MS);
685741
$bar->setMaxSteps(10);
686742
$bar->setProgress(5);
743+
usleep(self::TEN_MS);
687744
$bar->setMaxSteps(100);
688745
$bar->setProgress(10);
746+
usleep(self::TEN_MS);
689747
$bar->finish();
690748

691749
rewind($output->getStream());
@@ -706,6 +764,7 @@ public function testWithSmallScreen()
706764
$bar = new ProgressBar($output);
707765
putenv('COLUMNS=12');
708766
$bar->start();
767+
usleep(self::TEN_MS);
709768
$bar->advance();
710769
putenv('COLUMNS=120');
711770

@@ -726,7 +785,9 @@ public function testAddingPlaceholderFormatter()
726785
$bar->setFormat(' %remaining_steps% [%bar%]');
727786

728787
$bar->start();
788+
usleep(self::TEN_MS);
729789
$bar->advance();
790+
usleep(self::TEN_MS);
730791
$bar->finish();
731792

732793
rewind($output->getStream());
@@ -744,6 +805,7 @@ public function testMultilineFormat()
744805
$bar->setFormat("%bar%\nfoobar");
745806

746807
$bar->start();
808+
usleep(self::TEN_MS);
747809
$bar->advance();
748810
$bar->clear();
749811
$bar->finish();
@@ -789,6 +851,7 @@ public function testAnsiColorsAndEmojis()
789851
rewind($output->getStream());
790852

791853
$bar->setMessage('Looks good to me...', 'title');
854+
usleep(self::TEN_MS);
792855
$bar->advance(4);
793856

794857
rewind($output->getStream());

0 commit comments

Comments
 (0)
0