Blackfire.php 1.9 KB

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