| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of Twig. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
namespace ElementorDeps\Twig\Profiler\Dumper; |
| 12 |
|
| 13 |
use ElementorDeps\Twig\Profiler\Profile; |
| 14 |
/** |
| 15 |
* @author Fabien Potencier <[email protected]> |
| 16 |
*/ |
| 17 |
final class BlackfireDumper |
| 18 |
{ |
| 19 |
public function dump(Profile $profile) : string |
| 20 |
{ |
| 21 |
$data = []; |
| 22 |
$this->dumpProfile('main()', $profile, $data); |
| 23 |
$this->dumpChildren('main()', $profile, $data); |
| 24 |
$start = \sprintf('%f', \microtime(\true)); |
| 25 |
$str = <<<EOF |
| 26 |
file-format: BlackfireProbe |
| 27 |
cost-dimensions: wt mu pmu |
| 28 |
request-start: {$start} |
| 29 |
|
| 30 |
|
| 31 |
EOF; |
| 32 |
foreach ($data as $name => $values) { |
| 33 |
$str .= "{$name}//{$values['ct']} {$values['wt']} {$values['mu']} {$values['pmu']}\n"; |
| 34 |
} |
| 35 |
return $str; |
| 36 |
} |
| 37 |
private function dumpChildren(string $parent, Profile $profile, &$data) |
| 38 |
{ |
| 39 |
foreach ($profile as $p) { |
| 40 |
if ($p->isTemplate()) { |
| 41 |
$name = $p->getTemplate(); |
| 42 |
} else { |
| 43 |
$name = \sprintf('%s::%s(%s)', $p->getTemplate(), $p->getType(), $p->getName()); |
| 44 |
} |
| 45 |
$this->dumpProfile(\sprintf('%s==>%s', $parent, $name), $p, $data); |
| 46 |
$this->dumpChildren($name, $p, $data); |
| 47 |
} |
| 48 |
} |
| 49 |
private function dumpProfile(string $edge, Profile $profile, &$data) |
| 50 |
{ |
| 51 |
if (isset($data[$edge])) { |
| 52 |
++$data[$edge]['ct']; |
| 53 |
$data[$edge]['wt'] += \floor($profile->getDuration() * 1000000); |
| 54 |
$data[$edge]['mu'] += $profile->getMemoryUsage(); |
| 55 |
$data[$edge]['pmu'] += $profile->getPeakMemoryUsage(); |
| 56 |
} else { |
| 57 |
$data[$edge] = ['ct' => 1, 'wt' => \floor($profile->getDuration() * 1000000), 'mu' => $profile->getMemoryUsage(), 'pmu' => $profile->getPeakMemoryUsage()]; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|