8000 [TwigBundle] Created stopwatch tag for profiling templates by wouterj · Pull Request #7953 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBundle] Created stopwatch tag for profiling templates #7953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

wouterj
Copy link
Member
@wouterj wouterj commented May 6, 2013
Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR symfony/symfony-docs#2630

This PR adds a new tag to Twig which you can use to time parts of a template and see it in the timing tab of the profiler.

Usage:

{% stopwatch foo %}
... some things that gets timed
{% endstopwatch %}

This feature is based on the idea of @lsmith77 (see also our twitter conversation: https://twitter.com/lsmith/status/330772561413672962 )

@lsmith77
Copy link
Contributor
lsmith77 commented May 6, 2013

thank you for this .. works perfectly.
note we discussed at length if this should go into the TwigBundle or into WebProfilerBundle. in the end we agreed on putting it into TwigBundle since this way one can even put template using this tag into production where the profiler is usually disabled.

{
$name = $this->getAttribute('name');

$compiler
Copy link
Contributor

Choose a reason for hiding this comment

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

we might want to pass a parameter into the StopwatchTokenParser and from there into StopwatchNode if the stopwatch service is even available. If so we would then not even write the startEvent/stopEvent code into the cached PHP.

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought about that too. I'll implement something like this

@fabpot
Copy link
Member
fabpot commented May 6, 2013

I would have put the code into the Twig bridge instead as this is where the integration of Twig and components happens. That would make the tag easily available for Drupal and Silex for instance.

@lsmith77
Copy link
Contributor
lsmith77 commented May 6, 2013

ah doh .. makes sense.

* Some stuff which will be recorded on the timeline
* {% endstopwatch %}
*/
new StopwatchTokenParser(),
Copy link
Contributor

Choose a reason for hiding this comment

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

you are missing the $stopwatchIsAvailable parameter here

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@wouterj
Copy link
Member Author
wouterj commented May 7, 2013

@fabpot I've moved the code to the twig bridge

* file that was distributed with this source code.
*/

namespace Symfony\Bundle\TwigBridge\Extension;
Copy link
Contributor

Choose a reason for hiding this comment

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

Namespace is not correct, should be: Symfony\Bridge\Twig\Extension.

@mpdude
Copy link
Contributor
mpdude commented May 7, 2013

Great! That deserves a "living on the edge" blog entry :-)

$body = $this->parser->subparse(array($this, 'decideStopwatchEnd'), true);
$stream->expect(\Twig_Token::BLOCK_END_TYPE);

return new StopwatchNode($name, $body, $this->stopwatchIsAvailable, $lineno, $this->getTag());
Copy link
Contributor

Choose a reason for hiding this comment

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

we can not simply return the $body if not stopwatchIsAvailable?

Copy link
Member Author

Choose a reason for hiding this comment

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

I need to test that. If it works, it's definitelly better than what we now have.

8000

Copy link
Member Author

Choose a reason for hiding this comment

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

tested and it works, it's implemented now

@fabpot
Copy link
Member
fabpot commented May 9, 2013

You should add a note in the CHANGELOG of the Twig bridge about this addition.

@Tobion
Copy link
Contributor
Tobion commented May 9, 2013

What happens when u have multiple stopwatch blocks with the same name? Does it get aggregated or overwritten?

{% stopwatch foo %}
... some things that gets timed
{% endstopwatch %}

uninteresting stuff

{% stopwatch foo %}
... some things that gets timed
{% endstopwatch %}

@stof
Copy link
Member
stof commented May 9, 2013

Tests are missing

@stof
Copy link
Member
stof commented May 9, 2013

btw, using NAME_TYPE forbids having dots in the event name. Shouldn't we allow using a string too ?

And would it make sense to allow passing any Twig expression instead of only compile-time values ? Nothing in the current code requires to be able to use the event name at compile-time so we could make it dynamic.

@wouterj
Copy link
Member Author
wouterj commented May 9, 2013

@stof should I add a StopwatchTokenParserTest? And what exactly do you mean with your last paragraph? Allowing string is a great idea, I'll add it.

@Tobion due to the way the stopwatch works, only the first one is recorded. I'll throw an error if the name already exists.

Todo

  • Add test
  • Allow strings
  • Throw error when name already exists
  • Make it dynamic (?)

@fabpot
Copy link
Member
fabpot commented May 9, 2013

We should use start, stop instead to allow what @Tobion describe. Adn we should also support categories.

@wouterj
Copy link
Member Author
wouterj commented May 9, 2013

@fabpot I can understand it wrong, but I already use start and stop?

@stof
Copy link
Member
stof commented May 9, 2013

@wouterj here is what I mean:

{% stopwatch 'foo' %}
    {# equivalent of current stopwatch foo #}
{% endstopwatch %}

{% set bar = 'foo' %}
{% stopwatch bar %}
    {# uses the variable #}
{% endstopwatch %}

{% stopwatch (bar ~ 'test')|upper %}
    {# uses a complex expression #}
{% endstopwatch %}

@stof
Copy link
Member
stof commented May 9, 2013

hmm, it could cause an issue to stop as we would need to stop the one we started, not to re-evaluate the expression as variables could have changed inside the body

@wouterj
Copy link
Member Author
wouterj commented May 9, 2013

Thanks for the explaination! But do you really think it's worth it? The core block tag doesn't support it and I don't think people will use such a things for debugging.

@stof
Copy link
Member
stof commented May 9, 2013

@wouterj block names have to be known at compile time. So it is a bad comparison

@matthiasnoback
Copy link

I think it would be good to support {% endstopwatch 'name' %} to make sure the right stopwatch is stopped. As far as I can see, it is also not possible to nest these tags, am I right?

@stof
Copy link
Member
stof commented May 10, 2013

@matthiasnoback The end tag should stop the corresponding opening tag. So passing the name explicitly is only a way to allow mistakes as it must be the same value.

And nothing forbids you to nest them if you use different names

@wouterj
Copy link
Member Author
wouterj commented May 10, 2013

I've implemented everything that is said here, except for 2 things:

  • Dynamic names. I fail to see when this is used.
  • Fabien's last comment (I can't follow that one). It throws an exception now if 2 names are equal

@wouterj
Copy link
Member Author
wouterj commented Jul 8, 2013

ping @fabpot

@@ -4,6 +4,7 @@ CHANGELOG
2.3.0
-----

* added stopwatch tag to time templates with the WebProfilerBundle
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not correct, 2.3 is already released.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@fabpot
Copy link
Member
fabpot commented Aug 11, 2013

Closing in favor of #8719

@fabpot fabpot closed this Aug 11, 2013
fabpot added a commit that referenced this pull request Aug 13, 2013
This PR was merged into the master branch.

Discussion
----------

[TwigBundle] Created stopwatch tag for profiling templates

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #7953
| License       | MIT
| Doc PR        | symfony/symfony-docs#2630

This PR is the continuation of #7953

This PR adds a new tag to Twig which you can use to time parts of a template and see it in the timing tab of the profiler.

Usage:

````jinja
{% stopwatch foo %}
... some things that gets timed
{% endstopwatch %}
````

Commits
-------

29a58e7 change the stopwatch argument to be any valid expression
4590974 removed code that prevents the stopwatch to work properly
2f67776 removed unneeded safeguard as it's already done during compilation
bbad387 fixed CS
f39ed57 Created stopwatch tag
@wouterj wouterj deleted the twig_stopwatch_helper branch May 1, 2014 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0