Project

General

Profile

1
<?php
2

    
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2015 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
class Twig_Profiler_Dumper_Blackfire
16
{
17
    public function dump(Twig_Profiler_Profile $profile)
18
    {
19
        $data = array();
20
        $this->dumpProfile('main()', $profile, $data);
21
        $this->dumpChildren('main()', $profile, $data);
22

    
23
        $start = microtime(true);
24
        $str = <<<EOF
25
file-format: BlackfireProbe
26
cost-dimensions: wt mu pmu
27
request-start: {$start}
28

    
29

    
30
EOF;
31

    
32
        foreach ($data as $name => $values) {
33
            $str .= "{$name}//{$values['ct']} {$values['wt']} {$values['mu']} {$values['pmu']}\n";
34
        }
35

    
36
        return $str;
37
    }
38

    
39
    private function dumpChildren($parent, Twig_Profiler_Profile $profile, &$data)
40
    {
41
        foreach ($profile as $p) {
42
            if ($p->isTemplate()) {
43
                $name = $p->getTemplate();
44
            } else {
45
                $name = sprintf('%s::%s(%s)', $p->getTemplate(), $p->getType(), $p->getName());
46
            }
47
            $this->dumpProfile(sprintf('%s==>%s', $parent, $name), $p, $data);
48
            $this->dumpChildren($name, $p, $data);
49
        }
50
    }
51

    
52
    private function dumpProfile($edge, Twig_Profiler_Profile $profile, &$data)
53
    {
54
        if (isset($data[$edge])) {
55
            $data[$edge]['ct'] += 1;
56
            $data[$edge]['wt'] += floor($profile->getDuration() * 1000000);
57
            $data[$edge]['mu'] += $profile->getMemoryUsage();
58
            $data[$edge]['pmu'] += $profile->getPeakMemoryUsage();
59
        } else {
60
            $data[$edge] = array(
61
                'ct' => 1,
62
                'wt' => floor($profile->getDuration() * 1000000),
63
                'mu' => $profile->getMemoryUsage(),
64
                'pmu' => $profile->getPeakMemoryUsage(),
65
            );
66
        }
67
    }
68
}
(1-1/3)