annotate vendor/psy/psysh/src/Command/TimeitCommand.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of Psy Shell.
Chris@0 5 *
Chris@0 6 * (c) 2012-2018 Justin Hileman
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Psy\Command;
Chris@0 13
Chris@0 14 use PhpParser\NodeTraverser;
Chris@0 15 use PhpParser\PrettyPrinter\Standard as Printer;
Chris@0 16 use Psy\Command\TimeitCommand\TimeitVisitor;
Chris@0 17 use Psy\Input\CodeArgument;
Chris@0 18 use Psy\ParserFactory;
Chris@0 19 use Psy\Shell;
Chris@0 20 use Symfony\Component\Console\Input\InputInterface;
Chris@0 21 use Symfony\Component\Console\Input\InputOption;
Chris@0 22 use Symfony\Component\Console\Output\OutputInterface;
Chris@0 23
Chris@0 24 /**
Chris@0 25 * Class TimeitCommand.
Chris@0 26 */
Chris@0 27 class TimeitCommand extends Command
Chris@0 28 {
Chris@0 29 const RESULT_MSG = '<info>Command took %.6f seconds to complete.</info>';
Chris@0 30 const AVG_RESULT_MSG = '<info>Command took %.6f seconds on average (%.6f median; %.6f total) to complete.</info>';
Chris@0 31
Chris@0 32 private static $start = null;
Chris@0 33 private static $times = [];
Chris@0 34
Chris@0 35 private $parser;
Chris@0 36 private $traverser;
Chris@0 37 private $printer;
Chris@0 38
Chris@0 39 /**
Chris@0 40 * {@inheritdoc}
Chris@0 41 */
Chris@0 42 public function __construct($name = null)
Chris@0 43 {
Chris@0 44 $parserFactory = new ParserFactory();
Chris@0 45 $this->parser = $parserFactory->createParser();
Chris@0 46
Chris@0 47 $this->traverser = new NodeTraverser();
Chris@0 48 $this->traverser->addVisitor(new TimeitVisitor());
Chris@0 49
Chris@0 50 $this->printer = new Printer();
Chris@0 51
Chris@0 52 parent::__construct($name);
Chris@0 53 }
Chris@0 54
Chris@0 55 /**
Chris@0 56 * {@inheritdoc}
Chris@0 57 */
Chris@0 58 protected function configure()
Chris@0 59 {
Chris@0 60 $this
Chris@0 61 ->setName('timeit')
Chris@0 62 ->setDefinition([
Chris@0 63 new InputOption('num', 'n', InputOption::VALUE_REQUIRED, 'Number of iterations.'),
Chris@0 64 new CodeArgument('code', CodeArgument::REQUIRED, 'Code to execute.'),
Chris@0 65 ])
Chris@0 66 ->setDescription('Profiles with a timer.')
Chris@0 67 ->setHelp(
Chris@0 68 <<<'HELP'
Chris@0 69 Time profiling for functions and commands.
Chris@0 70
Chris@0 71 e.g.
Chris@0 72 <return>>>> timeit sleep(1)</return>
Chris@0 73 <return>>>> timeit -n1000 $closure()</return>
Chris@0 74 HELP
Chris@0 75 );
Chris@0 76 }
Chris@0 77
Chris@0 78 /**
Chris@0 79 * {@inheritdoc}
Chris@0 80 */
Chris@0 81 protected function execute(InputInterface $input, OutputInterface $output)
Chris@0 82 {
Chris@0 83 $code = $input->getArgument('code');
Chris@0 84 $num = $input->getOption('num') ?: 1;
Chris@0 85 $shell = $this->getApplication();
Chris@0 86
Chris@0 87 $instrumentedCode = $this->instrumentCode($code);
Chris@0 88
Chris@0 89 self::$times = [];
Chris@0 90
Chris@0 91 for ($i = 0; $i < $num; $i++) {
Chris@0 92 $_ = $shell->execute($instrumentedCode);
Chris@0 93 $this->ensureEndMarked();
Chris@0 94 }
Chris@0 95
Chris@0 96 $shell->writeReturnValue($_);
Chris@0 97
Chris@0 98 $times = self::$times;
Chris@0 99 self::$times = [];
Chris@0 100
Chris@0 101 if ($num === 1) {
Chris@0 102 $output->writeln(sprintf(self::RESULT_MSG, $times[0]));
Chris@0 103 } else {
Chris@0 104 $total = array_sum($times);
Chris@0 105 rsort($times);
Chris@0 106 $median = $times[round($num / 2)];
Chris@0 107
Chris@0 108 $output->writeln(sprintf(self::AVG_RESULT_MSG, $total / $num, $median, $total));
Chris@0 109 }
Chris@0 110 }
Chris@0 111
Chris@0 112 /**
Chris@0 113 * Internal method for marking the start of timeit execution.
Chris@0 114 *
Chris@0 115 * A static call to this method will be injected at the start of the timeit
Chris@0 116 * input code to instrument the call. We will use the saved start time to
Chris@0 117 * more accurately calculate time elapsed during execution.
Chris@0 118 */
Chris@0 119 public static function markStart()
Chris@0 120 {
Chris@0 121 self::$start = microtime(true);
Chris@0 122 }
Chris@0 123
Chris@0 124 /**
Chris@0 125 * Internal method for marking the end of timeit execution.
Chris@0 126 *
Chris@0 127 * A static call to this method is injected by TimeitVisitor at the end
Chris@0 128 * of the timeit input code to instrument the call.
Chris@0 129 *
Chris@0 130 * Note that this accepts an optional $ret parameter, which is used to pass
Chris@0 131 * the return value of the last statement back out of timeit. This saves us
Chris@0 132 * a bunch of code rewriting shenanigans.
Chris@0 133 *
Chris@0 134 * @param mixed $ret
Chris@0 135 *
Chris@0 136 * @return mixed it just passes $ret right back
Chris@0 137 */
Chris@0 138 public static function markEnd($ret = null)
Chris@0 139 {
Chris@0 140 self::$times[] = microtime(true) - self::$start;
Chris@0 141 self::$start = null;
Chris@0 142
Chris@0 143 return $ret;
Chris@0 144 }
Chris@0 145
Chris@0 146 /**
Chris@0 147 * Ensure that the end of code execution was marked.
Chris@0 148 *
Chris@0 149 * The end *should* be marked in the instrumented code, but just in case
Chris@0 150 * we'll add a fallback here.
Chris@0 151 */
Chris@0 152 private function ensureEndMarked()
Chris@0 153 {
Chris@0 154 if (self::$start !== null) {
Chris@0 155 self::markEnd();
Chris@0 156 }
Chris@0 157 }
Chris@0 158
Chris@0 159 /**
Chris@0 160 * Instrument code for timeit execution.
Chris@0 161 *
Chris@0 162 * This inserts `markStart` and `markEnd` calls to ensure that (reasonably)
Chris@0 163 * accurate times are recorded for just the code being executed.
Chris@0 164 *
Chris@0 165 * @param string $code
Chris@0 166 *
Chris@0 167 * @return string
Chris@0 168 */
Chris@0 169 private function instrumentCode($code)
Chris@0 170 {
Chris@0 171 return $this->printer->prettyPrint($this->traverser->traverse($this->parse($code)));
Chris@0 172 }
Chris@0 173
Chris@0 174 /**
Chris@0 175 * Lex and parse a string of code into statements.
Chris@0 176 *
Chris@0 177 * @param string $code
Chris@0 178 *
Chris@0 179 * @return array Statements
Chris@0 180 */
Chris@0 181 private function parse($code)
Chris@0 182 {
Chris@0 183 $code = '<?php ' . $code;
Chris@0 184
Chris@0 185 try {
Chris@0 186 return $this->parser->parse($code);
Chris@0 187 } catch (\PhpParser\Error $e) {
Chris@0 188 if (strpos($e->getMessage(), 'unexpected EOF') === false) {
Chris@0 189 throw $e;
Chris@0 190 }
Chris@0 191
Chris@0 192 // If we got an unexpected EOF, let's try it again with a semicolon.
Chris@0 193 return $this->parser->parse($code . ';');
Chris@0 194 }
Chris@0 195 }
Chris@0 196 }