|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpBench\Executor\Benchmark; |
| 4 | + |
| 5 | +use PhpBench\Compat\SymfonyOptionsResolverCompat; |
| 6 | +use PhpBench\Executor\BenchmarkExecutorInterface; |
| 7 | +use PhpBench\Executor\Exception\ExecutionError; |
| 8 | +use PhpBench\Executor\ExecutionContext; |
| 9 | +use PhpBench\Executor\ExecutionResults; |
| 10 | +use PhpBench\Model\Result\MemoryResult; |
| 11 | +use PhpBench\Model\Result\OpcodeResult; |
| 12 | +use PhpBench\Model\Result\TimeResult; |
| 13 | +use PhpBench\Opcache\OpcodeDebugParser; |
| 14 | +use PhpBench\Registry\Config; |
| 15 | +use PhpBench\Remote\Exception\ScriptErrorException; |
| 16 | +use PhpBench\Remote\Launcher; |
| 17 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 18 | + |
| 19 | +class OpcodeExecutor implements BenchmarkExecutorInterface |
| 20 | +{ |
| 21 | + public const OPTION_PHP_CONFIG = 'php_config'; |
| 22 | + public const OPTION_SAFE_PARAMETERS = 'safe_parameters'; |
| 23 | + |
| 24 | + private const PHP_OPTION_MAX_EXECUTION_TIME = 'max_execution_time'; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var Launcher |
| 28 | + */ |
| 29 | + private $launcher; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + private $templatePath; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var OpcodeDebugParser |
| 38 | + */ |
| 39 | + private $parser; |
| 40 | + |
| 41 | + public function __construct( |
| 42 | + Launcher $launcher, |
| 43 | + OpcodeDebugParser $parser |
| 44 | + ) |
| 45 | + { |
| 46 | + $this->launcher = $launcher; |
| 47 | + $this->parser = $parser; |
| 48 | + } |
| 49 | + |
| 50 | + public function execute(ExecutionContext $context, Config $config): ExecutionResults |
| 51 | + { |
| 52 | + $tokens = $this->createTokens($context, $config); |
| 53 | + $payload = $this->launcher->payload( |
| 54 | + __DIR__ . '/template/remote.template', |
| 55 | + $tokens, |
| 56 | + $context->getTimeout() |
| 57 | + ); |
| 58 | + $payload->mergePhpConfig(array_merge( |
| 59 | + [ |
| 60 | + self::PHP_OPTION_MAX_EXECUTION_TIME => 0, |
| 61 | + ], |
| 62 | + [ |
| 63 | + 'opcache.enable_cli' => 1, |
| 64 | + 'opcache.opt_debug_level' => '0x10000', |
| 65 | + ], |
| 66 | + $config[self::OPTION_PHP_CONFIG] ?? [], |
| 67 | + )); |
| 68 | + |
| 69 | + |
| 70 | + try { |
| 71 | + $result = $payload->launchResult(); |
| 72 | + } catch (ScriptErrorException $error) { |
| 73 | + throw new ExecutionError($error->getMessage(), 0, $error); |
| 74 | + } |
| 75 | + |
| 76 | + $data = $result->unserializeResult(); |
| 77 | + file_put_contents('example', $result->stderr()); |
| 78 | + |
| 79 | + return ExecutionResults::fromResults( |
| 80 | + new OpcodeResult($this->parser->countOpcodes($result->stderr())), |
| 81 | + TimeResult::fromArray($data['time']), |
| 82 | + MemoryResult::fromArray($data['mem']) |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * {@inheritdoc} |
| 88 | + */ |
| 89 | + public function configure(OptionsResolver $options): void |
| 90 | + { |
| 91 | + $options->setDefaults([ |
| 92 | + self::OPTION_PHP_CONFIG => [ |
| 93 | + ], |
| 94 | + self::OPTION_SAFE_PARAMETERS => false, |
| 95 | + ]); |
| 96 | + $options->setAllowedTypes(self::OPTION_PHP_CONFIG, ['array']); |
| 97 | + $options->setAllowedTypes(self::OPTION_SAFE_PARAMETERS, ['bool']); |
| 98 | + SymfonyOptionsResolverCompat::setInfos($options, [ |
| 99 | + self::OPTION_PHP_CONFIG => 'Key value array of ini settings, e.g. ``{"max_execution_time":100}``', |
| 100 | + self::OPTION_SAFE_PARAMETERS => 'INTERNAL: Use process process-safe parameters, this option exists for backwards-compatibility and will be removed in PHPBench 2.0' |
| 101 | + ]); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @return array<string,mixed> |
| 106 | + */ |
| 107 | + protected function createTokens(ExecutionContext $context, Config $config) : array |
| 108 | + { |
| 109 | + return [ |
| 110 | + 'class' => $context->getClassName(), |
| 111 | + 'file' => $context->getClassPath(), |
| 112 | + 'subject' => $context->getMethodName(), |
| 113 | + 'revolutions' => $context->getRevolutions(), |
| 114 | + 'beforeMethods' => var_export($context->getBeforeMethods(), true), |
| 115 | + 'afterMethods' => var_export($context->getAfterMethods(), true), |
| 116 | + 'parameters' => var_export($this->resolveParameterSet($context, $config), true), |
| 117 | + 'warmup' => $context->getWarmup() ?: 0, |
| 118 | + ]; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return array<string,mixed> |
| 123 | + */ |
| 124 | + private function resolveParameterSet(ExecutionContext $context, Config $config): array |
| 125 | + { |
| 126 | + if (isset($config[self::OPTION_SAFE_PARAMETERS]) && $config[self::OPTION_SAFE_PARAMETERS]) { |
| 127 | + return $context->getParameterSet()->toSerializedParameters(); |
| 128 | + } |
| 129 | + |
| 130 | + return $context->getParameterSet()->toUnserializedParameters(); |
| 131 | + } |
| 132 | +} |
0 commit comments