Xdebug profiling doesn't work

For a client I need to investigate what functions/code slows down their platform the most. The best thing I could have is an overview of how long every PHP function takes to execute. I can’t find any good solutions for this, apart from Xdebug profiling.

At the moment Local has Xdebug v2.9 built-in, but I can’t figure out how to start profiling. I added the code below in the PHP configuration, but it doesn’t seem to do anything when I restart the server. I can see the settings have been applied when I look into phpinfo(), but there are no logging/profiling files appearing in the directory.

xdebug.remote_autostart=1
xdebug.profiler_append=1
xdebug.remote_connect_back=On
xdebug.profiler_enable=1
xdebug.profiler_output_dir=""
xdebug.profiler_enable_trigger=1

I checked out various support topics and blogs, but didn’t find a solution yet. Who can help me out with this?

I haven’t ever tried getting this to work, so I’d be curious to know what you find out.

In general, when I’m investigating performance issues with WordPress sites, it’s more likely an issue with the DB and any complex queries that are being run. As an initial look at things, have to installed and enabled Query Monitor and viewed what kinds of queries are being run to generate the page you are wanting to optimize?

To your specific question of Xdebug, I took a look at the Starting the profiler doc – it mentions that you might need to add this to the php.ini.hbs config file and restart the Local site:

xdebug.mode=profile

Well the thing with Query Monitor is that it mainly gives insight in SQL-queries but not so much in what PHP-functions are actually running and/or how long they take or how many times they are being started.

I feel like we did as much as we can optimising the SQL-part of our platform, but maybe there’s PHP-functions could be optimised more.

1 Like

Nice! PHP performance in WordPress is something that I’d love to have more experience with, so as you work through this, I’d love to review your findings.

Maybe you’ve seen this already, but for a while there this tool was something I turned to for quickly zeroing in on different problem functions:

wp package install wp-cli/profile-command:@stable

As of newer versions of PHP, this tool isn’t as precise because a number of the functions that are added to hooks aren’t recorded.

Hi Ben,

When I try to install the package in the Site Shell I am receiving the following error:
Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 2267527 bytes) in phar:///Applications/Local.app/Contents/Resources/extraResources/bin/wp-cli/wp-cli.phar/vendor/composer/composer/src/Composer/Util/RemoteFilesystem.php on line 461

Ooof. I always forget that Composer (what wpcli uses to install packages) is such a memory hog.

See this WP make post for the background info: Common Issues – WP-CLI – WordPress.org

In the context of a Local site, you basically need to increase the memory limit for the PHP process. To do that:

  1. Open the php.ini.hbs file for the site and increase the memory limit.
  2. Save the file and restart the Local site to re-compile that configuration

When I did it, I had to go all the way to 1Gb to get that package to install:

After that, check out the docs for more usage info, but for example, on a plain wp site, I can profile the /sample-page/ and in particular the init hook for that page:

★  app/public % wp profile stage --all --url='http://profiling-php.local/sample-page/'
+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| hook                     | callback_count | time    | query_time | query_count | cache_ratio | cache_hits | cache_misses | request_time | request_count |
+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| muplugins_loaded:before  | 0              | 0.148s  | 0.0005s    | 1           | 25%         | 1          | 3            | 0s           | 0             |
| muplugins_loaded         | 2              | 0.0001s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| plugins_loaded:before    | 0              | 0.0675s | 0s         | 0           | 47.06%      | 8          | 9            | 0s           | 0             |
| plugins_loaded           | 4              | 0.0079s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| setup_theme:before       | 0              | 0.0006s | 0s         | 0           | 50%         | 2          | 2            | 0s           | 0             |
| setup_theme              | 2              | 0.0024s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| after_setup_theme:before | 0              | 0.0308s | 0.0002s    | 1           | 95.65%      | 88         | 4            | 0s           | 0             |
| after_setup_theme        | 2              | 0.0052s | 0s         | 0           | 100%        | 6          | 0            | 0s           | 0             |
| init:before              | 0              | 0.0033s | 0s         | 0           | 100%        | 2          | 0            | 0s           | 0             |
| init                     | 32             | 0.1175s | 0.0007s    | 2           | 98.95%      | 188        | 2            | 0s           | 0             |
| wp_loaded:before         | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp_loaded                | 2              | 0.0003s | 0s         | 0           | 100%        | 2          | 0            | 0s           | 0             |
| parse_request:before     | 0              | 0.0316s | 0.0016s    | 2           | 57.14%      | 4          | 3            | 0s           | 0             |
| parse_request            | 1              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| send_headers:before      | 0              | 0.0004s | 0s         | 0           | 100%        | 4          | 0            | 0s           | 0             |
| send_headers             | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| pre_get_posts:before     | 0              | 0.0004s | 0s         | 0           | 100%        | 7          | 0            | 0s           | 0             |
| pre_get_posts            | 1              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| the_posts:before         | 0              | 0.0049s | 0.001s     | 4           | 41.67%      | 5          | 7            | 0s           | 0             |
| the_posts                | 2              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp:before                | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp                       | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| template_redirect:before | 0              | 0.0005s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| template_redirect        | 7              | 0.0025s | 0s         | 0           | 100%        | 21         | 0            | 0s           | 0             |
| template_include:before  | 0              | 0.0008s | 0s         | 0           | 100%        | 9          | 0            | 0s           | 0             |
| template_include         | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp_head:before           | 0              | 0.0032s | 0s         | 0           | 100%        | 10         | 0            | 0s           | 0             |
| wp_head                  | 25             | 0.0221s | 0s         | 0           | 96.39%      | 160        | 6            | 0s           | 0             |
| loop_start:before        | 0              | 0.01s   | 0.0011s    | 1           | 98.73%      | 78         | 1            | 0s           | 0             |
| loop_start               | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| loop_end:before          | 0              | 0.0091s | 0.0006s    | 2           | 90.63%      | 29         | 3            | 0s           | 0             |
| loop_end                 | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp_footer:before         | 0              | 0.0179s | 0.0017s    | 7           | 86.59%      | 71         | 11           | 0s           | 0             |
| wp_footer                | 5              | 0.0015s | 0s         | 0           | 100%        | 4          | 0            | 0s           | 0             |
| wp_footer:after          | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| total (35)               | 85             | 0.4888s | 0.0074s    | 20          | 84.39%      | 699        | 51           | 0s           | 0             |
+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
★  app/public % wp profile hook init --url='http://profiling-php.local/sample-page/'
+--------------------------------------------+-------------------------------------------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| callback                                   | location                                        | time    | query_time | query_count | cache_ratio | cache_hits | cache_misses | request_time | request_count |
+--------------------------------------------+-------------------------------------------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| function(){}                               | phar:///Applications/Local.app/Contents/Resourc | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
|                                            | es/extraResources/bin/wp-cli/wp-cli.phar/vendor |         |            |             |             |            |              |              |               |
|                                            | /wp-cli/wp-cli/php/WP_CLI/Runner.php:1514       |         |            |             |             |            |              |              |               |
| create_initial_post_types()                | wp-includes/post.php:20                         | 0.0447s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| create_initial_taxonomies()                | wp-includes/taxonomy.php:24                     | 0.0148s | 0s         | 0           | 100%        | 20         | 0            | 0s           | 0             |
| wp_widgets_init()                          | wp-includes/widgets.php:1751                    | 0.038s  | 0.0002s    | 1           | 99.33%      | 148        | 1            | 0s           | 0             |
| smilies_init()                             | wp-includes/functions.php:4328                  | 0.0004s | 0s         | 0           | 100%        | 2          | 0            | 0s           | 0             |
| localwp_auto_login()                       | /Applications/Local.app/Contents/Resources/extr | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
|                                            | aResources/local-bootstrap.php:10               |         |            |             |             |            |              |              |               |
| function(){}                               | phar:///Applications/Local.app/Contents/Resourc | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
|                                            | es/extraResources/bin/wp-cli/wp-cli.phar/vendor |         |            |             |             |            |              |              |               |
|                                            | /wp-cli/wp-cli/php/WP_CLI/Runner.php:1565       |         |            |             |             |            |              |              |               |
| wp_init_targeted_link_rel_filters()        | wp-includes/formatting.php:3253                 | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| _register_core_block_patterns_and_categori | wp-includes/block-patterns.php:17               | 0.006s  | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| es()                                       |                                                 |         |            |             |             |            |              |              |               |
| wp_cron()                                  | wp-includes/cron.php:947                        | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp_schedule_https_detection()              | wp-includes/https-detection.php:163             | 0.0002s | 0s         | 0           | 100%        | 2          | 0            | 0s           | 0             |
| wp_schedule_delete_old_privacy_export_file | wp-includes/functions.php:7373                  | 0.0002s | 0s         | 0           | 100%        | 2          | 0            | 0s           | 0             |
| s()                                        |                                                 |         |            |             |             |            |              |              |               |
| _show_post_preview()                       | wp-includes/revision.php:607                    | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| rest_api_init()                            | wp-includes/rest-api.php:163                    | 0.0002s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp_sitemaps_get_server()                   | wp-includes/sitemaps.php:22                     | 0.0014s | 0s         | 0           | 100%        | 6          | 0            | 0s           | 0             |
| kses_init()                                | wp-includes/kses.php:2133                       | 0.0003s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp_schedule_update_checks()                | wp-includes/update.php:849                      | 0.0005s | 0s         | 0           | 100%        | 6          | 0            | 0s           | 0             |
| register_block_core_archives()             | wp-includes/blocks/archives.php:111             | 0.0004s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| register_block_core_block()                | wp-includes/blocks/block.php:64                 | 0.0005s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| register_block_core_calendar()             | wp-includes/blocks/calendar.php:52              | 0.0003s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| register_block_core_categories()           | wp-includes/blocks/categories.php:89            | 0.0004s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| register_block_core_latest_comments()      | wp-includes/blocks/latest-comments.php:148      | 0.0003s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| register_block_core_latest_posts()         | wp-includes/blocks/latest-posts.php:197         | 0.0003s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| register_block_core_rss()                  | wp-includes/blocks/rss.php:98                   | 0.0004s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| register_block_core_search()               | wp-includes/blocks/search.php:112               | 0.0003s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| register_block_core_shortcode()            | wp-includes/blocks/shortcode.php:23             | 0.0003s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| register_block_core_social_link()          | wp-includes/blocks/social-link.php:49           | 0.0003s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| register_block_core_tag_cloud()            | wp-includes/blocks/tag-cloud.php:46             | 0.0002s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| register_core_block_types_from_metadata()  | wp-includes/blocks/index.php:27                 | 0.0067s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| twenty_twenty_one_register_block_styles()  | twentytwentyone/inc/block-styles.php:20         | 0.0011s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| WP_Block_Supports::init()                  | wp-includes/class-wp-block-supports.php:65      | 0.001s  | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| check_theme_switched()                     | wp-includes/theme.php:3276                      | 0.0007s | 0.0003s    | 1           | 66.67%      | 2          | 1            | 0s           | 0             |
+--------------------------------------------+-------------------------------------------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| total (32)                                 |                                                 | 0.1199s | 0.0005s    | 2           | 95.75%      | 188        | 2            | 0s           | 0             |
+--------------------------------------------+-------------------------------------------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.