Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[WebServerBundle] Add support for Xdebug's Profiler #28298

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

Merged
merged 1 commit into from
Sep 9, 2018

Conversation

maidmaid
Copy link
Contributor

@maidmaid maidmaid commented Aug 28, 2018

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets /
License MIT
Doc PR /

Xdebug's Profiler is a powerful tool that gives you the ability to analyze your PHP code and determine bottlenecks or generally see which parts of your code are slow and could use a speed boost.

https://xdebug.org/docs/profiler

When we run/start the web server, it would be useful to enable the trigger for the Xdebug's Profiler. That means we could easily trigger the creation of a Xdebug profile and analysing it thanks to PhpStorm which provides an Execution Statistics panel (examine the summary information about execution metrics of every called function) and a Call Tree panel (explore the execution paths of all called functions). You can see these two panels in action here for a better understanding.

@@ -101,6 +107,11 @@ public function getAddress()
return $this->hostname.':'.$this->port;
}

public function isXdebugUsed()
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure adding this publilc method makes sense.
Let's use extension_loaded inline instead, don't you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we inline it, the developers could no longer disable this feature if wanted. Ok for you ?

Copy link
Member

Choose a reason for hiding this comment

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

Nobody is going to extend this class anyway.

@nicolas-grekas nicolas-grekas added this to the next milestone Aug 30, 2018
@fabpot
Copy link
Member

fabpot commented Aug 30, 2018

I would add a flag instead.

@maidmaid
Copy link
Contributor Author

Could you be more precise please ?

@fabpot
Copy link
Member

fabpot commented Aug 30, 2018

Being able to use an option (sorry, doing too much of that other language that is not PHP) on commands: --enable-xdebug or something similar.

@maidmaid
Copy link
Contributor Author

--enable-xdebug

IMO, this feature should be enabled by default (if xdebug extension is loaded of course), so if we really want make this feature controllable, --disable-xdebug should make more sense. But I'd prefer to make it simple by not making it controllable : if xdebug extension is loaded, then use it, else do not.

@maidmaid maidmaid force-pushed the webserverbundle-xdebug branch 2 times, most recently from e25fe30 to 62852ab Compare August 30, 2018 19:01
@nicolas-grekas
Copy link
Member

I'm wondering: why would you need to do this? I mean: can't you change your php.ini instead and get the same result in a more flexible way (i.e. don't force the setting to everyone?)

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Aug 31, 2018

Or can't we make php -dxdebug.profiler_enable_trigger=1 bin/console server:run work instead?

@maidmaid
Copy link
Contributor Author

php -dxdebug.profiler_enable_trigger=1 bin/console server:run

I thought that worked firstly, but it actually does not because we enable it only in the main process, not in the sub process running php -S 127.0.0.1:8000

@nicolas-grekas
Copy link
Member

Yep I know, but we can make it work. It would be better IMHO. We could even make it work for all xdebug ini settings if it makes sense, isn't it?

@maidmaid
Copy link
Contributor Author

maidmaid commented Aug 31, 2018

We could even make it work for all xdebug ini settings

After checking again the settings described in https://xdebug.org/docs/all_settings, just enabling the trigger of the profiler seems relevant IMO.

@@ -150,7 +150,9 @@ private function createServerProcess(WebServerConfig $config)
throw new \RuntimeException('Unable to find the PHP binary.');
}

$process = new Process(array_merge(array($binary), $finder->findArguments(), array('-dvariables_order=EGPCS', '-S', $config->getAddress(), $config->getRouter())));
$xdebugArgs = \extension_loaded('xdebug') ? array('-dxdebug.profiler_enable_trigger=1') : array();
Copy link
Member

Choose a reason for hiding this comment

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

should we check whether this setting is enabled in the current PHP instead of always adding them ? AFAIK, turning on this feature adds overhead to the execution of PHP, as Xdebug needs to instrument the code (even though it does not actually collect the profiling data).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's the reason why I turn on only the trigger of the profiler but not the profiler itself, whose the overhead is insignifiant.

@nicolas-grekas
Copy link
Member

I would really prefer this to be made to work with php -dxdebug.profiler_enable_trigger=1 bin/console server:run instead.

@maidmaid maidmaid force-pushed the webserverbundle-xdebug branch from 62852ab to 8ef25cc Compare September 1, 2018 17:47
@maidmaid
Copy link
Contributor Author

maidmaid commented Sep 1, 2018

@nicolas-grekas Could you review the last version I committed ?

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.

(with minor comment)

@@ -133,6 +133,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$config = new WebServerConfig($documentRoot, $env, $input->getArgument('addressport'), $input->getOption('router'));

$io->success(sprintf('Server listening on http://%s', $config->getAddress()));
if (\extension_loaded('xdebug') && '1' === ini_get('xdebug.profiler_enable_trigger')) {
Copy link
Member

Choose a reason for hiding this comment

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

if (ini_get('xdebug.profiler_enable_trigger')) { would be enough to me (same below)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@maidmaid maidmaid force-pushed the webserverbundle-xdebug branch from 8ef25cc to 34930ec Compare September 1, 2018 18:53
@@ -133,6 +133,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$config = new WebServerConfig($documentRoot, $env, $input->getArgument('addressport'), $input->getOption('router'));

$io->success(sprintf('Server listening on http://%s', $config->getAddress()));
if ('1' === ini_get('xdebug.profiler_enable_trigger')) {
Copy link
Member

Choose a reason for hiding this comment

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

The '1' === doesn't make really sense.
The really correct way to check a boolean ini setting is by using filter_var:
filter_var(ini_get('xdebug.profiler_enable_trigger'), FILTER_VALIDATE_BOOLEAN)
See http://php.net/manual/fr/function.filter-var.php#121263
I'd suggest to either make a loose comparison (thus my original proposal: if (ini_get('xdebug.profiler_enable_trigger')) {) or a strict one using filter_var, but not something in between.

Copy link
Contributor

Choose a reason for hiding this comment

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

ini_get return the normalized value instead of the one present in the php.ini file, so the return value will be '1' ou '' here (or false if xdebug is not loaded).

Copy link
Member

Choose a reason for hiding this comment

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

Oh great! Let's use if (ini_get('xdebug.profiler_enable_trigger')) { then, to be consistent with the code base at least.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@maidmaid maidmaid force-pushed the webserverbundle-xdebug branch from 34930ec to 0f4c0a6 Compare September 9, 2018 09:48
@nicolas-grekas
Copy link
Member

Thank you @maidmaid.

@nicolas-grekas nicolas-grekas merged commit 0f4c0a6 into symfony:master Sep 9, 2018
nicolas-grekas added a commit that referenced this pull request Sep 9, 2018
…aidmaid)

This PR was merged into the 4.2-dev branch.

Discussion
----------

[WebServerBundle] Add support for Xdebug's Profiler

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | /
| License       | MIT
| Doc PR        | /

> Xdebug's Profiler is a powerful tool that gives you the ability to analyze your PHP code and determine bottlenecks or generally see which parts of your code are slow and could use a speed boost.

https://xdebug.org/docs/profiler

When we run/start the web server, it would be useful to enable the trigger for the Xdebug's Profiler. That means we could easily trigger the creation of a Xdebug profile and analysing it [thanks to PhpStorm](https://www.jetbrains.com/help/phpstorm/analyzing-xdebug-profiling-data.html) which provides an **Execution Statistics** panel (examine the summary information about execution metrics of every called function) and a **Call Tree** panel (explore the execution paths of all called functions). You can see these two panels in action [here](https://youtu.be/_ua_O01IICg?t=1m22s) for a better understanding.

Commits
-------

0f4c0a6 Add support for Xdebug Profiler
@nicolas-grekas nicolas-grekas removed this from the next milestone Nov 1, 2018
@nicolas-grekas nicolas-grekas added this to the 4.2 milestone Nov 1, 2018
This was referenced Nov 3, 2018
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.

6 participants