PhpBench\Extensions\XDebug\Converter\TraceToXmlConverter::convert PHP Method

convert() public method

public convert ( $path )
    public function convert($path)
    {
        $dom = new Document(1.0);
        $traceEl = $dom->createRoot('trace');
        $handle = fopen($path, 'r');
        $version = fgets($handle);
        if (!preg_match('/^Version: (.*)$/', $version, $matches)) {
            throw new \InvalidArgumentException(sprintf('Expected "Version" in trace "%s"', $path));
        }
        $version = $matches[1];
        $traceEl->setAttribute('version', $matches[1]);
        $format = fgets($handle);
        if (!preg_match('/^File format: (.*)$/', $format, $matches)) {
            throw new \InvalidArgumentException(sprintf('Expected "File format" in trace "%s"', $path));
        }
        $traceEl->setAttribute('format', $matches[1]);
        $start = fgets($handle);
        if (!preg_match('/^TRACE START \\[(.*)\\]$/', $start, $matches)) {
            throw new \InvalidArgumentException(sprintf('Expected "TRACE START" in "%s"', $path));
        }
        $traceEl->setAttribute('start', $matches[1]);
        $scopeEl = $traceEl;
        $tree = [];
        $lineNb = 3;
        $buffer = null;
        while ($line = fgets($handle)) {
            $lineNb++;
            if (preg_match('/TRACE END\\s+\\[(.*)\\]/', $line, $matches)) {
                $scopeEl->setAttribute('end-time', $last[3]);
                $scopeEl->setAttribute('end-memory', trim($last[4]));
                $scopeEl->setAttribute('end', $matches[1]);
                break;
            }
            $parts = explode("\t", $line);
            $level = $parts[0];
            // '0' is entry, '1' is exit, 'R' is return
            if (isset($parts[2]) && $parts[2] == '1') {
                $scopeEl = $tree[$level];
                $scopeEl->setAttribute('end-time', $parts[3]);
                $scopeEl->setAttribute('end-memory', trim($parts[4]));
                $scopeEl = $scopeEl->parentNode;
                continue;
            } elseif (isset($parts[2]) && $parts[2] == 'R') {
                $scopeEl = $tree[$level];
                $scopeEl = $scopeEl->parentNode;
                continue;
            } elseif (isset($parts[2]) && $parts[2] == '') {
                $last = $parts;
                continue;
            }
            if (count($parts) < 9) {
                throw new \InvalidArgumentException(sprintf('Expected at least 9 fields, got "%s" in "%s:%s"', count($parts), $path, $lineNb));
            }
            $entryEl = $scopeEl->appendElement('entry');
            $entryEl->setAttribute('level', $parts[0]);
            $entryEl->setAttribute('func_nb', $parts[1]);
            $entryEl->setAttribute('start-time', $parts[3]);
            $entryEl->setAttribute('start-memory', $parts[4]);
            $entryEl->setAttribute('function', $parts[5]);
            $entryEl->setAttribute('is_user', $parts[6]);
            $entryEl->setAttribute('include', $parts[7]);
            $entryEl->setAttribute('filename', $parts[8]);
            $entryEl->setAttribute('line', $parts[9]);
            // parse number of parameters if set.
            if (isset($parts[10])) {
                $entryEl->setAttribute('nb_params', $parts[10]);
            }
            // parse arguments
            $i = 11;
            while (isset($parts[$i])) {
                $argEl = $entryEl->appendElement('arg');
                $argEl->nodeValue = html_entity_decode($parts[$i]);
                $i++;
            }
            $tree[$level] = $entryEl;
            $scopeEl = $entryEl;
        }
        return $dom;
    }

Usage Example

Beispiel #1
0
 /**
  * {@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', 'xdebug.collect_params' => '3'];
     $payload->setPhpConfig($phpConfig);
     $path = $dir . DIRECTORY_SEPARATOR . $name . '.xt';
     // if the file exists, remove it. XDebug might not be installed
     // on the PHP binary and the file may not be generated. We should
     // fail in such a case and not use the file from a previous run.
     if ($this->filesystem->exists($path)) {
         $this->filesystem->remove($path);
     }
     $result = $payload->launch();
     if (false === $this->filesystem->exists($path)) {
         throw new \RuntimeException(sprintf('Trace file at "%s" was not generated.', $path));
     }
     $dom = $this->converter->convert($path);
     $subject = $iteration->getVariant()->getSubject();
     $class = $subject->getBenchmark()->getClass();
     // remove leading slash from class name for matching
     // the class in the trace.
     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('number(%s/@end-time) - number(%s/@start-time)', $selector, $selector)) * 1000000.0);
     $memory = (int) $dom->evaluate(sprintf('number(%s/@end-memory) - number(%s/@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, $dom));
 }
All Usage Examples Of PhpBench\Extensions\XDebug\Converter\TraceToXmlConverter::convert
TraceToXmlConverter