Skip to content
Permalink
Browse files

Rate limit the progress bar updates (#1871)

* Rate limit the progress bar updates

* Fix psalm warning

* Made lines shorter
  • Loading branch information...
TysonAndre authored and muglug committed Jun 28, 2019
1 parent 220513c commit cba187ad5601bb985091d891eea531c129468e8c
Showing with 23 additions and 0 deletions.
  1. +23 −0 src/Psalm/Progress/DefaultProgress.php
@@ -8,11 +8,34 @@ class DefaultProgress extends LongProgress
{
const TOO_MANY_FILES = 1500;
// Update the progress bar at most once per 0.1 seconds.
// This reduces flickering and reduces the amount of time spent writing to STDERR and updating the terminal.
const PROGRESS_BAR_SAMPLE_INTERVAL = 0.1;
/** @var float the last time when the progress bar UI was updated */
private $previous_update_time = 0.0;
public function taskDone(int $level): void
{
if ($this->number_of_tasks > self::TOO_MANY_FILES) {
++$this->progress;
// Source for rate limiting:
// https://github.com/phan/phan/blob/9a788581ee1a4e1c35bebf89c435fd8a238c1d17/src/Phan/CLI.php
$time = \microtime(true);
// If not enough time has elapsed, then don't update the progress bar.
// Making the update frequency based on time (instead of the number of files)
// prevents the terminal from rapidly flickering while processing small/empty files,
// and reduces the time spent writing to stderr.
if ($time - $this->previous_update_time < self::PROGRESS_BAR_SAMPLE_INTERVAL) {
// Make sure to output the section for 100% completion regardless of limits, to avoid confusion.
if ($this->progress !== $this->number_of_tasks) {
return;
}
}
$this->previous_update_time = $time;
$inner_progress = self::renderInnerProgressBar(
self::NUMBER_OF_COLUMNS,
$this->progress / $this->number_of_tasks

0 comments on commit cba187a

Please sign in to comment.
You can’t perform that action at this time.