WP-CLI as an analysis tool

Last Revised: October 2, 2021

If you don’t already have WP-CLI, install it on your server. Once you have it, you can expand its functionality.

WP-CLI profile

WP-CLI profile makes an analysis of several indicators of execution in a WordPress installation so that you can detect slow points in its operation.

The first thing will be to install it if you do not already have it.

wp package install wp-cli/profile-command --allow-root

Once we have it installed, we can go to the folder where we have the WordPress to analyze.

cd /home/example.com/public_html/

When we are in the folder where we have our WordPress, we can make a series of queries.

The first of these is the “bootstrap”, which will analyze the base installation of WordPress, loading the WordPress itself, the plugins, themes and the startup.

wp profile stage bootstrap --all --allow-root

This will return a table similar to the following:

+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| hook                     | callback_count | time    | query_time | query_count | cache_ratio | cache_hits | cache_misses | request_time | request_count |
+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| muplugins_loaded:before  |                | 0.1316s | 0.0006s    | 1           | 25%         | 1          | 3            | 0s           | 0             |
| muplugins_loaded         | 2              | 0.0002s | 0s         | 0           | 50%         | 1          | 1            | 0s           | 0             |
| plugins_loaded:before    |                | 0.0161s | 0.0006s    | 2           | 54.17%      | 13         | 11           | 0s           | 0             |
| plugins_loaded           | 5              | 0.004s  | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| setup_theme:before       |                | 0.0002s | 0s         | 0           | 100%        | 4          | 0            | 0s           | 0             |
| setup_theme              | 2              | 0.0004s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| after_setup_theme:before |                | 0.0072s | 0s         | 0           | 100%        | 89         | 0            | 0s           | 0             |
| after_setup_theme        | 2              | 0.0008s | 0s         | 0           | 100%        | 6          | 0            | 0s           | 0             |
| init:before              |                | 0.0024s | 0s         | 0           | 100%        | 2          | 0            | 0s           | 0             |
| init                     | 30             | 0.0197s | 0.0006s    | 2           | 98.97%      | 192        | 2            | 0s           | 0             |
| wp_loaded:before         |                | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp_loaded                | 1              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| parse_request:before     |                | 0.0242s | 0s         | 0           | 100%        | 4          | 0            | 0s           | 0             |
| parse_request            | 1              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| send_headers:before      |                | 0.0001s | 0s         | 0           | 100%        | 4          | 0            | 0s           | 0             |
| send_headers             | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| pre_get_posts:before     |                | 0.0002s | 0s         | 0           | 100%        | 2          | 0            | 0s           | 0             |
| pre_get_posts            | 1              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| the_posts:before         |                | 0.001s  | 0.0005s    | 1           | 100%        | 6          | 0            | 0s           | 0             |
| the_posts                | 2              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp:before                |                | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp                       | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| template_redirect:before |                | 0.0002s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| template_redirect        | 7              | 0.0006s | 0s         | 0           | 100%        | 14         | 0            | 0s           | 0             |
| template_include:before  |                | 0.0001s | 0s         | 0           | 100%        | 2          | 0            | 0s           | 0             |
| template_include         | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp_head:before           |                | 0.0008s | 0s         | 0           | 100%        | 10         | 0            | 0s           | 0             |
| wp_head                  | 25             | 0.0037s | 0s         | 0           | 95.37%      | 103        | 5            | 0s           | 0             |
| loop_start:before        |                | 0.0013s | 0s         | 0           | 100%        | 64         | 0            | 0s           | 0             |
| loop_start               | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| loop_end:before          |                | 0.0054s | 0.0005s    | 2           | 97.39%      | 112        | 3            | 0s           | 0             |
| loop_end                 | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp_footer:before         |                | 0.0046s | 0.0012s    | 4           | 94.44%      | 68         | 4            | 0s           | 0             |
| wp_footer                | 5              | 0.0004s | 0s         | 0           | 100%        | 4          | 0            | 0s           | 0             |
| wp_footer:after          |                | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| total (35)               | 83             | 0.2255s | 0.0041s    | 12          | 90.77%      | 701        | 29           | 0s           | 0             |
+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+

The second test is to make a query to the “loop” to bring information from an entry or content.

wp profile stage main_query --all --allow-root

This will return a table similar to the following:

+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| hook                     | callback_count | time    | query_time | query_count | cache_ratio | cache_hits | cache_misses | request_time | request_count |
+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| muplugins_loaded:before  |                | 0.1427s | 0.0005s    | 1           | 25%         | 1          | 3            | 0s           | 0             |
| muplugins_loaded         | 2              | 0.0002s | 0s         | 0           | 50%         | 1          | 1            | 0s           | 0             |
| plugins_loaded:before    |                | 0.0163s | 0.0006s    | 2           | 54.17%      | 13         | 11           | 0s           | 0             |
| plugins_loaded           | 5              | 0.0043s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| setup_theme:before       |                | 0.0003s | 0s         | 0           | 100%        | 4          | 0            | 0s           | 0             |
| setup_theme              | 2              | 0.0005s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| after_setup_theme:before |                | 0.0079s | 0s         | 0           | 100%        | 89         | 0            | 0s           | 0             |
| after_setup_theme        | 2              | 0.0008s | 0s         | 0           | 100%        | 6          | 0            | 0s           | 0             |
| init:before              |                | 0.0026s | 0s         | 0           | 100%        | 2          | 0            | 0s           | 0             |
| init                     | 30             | 0.0222s | 0.0006s    | 2           | 98.97%      | 192        | 2            | 0s           | 0             |
| wp_loaded:before         |                | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp_loaded                | 1              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| parse_request:before     |                | 0.0255s | 0s         | 0           | 100%        | 4          | 0            | 0s           | 0             |
| parse_request            | 1              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| send_headers:before      |                | 0.0001s | 0s         | 0           | 100%        | 4          | 0            | 0s           | 0             |
| send_headers             | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| pre_get_posts:before     |                | 0.0001s | 0s         | 0           | 100%        | 2          | 0            | 0s           | 0             |
| pre_get_posts            | 1              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| the_posts:before         |                | 0.0007s | 0.0002s    | 1           | 100%        | 6          | 0            | 0s           | 0             |
| the_posts                | 2              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp:before                |                | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp                       | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| template_redirect:before |                | 0.0003s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| template_redirect        | 7              | 0.0009s | 0s         | 0           | 100%        | 14         | 0            | 0s           | 0             |
| template_include:before  |                | 0.0002s | 0s         | 0           | 100%        | 2          | 0            | 0s           | 0             |
| template_include         | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp_head:before           |                | 0.0009s | 0s         | 0           | 100%        | 10         | 0            | 0s           | 0             |
| wp_head                  | 25             | 0.0036s | 0s         | 0           | 95.37%      | 103        | 5            | 0s           | 0             |
| loop_start:before        |                | 0.0014s | 0s         | 0           | 100%        | 64         | 0            | 0s           | 0             |
| loop_start               | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| loop_end:before          |                | 0.0053s | 0.0005s    | 2           | 97.39%      | 112        | 3            | 0s           | 0             |
| loop_end                 | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp_footer:before         |                | 0.0044s | 0.0009s    | 4           | 94.44%      | 68         | 4            | 0s           | 0             |
| wp_footer                | 5              | 0.0004s | 0s         | 0           | 100%        | 4          | 0            | 0s           | 0             |
| wp_footer:after          |                | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| total (35)               | 83             | 0.2418s | 0.0034s    | 12          | 90.77%      | 701        | 29           | 0s           | 0             |
+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+

The third and final test is to make a query to the “template”, which would load the core and information to present it on the front.

wp profile stage template --all --allow-root

This will return a table similar to the following:

+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| hook                     | callback_count | time    | query_time | query_count | cache_ratio | cache_hits | cache_misses | request_time | request_count |
+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| muplugins_loaded:before  |                | 0.1338s | 0.0006s    | 1           | 25%         | 1          | 3            | 0s           | 0             |
| muplugins_loaded         | 2              | 0.0002s | 0s         | 0           | 50%         | 1          | 1            | 0s           | 0             |
| plugins_loaded:before    |                | 0.0152s | 0.0006s    | 2           | 54.17%      | 13         | 11           | 0s           | 0             |
| plugins_loaded           | 5              | 0.0038s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| setup_theme:before       |                | 0.0002s | 0s         | 0           | 100%        | 4          | 0            | 0s           | 0             |
| setup_theme              | 2              | 0.0004s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| after_setup_theme:before |                | 0.0068s | 0s         | 0           | 100%        | 89         | 0            | 0s           | 0             |
| after_setup_theme        | 2              | 0.0008s | 0s         | 0           | 100%        | 6          | 0            | 0s           | 0             |
| init:before              |                | 0.0023s | 0s         | 0           | 100%        | 2          | 0            | 0s           | 0             |
| init                     | 30             | 0.0193s | 0.0008s    | 2           | 98.97%      | 192        | 2            | 0s           | 0             |
| wp_loaded:before         |                | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp_loaded                | 1              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| parse_request:before     |                | 0.0231s | 0s         | 0           | 100%        | 4          | 0            | 0s           | 0             |
| parse_request            | 1              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| send_headers:before      |                | 0.0001s | 0s         | 0           | 100%        | 4          | 0            | 0s           | 0             |
| send_headers             | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| pre_get_posts:before     |                | 0.0001s | 0s         | 0           | 100%        | 2          | 0            | 0s           | 0             |
| pre_get_posts            | 1              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| the_posts:before         |                | 0.0008s | 0.0003s    | 1           | 100%        | 6          | 0            | 0s           | 0             |
| the_posts                | 2              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp:before                |                | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp                       | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| template_redirect:before |                | 0.0003s | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| template_redirect        | 7              | 0.001s  | 0s         | 0           | 100%        | 14         | 0            | 0s           | 0             |
| template_include:before  |                | 0.0002s | 0s         | 0           | 100%        | 2          | 0            | 0s           | 0             |
| template_include         | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp_head:before           |                | 0.0012s | 0s         | 0           | 100%        | 10         | 0            | 0s           | 0             |
| wp_head                  | 25             | 0.0051s | 0s         | 0           | 95.37%      | 103        | 5            | 0s           | 0             |
| loop_start:before        |                | 0.0014s | 0s         | 0           | 100%        | 64         | 0            | 0s           | 0             |
| loop_start               | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| loop_end:before          |                | 0.0056s | 0.0007s    | 2           | 97.39%      | 112        | 3            | 0s           | 0             |
| loop_end                 | 0              | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
| wp_footer:before         |                | 0.0049s | 0.0012s    | 4           | 94.44%      | 68         | 4            | 0s           | 0             |
| wp_footer                | 5              | 0.0004s | 0s         | 0           | 100%        | 4          | 0            | 0s           | 0             |
| wp_footer:after          |                | 0s      | 0s         | 0           |             | 0          | 0            | 0s           | 0             |
+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+
| total (35)               | 83             | 0.2274s | 0.0042s    | 12          | 90.77%      | 701        | 29           | 0s           | 0             |
+--------------------------+----------------+---------+------------+-------------+-------------+------------+--------------+--------------+---------------+

This data can help us to find bottlenecks or points where there is something that does not correspond. Some options are to turn off all the plugins, activate a theme of the “Twenty” and activate to see their behavior according to and adapt the configurations.

WP-CLI doctor

WP-CLI doctor performs a search for possible configurations that can be reviewed.

The first thing will be to install it if you do not already have it.

wp package install wp-cli/doctor-command --allow-root

Once we have it installed, we can go to the folder where we have the WordPress to analyze.

cd /home/example.com/public_html/

Once we are in the folder where we have our WordPress, we can make a series of queries.

The first thing we will do is list all those analyzes that are possible.

wp doctor list --allow-root

This will return the list of items to review.

+----------------------------+--------------------------------------------------------------------------------+
| name                       | description                                                                    |
+----------------------------+--------------------------------------------------------------------------------+
| autoload-options-size      | Warns when autoloaded options size exceeds threshold of 900 kb.                |
| constant-savequeries-falsy | Confirms expected state of the SAVEQUERIES constant.                           |
| constant-wp-debug-falsy    | Confirms expected state of the WP_DEBUG constant.                              |
| core-update                | Errors when new WordPress minor release is available; warns for major release. |
| core-verify-checksums      | Verifies WordPress files against published checksums; errors on failure.       |
| cron-count                 | Errors when there's an excess of 50 total cron jobs registered.                |
| cron-duplicates            | Errors when there's an excess of 10 duplicate cron jobs registered.            |
| file-eval                  eval(.*base64_decode(.*.  |
| option-blog-public         | Confirms the expected value of the 'blog_public' option.                       |
| plugin-active-count        | Warns when there are greater than 80 plugins activated.                        |
| plugin-deactivated         | Warns when greater than 40% of plugins are deactivated.                        |
| plugin-update              | Warns when there are plugin updates available.                                 |
| theme-update               | Warns when there are theme updates available.                                  |
| cache-flush                wp_cache_flush() function.          |
| php-in-upload              | Warns when a PHP file is present in the Uploads folder.                        |
| language-update            | Warns when there are language updates available.                               |
+----------------------------+--------------------------------------------------------------------------------+

To do a test, for example autoload-options-size we can run the following command.

wp doctor check autoload-options-size --allow-root

That will return a message similar to this:

+-----------------------+---------+-------------------------------------------------------------------+
| name                  | status  | message                                                           |
+-----------------------+---------+-------------------------------------------------------------------+
| autoload-options-size | success | Autoloaded options size (13.78kb) is less than threshold (900kb). |
+-----------------------+---------+-------------------------------------------------------------------+

The status can be one of the following:

  • success: everything is correct
  • warning: there is something to improve
  • error: there is a problem

About this document

This document is regulated by the EUPL v1.2 license, published in WP SysAdmin and created by Javier Casares. Please, if you use this content in your website, your presentation or any material you distribute, remember to mention this site or its author, and having to put the material you create under EUPL license.