blob: 03abe0fa071b107fd58ddad5fe85c7f6baf0d70a [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001<?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
12namespace Twig\Profiler\Dumper;
13
14use Twig\Profiler\Profile;
15
16/**
17 * @author Fabien Potencier <fabien@symfony.com>
18 */
19final class BlackfireDumper
20{
21 public function dump(Profile $profile): string
22 {
23 $data = [];
24 $this->dumpProfile('main()', $profile, $data);
25 $this->dumpChildren('main()', $profile, $data);
26
27 $start = sprintf('%f', microtime(true));
28 $str = <<<EOF
29file-format: BlackfireProbe
30cost-dimensions: wt mu pmu
31request-start: $start
32
33
34EOF;
35
36 foreach ($data as $name => $values) {
37 $str .= "$name//{$values['ct']} {$values['wt']} {$values['mu']} {$values['pmu']}\n";
38 }
39
40 return $str;
41 }
42
43 private function dumpChildren(string $parent, Profile $profile, &$data)
44 {
45 foreach ($profile as $p) {
46 if ($p->isTemplate()) {
47 $name = $p->getTemplate();
48 } else {
49 $name = sprintf('%s::%s(%s)', $p->getTemplate(), $p->getType(), $p->getName());
50 }
51 $this->dumpProfile(sprintf('%s==>%s', $parent, $name), $p, $data);
52 $this->dumpChildren($name, $p, $data);
53 }
54 }
55
56 private function dumpProfile(string $edge, Profile $profile, &$data)
57 {
58 if (isset($data[$edge])) {
59 ++$data[$edge]['ct'];
60 $data[$edge]['wt'] += floor($profile->getDuration() * 1000000);
61 $data[$edge]['mu'] += $profile->getMemoryUsage();
62 $data[$edge]['pmu'] += $profile->getPeakMemoryUsage();
63 } else {
64 $data[$edge] = [
65 'ct' => 1,
66 'wt' => floor($profile->getDuration() * 1000000),
67 'mu' => $profile->getMemoryUsage(),
68 'pmu' => $profile->getPeakMemoryUsage(),
69 ];
70 }
71 }
72}