Guest User

Untitled

a guest
Apr 25th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace AppBundle\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. class ProgressBar
  13. {
  14. /** @var OutputInterface */
  15. private $output;
  16. /** @var SymfonyProgressBar */
  17. private $progressBar;
  18.  
  19. const INTERVAL_MONITORING = [1, 10, 20];
  20.  
  21. private $timelineMemory = [];
  22. private $intervalMemory = [-1, -1, -1];
  23. private $maxMemory;
  24.  
  25. private $timelineItemPerSecond = [];
  26. private $intervalItemPerSecond = [-1, -1, -1];
  27. private $minItemPerSecond;
  28.  
  29. private $lastTimeUpdated;
  30. private $previousItemCount = 0;
  31.  
  32. public function setOutput($output)
  33. {
  34. $this->output = $output;
  35. }
  36.  
  37. private function init()
  38. {
  39. $this->timelineMemory = [];
  40. $this->intervalMemory = [-1, -1, -1];
  41.  
  42. $this->timelineItemPerSecond = [];
  43. $this->intervalItemPerSecond = [-1, -1, -1];
  44.  
  45.  
  46. $this->lastTimeUpdated = time();
  47. $this->previousItemCount = 0;
  48. }
  49.  
  50. public function create($size, $title = 'Progression', $maxMemory = 30, $minItemPerSecond = 50)
  51. {
  52. $this->init();
  53. $this->maxMemory = $maxMemory;
  54. $this->minItemPerSecond = $minItemPerSecond;
  55.  
  56. $this->progressBar =
  57. ($this->output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL)
  58. ? new SymfonyProgressBar(new NullOutput(), $size)
  59. : new SymfonyProgressBar($this->output, $size);
  60. SymfonyProgressBar::setPlaceholderFormatterDefinition('memory', [$this, 'renderRightbar']);
  61. $this->progressBar->setFormat(" \033[44;37m %title:-37s% \033[0m\n %current%/%max% %bar% %percent:3s%%\n 🏁 %remaining:-10s% %memory:37s%");
  62. $this->progressBar->setBarCharacter($done = "\033[32m=\033[0m");
  63. $this->progressBar->setEmptyBarCharacter($empty = "\033[31m \033[0m");
  64. $this->progressBar->setProgressCharacter($progress = "\033[32m>\033[0m");
  65. $this->progressBar->setBarWidth(400);
  66. $this->progressBar->setMessage($title, 'title');
  67. $this->progressBar->start();
  68. }
  69.  
  70. private function isUpdate()
  71. {
  72. $time = time();
  73.  
  74. if ($time > $this->lastTimeUpdated) {
  75. $this->lastTimeUpdated = $time;
  76.  
  77. return true;
  78. }
  79.  
  80. return false;
  81. }
  82.  
  83. private function updateMemory()
  84. {
  85. $time = time();
  86.  
  87. if (count($this->timelineMemory) > self::INTERVAL_MONITORING[2]) {
  88. $this->timelineMemory = array_slice($this->timelineMemory, 1);
  89. }
  90.  
  91. $this->timelineMemory[$time] = $now = memory_get_usage(true);
  92.  
  93. $this->intervalMemory[0] = $this->formatMemory(
  94. $this->timelineMemory[$time - self::INTERVAL_MONITORING[0]] ?? $now,
  95. self::INTERVAL_MONITORING[0]
  96. );
  97. $this->intervalMemory[1] = $this->formatMemory(
  98. $this->timelineMemory[$time - self::INTERVAL_MONITORING[1]] ?? -1,
  99. self::INTERVAL_MONITORING[1]
  100. );
  101. $this->intervalMemory[2] = $this->formatMemory(
  102. $this->timelineMemory[$time - self::INTERVAL_MONITORING[2]] ?? -1,
  103. self::INTERVAL_MONITORING[2]
  104. );
  105. }
  106.  
  107. private function updateItemPerSecond()
  108. {
  109. $time = time();
  110.  
  111. if (count($this->timelineItemPerSecond) > self::INTERVAL_MONITORING[2]) {
  112. $this->timelineItemPerSecond = array_slice($this->timelineItemPerSecond, 1);
  113. }
  114.  
  115. $this->timelineItemPerSecond[$time] = $now = $this->progressBar->getProgress() - $this->previousItemCount;
  116. $this->previousItemCount = $this->progressBar->getProgress();
  117.  
  118. $this->intervalItemPerSecond[0] = $this->formatItemsCount(
  119. $this->timelineItemPerSecond[$time - self::INTERVAL_MONITORING[0]] ?? $now,
  120. self::INTERVAL_MONITORING[0]
  121. );
  122. $this->intervalItemPerSecond[1] = $this->formatItemsCount(
  123. $this->timelineItemPerSecond[$time - self::INTERVAL_MONITORING[1]] ?? -1,
  124. self::INTERVAL_MONITORING[1]
  125. );
  126. $this->intervalItemPerSecond[2] = $this->formatItemsCount(
  127. $this->timelineItemPerSecond[$time - self::INTERVAL_MONITORING[2]] ?? -1,
  128. self::INTERVAL_MONITORING[2]
  129. );
  130. }
  131.  
  132. public function renderRightbar(SymfonyProgressBar $bar)
  133. {
  134. return sprintf(
  135. 'MEMORY %s / ITEMS %s',
  136. implode(' ', $this->intervalMemory),
  137. implode(' ', $this->intervalItemPerSecond)
  138. );
  139. }
  140.  
  141. private function formatMemory($memory, $time)
  142. {
  143. $colors = (($memory/1000) > $this->maxMemory * 1000) ? '41;37' : '44;37';
  144. $message = $time.'s = '.Helper::formatMemory($memory);
  145. return "\033[".$colors.'m '.$message." \033[0m";
  146. }
  147.  
  148. private function formatItemsCount($itemsCount, $time)
  149. {
  150. $colors = ($itemsCount < $this->minItemPerSecond) ? '41;37' : '44;37';
  151. $message = $time.'s = '.$itemsCount;
  152. return "\033[".$colors.'m '.$message."/s \033[0m";
  153. }
  154.  
  155. public function setProgress($progress)
  156. {
  157. if ($this->isUpdate()) {
  158. $this->updateMemory();
  159. $this->updateItemPerSecond();
  160. }
  161.  
  162. $this->progressBar->setProgress($progress);
  163. }
  164.  
  165. public function advance()
  166. {
  167. $this->progressBar->advance();
  168. }
  169.  
  170. public function finish()
  171. {
  172. $this->progressBar->finish();
  173.  
  174. $this->itemCount = [];
  175.  
  176. }
  177. }
Add Comment
Please, Sign In to add comment