TimeitCommand.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2023 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy\Command;
  11. use PhpParser\NodeTraverser;
  12. use PhpParser\PrettyPrinter\Standard as Printer;
  13. use Psy\Command\TimeitCommand\TimeitVisitor;
  14. use Psy\Input\CodeArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. /**
  19. * Class TimeitCommand.
  20. */
  21. class TimeitCommand extends Command
  22. {
  23. const RESULT_MSG = '<info>Command took %.6f seconds to complete.</info>';
  24. const AVG_RESULT_MSG = '<info>Command took %.6f seconds on average (%.6f median; %.6f total) to complete.</info>';
  25. // All times stored as nanoseconds!
  26. private static $start = null;
  27. private static $times = [];
  28. private $parser;
  29. private $traverser;
  30. private $printer;
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function __construct($name = null)
  35. {
  36. $this->parser = new CodeArgumentParser();
  37. $this->traverser = new NodeTraverser();
  38. $this->traverser->addVisitor(new TimeitVisitor());
  39. $this->printer = new Printer();
  40. parent::__construct($name);
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. protected function configure()
  46. {
  47. $this
  48. ->setName('timeit')
  49. ->setDefinition([
  50. new InputOption('num', 'n', InputOption::VALUE_REQUIRED, 'Number of iterations.'),
  51. new CodeArgument('code', CodeArgument::REQUIRED, 'Code to execute.'),
  52. ])
  53. ->setDescription('Profiles with a timer.')
  54. ->setHelp(
  55. <<<'HELP'
  56. Time profiling for functions and commands.
  57. e.g.
  58. <return>>>> timeit sleep(1)</return>
  59. <return>>>> timeit -n1000 $closure()</return>
  60. HELP
  61. );
  62. }
  63. /**
  64. * {@inheritdoc}
  65. *
  66. * @return int 0 if everything went fine, or an exit code
  67. */
  68. protected function execute(InputInterface $input, OutputInterface $output): int
  69. {
  70. $code = $input->getArgument('code');
  71. $num = (int) ($input->getOption('num') ?: 1);
  72. $shell = $this->getApplication();
  73. $instrumentedCode = $this->instrumentCode($code);
  74. self::$times = [];
  75. do {
  76. $_ = $shell->execute($instrumentedCode);
  77. $this->ensureEndMarked();
  78. } while (\count(self::$times) < $num);
  79. $shell->writeReturnValue($_);
  80. $times = self::$times;
  81. self::$times = [];
  82. if ($num === 1) {
  83. $output->writeln(\sprintf(self::RESULT_MSG, $times[0] / 1e+9));
  84. } else {
  85. $total = \array_sum($times);
  86. \rsort($times);
  87. $median = $times[\round($num / 2)];
  88. $output->writeln(\sprintf(self::AVG_RESULT_MSG, ($total / $num) / 1e+9, $median / 1e+9, $total / 1e+9));
  89. }
  90. return 0;
  91. }
  92. /**
  93. * Internal method for marking the start of timeit execution.
  94. *
  95. * A static call to this method will be injected at the start of the timeit
  96. * input code to instrument the call. We will use the saved start time to
  97. * more accurately calculate time elapsed during execution.
  98. */
  99. public static function markStart()
  100. {
  101. self::$start = \hrtime(true);
  102. }
  103. /**
  104. * Internal method for marking the end of timeit execution.
  105. *
  106. * A static call to this method is injected by TimeitVisitor at the end
  107. * of the timeit input code to instrument the call.
  108. *
  109. * Note that this accepts an optional $ret parameter, which is used to pass
  110. * the return value of the last statement back out of timeit. This saves us
  111. * a bunch of code rewriting shenanigans.
  112. *
  113. * @param mixed $ret
  114. *
  115. * @return mixed it just passes $ret right back
  116. */
  117. public static function markEnd($ret = null)
  118. {
  119. self::$times[] = \hrtime(true) - self::$start;
  120. self::$start = null;
  121. return $ret;
  122. }
  123. /**
  124. * Ensure that the end of code execution was marked.
  125. *
  126. * The end *should* be marked in the instrumented code, but just in case
  127. * we'll add a fallback here.
  128. */
  129. private function ensureEndMarked()
  130. {
  131. if (self::$start !== null) {
  132. self::markEnd();
  133. }
  134. }
  135. /**
  136. * Instrument code for timeit execution.
  137. *
  138. * This inserts `markStart` and `markEnd` calls to ensure that (reasonably)
  139. * accurate times are recorded for just the code being executed.
  140. */
  141. private function instrumentCode(string $code): string
  142. {
  143. return $this->printer->prettyPrint($this->traverser->traverse($this->parser->parse($code)));
  144. }
  145. }