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