annotate vendor/psy/psysh/src/Command/TimeitCommand.php @ 5:12f9dff5fda9 tip

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