From f7e1bec0cf28cc6de2146c9bebb487cdd9876397 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Wed, 29 May 2019 12:33:24 +0200 Subject: [PATCH] Very rought first working profiling --- .../Common/Twig/TwigClockworkDataSource.php | 22 +++---- .../Grav/Common/Twig/TwigProfileProcessor.php | 63 +++++++++++++++++++ 2 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 system/src/Grav/Common/Twig/TwigProfileProcessor.php diff --git a/system/src/Grav/Common/Twig/TwigClockworkDataSource.php b/system/src/Grav/Common/Twig/TwigClockworkDataSource.php index 033f9159d..8c3a5dc91 100644 --- a/system/src/Grav/Common/Twig/TwigClockworkDataSource.php +++ b/system/src/Grav/Common/Twig/TwigClockworkDataSource.php @@ -14,6 +14,7 @@ use Clockwork\Helpers\Serializer; use Clockwork\Request\Request; use Clockwork\Request\Timeline; use Grav\Common\Grav; +use Twig\Profiler\Profile; class TwigClockworkDataSource extends DataSource { @@ -22,6 +23,8 @@ class TwigClockworkDataSource extends DataSource */ protected $views; + protected $root; + /** * Create a new data source, takes Twig instance as an argument */ @@ -44,19 +47,16 @@ class TwigClockworkDataSource extends DataSource protected function processTwigTimings() { - $time = 10; - $data = ['foo', 'bar']; + $profile = Grav::instance()['twig']->profile(); + + $processor = new TwigProfileProcessor(); + + $processor->process($profile, $this->views); + - $dumper = new \Twig\Profiler\Dumper\TextDumper(); - $output = $dumper->dump(Grav::instance()['twig']->profile()); - $this->views->addEvent( - 'Twig', - 'Test Description', - $time, - $time + 10, - [$output] - ); } + + } diff --git a/system/src/Grav/Common/Twig/TwigProfileProcessor.php b/system/src/Grav/Common/Twig/TwigProfileProcessor.php new file mode 100644 index 000000000..4c1b582f3 --- /dev/null +++ b/system/src/Grav/Common/Twig/TwigProfileProcessor.php @@ -0,0 +1,63 @@ +isRoot()) { + $this->root = $profile->getDuration(); + $name = $profile->getName(); + } else { + if ($profile->isTemplate()) { + $name = $profile->getTemplate(); + } else { + $name = $this->formatNonTemplate($profile); + } + } + + $percent = $this->root ? $profile->getDuration() / $this->root * 100 : 0; + + + $data = [$this->formatTime($profile, $percent)]; + + + $views->addEvent( + $counter, + $profile->getTemplate(), + 0, + $profile->getDuration(), + [ 'name' => $name, 'data' => $data ] + ); + + foreach ($profile as $i => $p) { + $this->process($p, $views, ++$counter); + } + + + } + + protected function formatNonTemplate(Profile $profile) + { + return sprintf('%s::%s(%s)', $profile->getTemplate(), $profile->getType(), $profile->getName()); + } + + protected function formatTime(Profile $profile, $percent) + { + return sprintf('%.2fms/%.0f%%', $profile->getDuration() * 1000, $percent); + } +}