4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Command;
14 use PhpParser\NodeTraverser;
15 use PhpParser\PrettyPrinter\Standard as Printer;
16 use Psy\Command\TimeitCommand\TimeitVisitor;
17 use Psy\Input\CodeArgument;
18 use Psy\ParserFactory;
19 use Symfony\Component\Console\Input\InputInterface;
20 use Symfony\Component\Console\Input\InputOption;
21 use Symfony\Component\Console\Output\OutputInterface;
24 * Class TimeitCommand.
26 class TimeitCommand extends Command
28 const RESULT_MSG = '<info>Command took %.6f seconds to complete.</info>';
29 const AVG_RESULT_MSG = '<info>Command took %.6f seconds on average (%.6f median; %.6f total) to complete.</info>';
31 private static $start = null;
32 private static $times = [];
41 public function __construct($name = null)
43 $parserFactory = new ParserFactory();
44 $this->parser = $parserFactory->createParser();
46 $this->traverser = new NodeTraverser();
47 $this->traverser->addVisitor(new TimeitVisitor());
49 $this->printer = new Printer();
51 parent::__construct($name);
57 protected function configure()
62 new InputOption('num', 'n', InputOption::VALUE_REQUIRED, 'Number of iterations.'),
63 new CodeArgument('code', CodeArgument::REQUIRED, 'Code to execute.'),
65 ->setDescription('Profiles with a timer.')
68 Time profiling for functions and commands.
71 <return>>>> timeit sleep(1)</return>
72 <return>>>> timeit -n1000 $closure()</return>
80 protected function execute(InputInterface $input, OutputInterface $output)
82 $code = $input->getArgument('code');
83 $num = $input->getOption('num') ?: 1;
84 $shell = $this->getApplication();
86 $instrumentedCode = $this->instrumentCode($code);
90 for ($i = 0; $i < $num; $i++) {
91 $_ = $shell->execute($instrumentedCode);
92 $this->ensureEndMarked();
95 $shell->writeReturnValue($_);
97 $times = self::$times;
101 $output->writeln(\sprintf(self::RESULT_MSG, $times[0]));
103 $total = \array_sum($times);
105 $median = $times[\round($num / 2)];
107 $output->writeln(\sprintf(self::AVG_RESULT_MSG, $total / $num, $median, $total));
112 * Internal method for marking the start of timeit execution.
114 * A static call to this method will be injected at the start of the timeit
115 * input code to instrument the call. We will use the saved start time to
116 * more accurately calculate time elapsed during execution.
118 public static function markStart()
120 self::$start = \microtime(true);
124 * Internal method for marking the end of timeit execution.
126 * A static call to this method is injected by TimeitVisitor at the end
127 * of the timeit input code to instrument the call.
129 * Note that this accepts an optional $ret parameter, which is used to pass
130 * the return value of the last statement back out of timeit. This saves us
131 * a bunch of code rewriting shenanigans.
135 * @return mixed it just passes $ret right back
137 public static function markEnd($ret = null)
139 self::$times[] = \microtime(true) - self::$start;
146 * Ensure that the end of code execution was marked.
148 * The end *should* be marked in the instrumented code, but just in case
149 * we'll add a fallback here.
151 private function ensureEndMarked()
153 if (self::$start !== null) {
159 * Instrument code for timeit execution.
161 * This inserts `markStart` and `markEnd` calls to ensure that (reasonably)
162 * accurate times are recorded for just the code being executed.
164 * @param string $code
168 private function instrumentCode($code)
170 return $this->printer->prettyPrint($this->traverser->traverse($this->parse($code)));
174 * Lex and parse a string of code into statements.
176 * @param string $code
178 * @return array Statements
180 private function parse($code)
182 $code = '<?php ' . $code;
185 return $this->parser->parse($code);
186 } catch (\PhpParser\Error $e) {
187 if (\strpos($e->getMessage(), 'unexpected EOF') === false) {
191 // If we got an unexpected EOF, let's try it again with a semicolon.
192 return $this->parser->parse($code . ';');