PhpBench\Benchmark\Remote\Payload::launch PHP Method

launch() public method

public launch ( )
    public function launch()
    {
        if (!file_exists($this->template)) {
            throw new \RuntimeException(sprintf('Could not find script template "%s"', $this->template));
        }
        $tokenSubs = [];
        foreach ($this->tokens as $key => $value) {
            $tokenSubs['{{ ' . $key . ' }}'] = $value;
        }
        $templateBody = file_get_contents($this->template);
        $script = str_replace(array_keys($tokenSubs), array_values($tokenSubs), $templateBody);
        $scriptPath = tempnam(sys_get_temp_dir(), 'PhpBench');
        file_put_contents($scriptPath, $script);
        $wrapper = '';
        if ($this->wrapper) {
            $wrapper = $this->wrapper . ' ';
        }
        $this->process->setCommandLine($wrapper . $this->phpBinary . $this->getIniString() . ' ' . escapeshellarg($scriptPath));
        $this->process->run();
        unlink($scriptPath);
        if (false === $this->process->isSuccessful()) {
            throw new ScriptErrorException(sprintf('%s%s', $this->process->getErrorOutput(), $this->process->getOutput()));
        }
        $output = $this->process->getOutput();
        $result = json_decode($output, true);
        if (null === $result) {
            throw new \RuntimeException(sprintf('Could not decode return value from script from template "%s" (should be a JSON encoded string): %s', $this->template, $output));
        }
        return $result;
    }

Usage Example

 /**
  * {@inheritdoc}
  */
 public function launch(Payload $payload, Iteration $iteration, Config $config)
 {
     $name = XDebugUtil::filenameFromIteration($iteration);
     $dir = $config['output_dir'];
     $phpConfig = ['xdebug.trace_output_name' => $name, 'xdebug.trace_output_dir' => $dir, 'xdebug.trace_format' => '1', 'xdebug.auto_trace' => '1', 'xdebug.coverage_enable' => '0'];
     $payload->setPhpConfig($phpConfig);
     $path = $dir . DIRECTORY_SEPARATOR . $name . '.xt';
     $result = $payload->launch();
     if (false === $this->filesystem->exists($path)) {
         throw new \RuntimeException(sprintf('Trace file at "%s" was not generated. Is XDebug enabled in the benchmarking environment', $path));
     }
     $dom = $this->converter->convert($path);
     $subject = $iteration->getVariant()->getSubject();
     $class = $subject->getBenchmark()->getClass();
     if (substr($class, 0, 1) == '\\') {
         $class = substr($class, 1);
     }
     // extract only the timings for the benchmark class, ignore the bootstrapping
     $selector = '//entry[@function="' . $class . '->' . $subject->getName() . '"]';
     // calculate stats from the trace
     $time = (int) ($dom->evaluate(sprintf('sum(%s/@end-time) - sum(/@start-time)', $selector, $selector)) * 1000000.0);
     $memory = (int) $dom->evaluate(sprintf('sum(%s/@end-memory) - sum(/@start-memory)', $selector, $selector));
     $funcCalls = (int) $dom->evaluate('count(' . $selector . '//*)');
     $iteration->setResult(new TimeResult($result['time']));
     $iteration->setResult(MemoryResult::fromArray($result['mem']));
     $iteration->setResult(new XDebugTraceResult($time, $memory, $funcCalls));
 }
All Usage Examples Of PhpBench\Benchmark\Remote\Payload::launch