@motchi0214 に教えてもらったんだけど、パフォーマンス調査用に WP-CLI チームが作って公開しているサブコマンド wp profile
がとても便利です。
Figure out why WordPress is slow with wp profile
wp-cli/profile-command
これ使うと、ドリルダウンしていって処理に時間がかかっているところを特定できます。
導入方法
wp-cli 入ってること前提、これ書いてる時点での僕の環境では wp-cli のバージョンは 1.5.0。
導入は簡単、GitHub の README.md に書いてある通り、以下のコマンド実行するだけ。
$ wp package install wp-cli/profile-command:@stable
Installing package wp-cli/profile-command (@stable)
Updating /home/amimoto-user/.wp-cli/packages/composer.json to require the package...
Using Composer to install the package...
---
Loading composer repositories with package information
Updating dependencies
Resolving dependencies through SAT
Looking at all rules.
Dependency resolution completed in 0.183 seconds
Analyzed 10765 packages to resolve dependencies
Analyzed 681386 rules to resolve dependencies
Package operations: 1 install, 0 updates, 0 removals
Installs: wp-cli/profile-command:v2.0.1
- Installing wp-cli/profile-command (v2.0.1)
Writing lock file
Generating autoload files
---
Success: Package installed.
PHP Notice: Trying to access array offset on value of type null in phar:///usr/share/wp-cli/phar/wp-cli.phar/vendor/wp-cli/package-command/src/Package_Command.php on line 1244
使い方
これもシンプル、大枠から見ていってドリルダウンしていく感じで。
まず、全体の処理時間確認
$ wp profile stage --fields=stage,time,cache_ratio
+------------+---------+-------------+
| stage | time | cache_ratio |
+------------+---------+-------------+
| bootstrap | 0.3982s | 99.79% |
| main_query | 0.0074s | 99.29% |
| template | 0.1109s | 99.82% |
+------------+---------+-------------+
| total (3) | 0.5165s | 99.63% |
+------------+---------+-------------+
bootstrap が時間かかってるみたいですね、bootstrap のどこが具体的に時間かかってるんだろう?
$ wp profile stage bootstrap --fields=hook,callback_count,time,cache_ratio --spotlight
+--------------------------+----------------+---------+-------------+
| hook | callback_count | time | cache_ratio |
+--------------------------+----------------+---------+-------------+
| muplugins_loaded:before | | 0.1122s | 100% |
| plugins_loaded:before | | 0.1557s | 99.31% |
| plugins_loaded | 30 | 0.0616s | 100% |
| after_setup_theme:before | | 0.0134s | 100% |
| init | 50 | 0.0241s | 100% |
| wp_loaded:after | | 0.0292s | |
+--------------------------+----------------+---------+-------------+
| total (6) | 80 | 0.3961s | 99.86% |
+--------------------------+----------------+---------+-------------+
plugins_loaded
hook を深掘りしてみるか
$ wp profile hook plugins_loaded --fields=callback,location,time --order=DESC --orderby=time
+------------------------------------------------------------------+-------------------------------------------------------------------+---------+
| callback | location | time |
+------------------------------------------------------------------+-------------------------------------------------------------------+---------+
| Jetpack::load_modules() | jetpack/class.jetpack.php:1749 | 0.0268s |
| Jetpack_Sync_Actions::init() | jetpack/sync/class.jetpack-sync-actions.php:17 | 0.0109s |
| Jetpack::plugin_textdomain() | jetpack/class.jetpack.php:928 | 0.0093s |
| wpcf7() | contact-form-7/settings.php:89 | 0.0069s |
| wp_maybe_load_widgets() | wp-includes/functions.php:3681 | 0.0042s |
| LogBook\plugins_loaded() | logbook/logbook.php:23 | 0.0017s |
| Jetpack->check_twitter_tags() | jetpack/class.jetpack.php:1997 | 0.0005s |
| TotalHacks->plugins_loaded() | wp-total-hacks/wp-total-hacks.php:125 | 0.0004s |
| BA_Edit_Author_Slug->load_textdomain() | edit-author-slug/includes/classes/class-edit-author-slug.php:330 | 0.0003s |
| NginxChampuru->plugins_loaded() | nginx-champuru/nginx-champuru.php:129 | 0.0002s |
| gist->plugins_loaded() | oembed-gist/oembed-gist.php:25 | 0.0002s |
| function(){} | mu-plugins/cloudfront-fix.php:2 | 0.0002s |
| wp_maybe_load_embeds() | wp-includes/embed.php:176 | 0.0001s |
| geoip_detect_load_textdomain() | geoip-detect/init.php:25 | 0.0001s |
| Jetpack->extra_oembed_providers() | jetpack/class.jetpack.php:1728 | 0.0001s |
| Jetpack_Sync_Actions::initialize_woocommerce() | jetpack/sync/class.jetpack-sync-actions.php:295 | 0.0001s |
| Amazonpolly_I18n->load_plugin_textdomain() | amazon-polly/includes/class-amazonpolly-i18n.php:34 | 0.0001s |
| Jetpack_Sync_Actions::initialize_wp_super_cache() | jetpack/sync/class.jetpack-sync-actions.php:308 | 0s |
| function(){} | hacks/hacks.php:255 | 0s |
| geoip_detect_defines() | geoip-detect/init.php:2 | 0s |
| _wp_customize_include() | wp-includes/theme.php:2772 | 0s |
| geoip_detect_maybe_upgrade_version() | geoip-detect/upgrade-plugin.php:73 | 0s |
| NginxChampuru_Caching->wp_cron_caching() | nginx-champuru/includes/caching.class.php:99 | 0s |
| Jetpack->check_rest_api_compat() | jetpack/class.jetpack.php:1844 | 0s |
| mycategoryorder_init() | my-category-order/mycategoryorder.php:12 | 0s |
| function(){} | c3-cloudfront-clear-cache/module/model/fixtures/avoid_preview_cac | 0s |
| | he.php:12 | |
| YellowTree\GeoipDetect\DataSources\Auto\AutoDataSource->on_plugi | geoip-detect/data-sources/auto.php:72 | 0s |
| ns_loaded() | | |
| Jetpack_3rd_Party_Domain_Mapping->attempt_to_hook_domain_mapping | jetpack/3rd-party/domain-mapping.php:41 | 0s |
| _plugins() | | |
| Nginx_Mobile_Theme->plugins_loaded() | nginx-mobile-theme/nginx-mobile-theme.php:48 | 0s |
| function(){} | hacks/hacks.php:23 | 0s |
+------------------------------------------------------------------+-------------------------------------------------------------------+---------+
| total (30) | | 0.0622s |
+------------------------------------------------------------------+-------------------------------------------------------------------+---------+