티스토리 수익 글 보기

티스토리 수익 글 보기

Add initial XHProf support to wp-env by joemcgill · Pull Request #48147 · WordPress/gutenberg · GitHub
Skip to content

Conversation

@joemcgill
Copy link
Member

@joemcgill joemcgill commented Feb 16, 2023

What?

This updates the configuration scripts to support passing a new –xhprof flag to wp env start, which will install and configure XHProf support to the WordPress environment.

Why?

Application profiling is an important tool for developers to use in order to discover potential performance bottlenecks in their code, but traditionally this tooling has been difficult to set up. By including support for the XHProf profiler directly in wp-env, we can “democratize profiling” by making these tools easier to use.

Additionally, these tools are already being used by the WordPress Core Performance team to help identify potential performance improvements to WordPress Core, and by including these tools natively in this package, we can make it easier for a broad number of people to contribute profiling information based on their own use cases.

How?

This works by installing the official XHProfile package from the PECL repository when the --xhprof flag is set, and updates the php.ini configuration as needed to support profiling PHP applications.

To enable profiling, you’ll need to include code to start the profiler and store or display the data that the profiler collects during an application run. One example of how to collect and analyze profiling data is to use XHGui (see these instructions for installing via Docker) and a library like PHP Profiler to send data collected during a profiling operation to XHGui. A simplified setup using an mu-plugin is demonstrated here: https://github.com/joemcgill/wp-profiler.

Testing Instructions

Testing Instructions for Keyboard

Screenshots or screencast

This updates the configuration scripts to support passing a new --xdebug flag to wp env start, which will install and configure XHProf support to the WordPress environment. To enable profiling, you'll need to edit the wp-config.php file to include the following code where the `wp-settings.php` file is included:

```php
// start profiling
xhprof_enable();

/** Sets up WordPress vars and included files. */

require_once ABSPATH . 'wp-settings.php';

// stop profiler
$xhprof_data = xhprof_disable();

// display raw xhprof data for the profiler run
print_r($xhprof_data);
```

For more info about how to configure XHProf, see: https://github.com/longxinH/xhprof.
Also see the original Facebook docs for XHProf here: http://web.archive.org/web/20110514095512/http://mirror.facebook.net/facebook/xhprof/doc.html.
// config has changed when only the xdebug param has changed. This is needed
// so that Docker will rebuild the image whenever the xdebug flag changes.
// config has changed when only the xdebug or xhprof params have changed. This
// is needed so that Docker will rebuild the image whenever the either flag changes.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// is needed so that Docker will rebuild the image whenever the either flag changes.
// is needed so that Docker will rebuild the image whenever either flag changes.

args.option( 'xhprof', {
describe:
'Enables XHProf. If not passed, XHProf is turned off.',
type: 'string',
Copy link
Member

Choose a reason for hiding this comment

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

If there’s only one XHProf mode, I think this should be a boolean instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

XHProf does support different configuration modes, though I’m unsure how important it is to support that config through the CLI versus as an option passed to the profiler when it’s loaded. See: https://github.com/joemcgill/wp-profiler/blob/trunk/plugin.php#L19-L24 for an example of how these flags can be set via code rather than configuration.

Copy link
Member

Choose a reason for hiding this comment

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

Good point. I think if people will have to configure this code in the first place, we don’t need to support setting the options via flags. If we did find a way to automate the wp-config.php changes, then it’d make sense to add them to the flags.

}

// @todo: test compat.
if ( config.xhprof !== 'off' ) {
Copy link
Member

Choose a reason for hiding this comment

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

If the xhprof flag is a boolean (see above), this could be written like config.xhprof === true

@noahtallen
Copy link
Member

Neat! Does this extension support most PHP versions? I know for Xdebug, we had to disable it for some PHP versions it didn’t support.

@joemcgill
Copy link
Member Author

Thanks for the quick look, @noahtallen! This is working, but can surely be simplified a bit so I decided to open this early for discussion. I’ll follow up with your comments above.

One thing that I’ve been debating is whether we should try to include support for XHGui directly with this PR, or if it is best to leave that to individual developers to use whatever GUI options they find most useful. I’d value your input on that question.

@joemcgill
Copy link
Member Author

According to the GitHub repo for the official XHProf PECL package, it currently supports PHP versions 7.2–8.2.

@noahtallen
Copy link
Member

One thing that I’ve been debating is whether we should try to include support for XHGui directly with this PR, or if it is best to leave that to individual developers to use whatever GUI options they find most useful. I’d value your input on that question.

I’m not mainly a PHP developer, so I haven’t used XHProf before. Is the GUI something most developers would expect to have available?

@joemcgill joemcgill added the [Tool] Env /packages/env label Feb 16, 2023
@joemcgill joemcgill self-assigned this Feb 16, 2023
@joemcgill
Copy link
Member Author

Is the GUI something most developers would expect to have available?

I think including it directly would make the developer experience much easier, since profiling is really not super helpful without some way to read/analyze the data that’s collected. There are other SaaS companies that have XHProf compatible/derivative GUIs as part of their service that could be used, but bundling XHGui would be a great help for someone wanting to get up and running without much extra config.

If we were to do so, I think it would require modifying the docker config to create two new containers: One for the XHGui application, and another for the MongoDB backend that it requires. The XHGui repo includes a docker-compose.yml file that could be used as part of the setup, but I may need some guidance on how to best approach that part in the context of the wp-env package.

@noahtallen
Copy link
Member

It should be relatively easy to update the docker compose file. It’s similarly generated in JS, so you could basically copy paste the docker-compose file you linked into env/lib/build-docker-compose-config.js, and also conditionally enable it based on the XHProf config flag

@justlevine
Copy link
Contributor

justlevine commented Jun 21, 2023

Is the goal of this PR scoped to just provide the tooling for local users to test and review manually in XHGui, or is there also some form of CI or local collation/reporting that that I’m just oblivious to in the diff?

@joemcgill
Copy link
Member Author

@justlevine this was initially only targeted at setting up the XHProf profiler in a wp-env environment so people could easily make use of the tooling. I plan to modify it to also add the containers for XHGui so that the data collected can be reviewed locally by users, I just haven’t had the opportunity to get back to it.

It should definitely be possible to use this tooling in a CI environment, but that’s not the goal of this specific PR.

@github-actions
Copy link

Warning: Type of PR label mismatch

To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.

  • Type-related labels to choose from: [Type] Automated Testing, [Type] Breaking Change, [Type] Bug, [Type] Build Tooling, [Type] Code Quality, [Type] Copy, [Type] Developer Documentation, [Type] Enhancement, [Type] Experimental, [Type] Feature, [Type] New API, [Type] Task, [Type] Technical Prototype, [Type] Performance, [Type] Project Management, [Type] Regression, [Type] Security, [Type] WP Core Ticket, Backport from WordPress Core, Gutenberg Plugin.
  • Labels found: [Tool] Env.

Read more about Type labels in Gutenberg. Don’t worry if you don’t have the required permissions to add labels; the PR reviewer should be able to help with the task.

@github-actions
Copy link

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you’re merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: joemcgill <joemcgill@git.wordpress.org>
Co-authored-by: noahtallen <noahtallen@git.wordpress.org>
Co-authored-by: justlevine <justlevine@git.wordpress.org>

To understand the WordPress project’s expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@joemcgill
Copy link
Member Author

So, it’s been a while 😬 and I thought I’d revisit this experiment. I’ve refreshed the branch so this applies cleanly against the new init-config.js structure.

Looking back at the conversation @noahtallen and I were having about this previously, the assumption was that this was only useful to support if we were also exposing some tooling that allowed folks to observe the profiling data via some included UI, like XHGUI. However, since then I’ve seen other tools like https://github.com/humanmade/query-monitor-flamegraph that depend on xhprof being enabled in the environment, so am wondering if it’s worth supporting the extension while being tooling agnostic?

@joemcgill joemcgill added the [Type] Feature New feature to highlight in changelogs. label Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Tool] Env /packages/env [Type] Feature New feature to highlight in changelogs.

Projects

Status: Code Review 👀

Development

Successfully merging this pull request may close these issues.

3 participants