Skip to content

Added a Twig profiler#13428

Merged
fabpot merged 2 commits intosymfony:2.7from
fabpot:twig-profiler
Jan 25, 2015
Merged

Added a Twig profiler#13428
fabpot merged 2 commits intosymfony:2.7from
fabpot:twig-profiler

Conversation

@fabpot
Copy link
Copy Markdown
Member

@fabpot fabpot commented Jan 16, 2015

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? yes
Tests pass? yes
Fixed tickets #6914
License MIT
Doc PR n/a

This PR integrates the new Twig 1.18 Profiler (see twigphp/Twig#1597) into Symfony (replace the current TimedTwigEngine) and adds a new Twig panel.

The timers are now available for all rendered templates (TimedTwigEngine was only able to get information from a few of them -- mainly the first template only).

The Twig panel gives you a lot of information about the execution of the templates, including a call graph.

image

image

image

image

image

@wimleers
Copy link
Copy Markdown

Very much looking forward to having this in Drupal 8! (Via https://www.drupal.org/project/webprofiler .)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this use lateCollect to take into account templates rendered later (in a streamed response for instance) ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went back and forth on this one, but the current version is probably fine with the late data collector.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this service is only meant to be accessed by twig.extension.profiler and data_collector.twig. It does not need to be retrieved dynamically. Right ? If yes, it could be marked private

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made public because you might want to get the profile from a CLI tool or something else. It is available through the data collector, but exposing it directly seems like a better idea.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fine with me (making it private would not allow inlining it anyway, as it is referenced twice)

@stof
Copy link
Copy Markdown
Member

stof commented Jan 16, 2015

the composer.json of the TwigBridge needs to be updated to Twig ~1.18

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it really be tied to the presence of debug.stopwatch (which is optional in the service definition already) ? this would prevent getting the Twig profiling data when you don't have the Stopwatch

@fabpot fabpot force-pushed the twig-profiler branch 6 times, most recently from 15a1815 to 9c621b6 Compare January 16, 2015 10:33
@hhamon
Copy link
Copy Markdown
Contributor

hhamon commented Jan 16, 2015

Oh yeah! This feature is very welcomed! Really nice to have.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, it's weird to call a getter before reading a property wich seem to not be related to the previous call
What's about

public function getComputedData($key)
{
    if (null === $this->computed ) {
        $this->computed = $this->computeData($this->getProfile());
    }

    return $this->computed[$key];
}

@emmanuelballery
Copy link
Copy Markdown

👍 ! Really great !

@Haehnchen
Copy link
Copy Markdown
Contributor

quick idea: would be nice to dump template context vars also. allows me to provide ide completion.

@aitboudad
Copy link
Copy Markdown
Contributor

👍

fabpot added a commit to twigphp/Twig that referenced this pull request Jan 20, 2015
This PR was merged into the 1.17-dev branch.

Discussion
----------

added a profiler

This profiler allows to better understand what's going on when rendering templates. It should be used when debug is true only as it adds some overhead.

Here is a sample output in the text format:

```
main 567.04ms/100%
└ index 463.28ms/82%
  └ base 463.24ms/82%
    └ base::block(header)
    └ index::block(content) 306.29ms/54%
    │ └ included 50.18ms/9%
    │ └ included 45.42ms/8%
    │ └ included 35.42ms/6%
    │ └ base::block(content) 104.97ms/19%
    │ │ └ included 34.90ms/6%
    │ └ included 35.84ms/6%
    └ base::block(footer) 156.84ms/28%
      └ included 38.96ms/7%
      └ base::macro(foo)
```

You can also dump a profile in a Blackfire compatible format to [visualise the data as a graph](http://goo.gl/RwNGFH).

A profile is also a great way to be able to list all templates/blocks/macros that were used when rendering a template.

A PR on Symfony (symfony/symfony#13428) integrates this profiler in the Symfony web profiler to replace the current TimedTwigEngine and to add a new Twig panel.

![symfony-web-profiler](https://cloud.githubusercontent.com/assets/47313/5755461/019cd6a4-9ca5-11e4-8762-68e2a709c336.png)

Commits
-------

4408caa added a profiler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.