|
8 | 8 |
|
9 | 9 | namespace Inhere\Console\Components; |
10 | 10 |
|
| 11 | +use Inhere\Console\Utils\CliUtil; |
| 12 | + |
11 | 13 | /** |
12 | 14 | * Class ExecComparator - PHP code exec speed comparator |
13 | 15 | * @package Inhere\Console\Components |
14 | 16 | */ |
15 | 17 | class ExecComparator |
16 | 18 | { |
| 19 | + /** |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + public $tmpDir; |
| 23 | + |
17 | 24 | /** |
18 | 25 | * @var array |
19 | 26 | */ |
20 | 27 | private $vars = []; |
21 | 28 |
|
22 | | - public function compare($code1, $code2, $loops = 10000) |
| 29 | + /** @var string[] */ |
| 30 | + private $sample1; |
| 31 | + |
| 32 | + /** @var string[] */ |
| 33 | + private $sample2; |
| 34 | + |
| 35 | + /** @var string */ |
| 36 | + private $common; |
| 37 | + |
| 38 | + /** @var int */ |
| 39 | + private $loops = 0; |
| 40 | + |
| 41 | + /** @var string */ |
| 42 | + private $time; |
| 43 | + |
| 44 | + /** |
| 45 | + * ExecComparator constructor. |
| 46 | + * @param string|null $tmpDir |
| 47 | + */ |
| 48 | + public function __construct(string $tmpDir = null) |
| 49 | + { |
| 50 | + $this->tmpDir = $tmpDir ?? CliUtil::getTempDir(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param string $code |
| 55 | + * @return $this |
| 56 | + */ |
| 57 | + public function setCommon(string $code) |
| 58 | + { |
| 59 | + $this->common = $code; |
| 60 | + |
| 61 | + return $this; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param int $times |
| 66 | + * @return $this |
| 67 | + */ |
| 68 | + public function setLoops(int $times) |
| 69 | + { |
| 70 | + if ($times <= 0) { |
| 71 | + throw new \InvalidArgumentException('The time must be gt zero'); |
| 72 | + } |
| 73 | + |
| 74 | + $this->loops = $times; |
| 75 | + |
| 76 | + return $this; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param string $code |
| 81 | + * @return $this |
| 82 | + */ |
| 83 | + public function setSample1(string $code) |
| 84 | + { |
| 85 | + $this->sample1['code'] = $code; |
| 86 | + |
| 87 | + return $this; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param string $code |
| 92 | + * @return $this |
| 93 | + */ |
| 94 | + public function setSample2(string $code) |
| 95 | + { |
| 96 | + $this->sample2['code'] = $code; |
| 97 | + |
| 98 | + return $this; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @param array $context |
| 103 | + * @param int $loops |
| 104 | + * @return array |
| 105 | + */ |
| 106 | + public function compare(array $context = [], int $loops = 0) |
| 107 | + { |
| 108 | + if ($loops) { |
| 109 | + $this->setLoops($loops); |
| 110 | + } |
| 111 | + |
| 112 | + $sTime = microtime(1); |
| 113 | + $this->time = date('ymdH'); |
| 114 | + |
| 115 | + $id = 1; |
| 116 | + $file1 = $this->dump($this->sample1['code'], $id); |
| 117 | + $info1 = $this->runSampleFile($file1, $id); |
| 118 | + |
| 119 | + $id = 2; |
| 120 | + $file2 = $this->dump($this->sample2['code'], $id); |
| 121 | + $info2 = $this->runSampleFile($file2, $id); |
| 122 | + $eTime = microtime(1); |
| 123 | + |
| 124 | + return [ |
| 125 | + $info1, |
| 126 | + $info2, |
| 127 | + 'total' => [ |
| 128 | + 'startTime' => $sTime, |
| 129 | + 'endTime' => $eTime, |
| 130 | + ] |
| 131 | + ]; |
| 132 | + } |
| 133 | + |
| 134 | + public function runSampleFile(string $file, int $id) |
23 | 135 | { |
| 136 | + $func = 'sample_func_' . $id; |
| 137 | + $sMem = memory_get_usage(); |
| 138 | + $sTime = microtime(1); |
| 139 | + |
| 140 | + // load and running |
| 141 | + ob_start(); |
| 142 | + require $file; |
| 143 | + $ret = $func(); |
| 144 | + $out = ob_get_clean(); |
| 145 | + |
| 146 | + $eMem = memory_get_usage(); |
| 147 | + $eTime = microtime(1); |
| 148 | + |
| 149 | + return [ |
| 150 | + 'startTime' => $sTime, |
| 151 | + 'endTime' => $eTime, |
| 152 | + 'startMem' => $sMem, |
| 153 | + 'endMem' => $eMem, |
| 154 | + 'output' => $out, |
| 155 | + 'return' => $ret, |
| 156 | + ]; |
| 157 | + } |
| 158 | + |
| 159 | + public function dump(string $code, int $id, array $context = []) |
| 160 | + { |
| 161 | + $file = $this->tmpDir . '/' . $this->time . '_' . md5($code . random_int(1000, 100000)) . '.php'; |
| 162 | + $common = $this->common; |
| 163 | + |
| 164 | + $content = <<<CODE |
| 165 | +function sample_func_{$id}() { |
| 166 | + // prepare |
| 167 | +$common |
| 168 | +
|
| 169 | + // exec |
| 170 | + for (\$i = 0; \$i < $this->loops; \$i++) { |
| 171 | + $code |
| 172 | + } |
| 173 | +} |
| 174 | +CODE; |
| 175 | + |
| 176 | + file_put_contents($file, '<?php' . PHP_EOL . $content); |
24 | 177 |
|
| 178 | + return $file; |
25 | 179 | } |
26 | 180 |
|
27 | 181 | /** |
|
0 commit comments