Skip to content
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

[FrameworkBundle][HttpKernel] Add the ability to enable the profiler using a parameter #43138

Merged
merged 1 commit into from Oct 30, 2021

Conversation

dunglas
Copy link
Member

@dunglas dunglas commented Sep 22, 2021

Q A
Branch? 5.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets n/a
License MIT
Doc PR todo

The Symfony Profiler is a convenient development tool, however, in some cases, it has a huge overhead.
I used Blackfire to benchmark a development environment on a real-world project and noticed that the average response time was reduced by 30% just by disabling the profiler.

This PR adds a new configuration option allowing to enable and disable the profiler on a per request basis using a query parameter, a form field (for POST requests), or a request attribute. This PR allows keeping the benefit of this tool while improving the developer experience.

To enable it:

framework:
    profiler:
        collect: false # Disable collection by default 
        collect_parameter: profile # Enable it if the parameter is set

Note: it's also possible to enable the collection by default and to disable it using the parameter

Then, to have the profiler, the user just has to add ?profile=1 to any URL.

@carsonbot carsonbot added this to the 5.4 milestone Sep 22, 2021
@carsonbot carsonbot changed the title [HttpKernel][FrameworkBundle] Add the ability to enable the profiler … [FrameworkBundle][HttpKernel] Add the ability to enable the profiler … Sep 22, 2021
@dunglas dunglas changed the title [FrameworkBundle][HttpKernel] Add the ability to enable the profiler … [FrameworkBundle][HttpKernel] Add the ability to enable the profiler using a parameter Sep 22, 2021
@dunglas dunglas force-pushed the feat/profiler-collect-parameter branch 2 times, most recently from 161e027 to 5dcb5c5 Compare September 22, 2021 18:00
Copy link
Member

@chalasr chalasr left a comment

Choose a reason for hiding this comment

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

Some tests are broken

@stof
Copy link
Member

stof commented Sep 22, 2021

Symfony 2.0 used to have a config setting allowing to provide a RequestMatcher service to activate collecting. This is more flexible than giving a parameter name in the config.

Also note that part of the overhead is caused by the fact that services are decorated with Traceable wrappers, and those would still be used if you enable the profiler with collect: false

@dunglas
Copy link
Member Author

dunglas commented Sep 23, 2021

@stof most of the overhead looks caused by the extra IOs, at least on my configuration (Docker for Mac). I checked and just disabling the collection improves the performance of 30%.

Regarding the request matcher, do you know why it has been removed? This probably more flexible but less practicable. Here you can just pass a query parameter to enable/disable the tool without having to change the config. We could even think about a tiny WebExtension to do that (similar to the Blackfire companion).

@florentdestremau
Copy link
Contributor

Suggestion: why not enabling / disabling with a cookie or with some switch in the symfony debug bar ?

@chalasr
Copy link
Member

chalasr commented Sep 23, 2021

The motivation for removing it was to discourage using the profiler in production, as the matcher option was mostly used for that: #24158.

But as you can see in the PR discussion, it was already useful to save some resources in dev at the time, and the performance cost related to collectors is growing since then.
IMHO a query param is good enough.

xabbuh added a commit that referenced this pull request Oct 1, 2021
This PR was merged into the 4.4 branch.

Discussion
----------

[HttpKernel] Relax some transient tests

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

As observed in #42824 and more recently #43138, these tests randomly break for some reason.
Replaces #42824

Commits
-------

30fa29f [HttpKernel] Relax some transient tests
@chalasr
Copy link
Member

chalasr commented Oct 8, 2021

Rebase needed to make the CI green

@@ -79,6 +81,10 @@ public function onKernelResponse(ResponseEvent $event)
}

$request = $event->getRequest();
if (null !== $this->collectParameter && null !== $collectParameterValue = $request->get($this->collectParameter)) {
true === $collectParameterValue || filter_var($collectParameterValue, \FILTER_VALIDATE_BOOLEAN) ? $this->profiler->enable() : $this->profiler->disable();
Copy link
Member

Choose a reason for hiding this comment

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

no need for the true === $collectParameterValue || part, it's a µoptim that we can remove imho

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

(just one minor comment)

@fabpot fabpot force-pushed the feat/profiler-collect-parameter branch from c196897 to eecff07 Compare October 30, 2021 10:22
@fabpot
Copy link
Member

fabpot commented Oct 30, 2021

Thank you @dunglas.

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.

None yet

8 participants