Very rought first working profiling

This commit is contained in:
Andy Miller
2019-05-29 12:33:24 +02:00
parent 320ab41435
commit f7e1bec0cf
2 changed files with 74 additions and 11 deletions

View File

@@ -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]
);
}
}

View File

@@ -0,0 +1,63 @@
<?php
/**
* @package Grav\Common\Twig
*
* @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/
namespace Grav\Common\Twig;
use Twig\Profiler\Profile;
use Clockwork\Request\Timeline;
class TwigProfileProcessor
{
private $root;
public function process(Profile $profile, Timeline $views, $counter = 0)
{
if ($profile->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);
}
}