WordPress is actually the most used CMS platform, with over a good 60% of the market share. This means that in a global average on the wild Internet, more than half of the websites (powered by a CMS) runs WordPress. Unfortunately, after a few years of usage and a medium size database (from 500 Mb to 2 Gb) your Web Application is inevitably getting slow.
Your first step will be to apply all best practices (upgrade WordPress and PHP, activate OPcache, enable Gzip compression, parallelizing HTTP requests, use a cache plugin, move static contents on a CDN, etc …). Unfortunately, in some specific cases, this will not fix your problem and you will have to go deeper to catch bugs.
Patience and persistence make an unbeatable combination for success. As there is no magic solution at this point, you will have to go deep in WordPress core to identify bottlenecks. Also, most of the time, there is no unique responsible, but several causes or a wrong combo. To dive into WordPress architecture correctly, here are a list of tools, you should use.
What’s happening ?
Use”Inspect Element” in Chrome
Use the Network panel to record information about each network operation on a page, including detailed timing data, HTTP request and response headers, cookies, and more. You can view load information in a timeline (#3) and identify any resources being served over 1 or 2 seconds.
Filters (#2) are also useful. Monitor Ajax requests with XHR, to ensure asynchronous content is served as speed of light. At the bottom of the screen (#5), ensure total weight of your elements are sized to match your expectation. Again there will be no magic to speed up your website : Average should not go above 1 Mb for a single page, especially your front page.
If you don’t feel confident to use “Chrome Dev Tools”, you can go to any online services and get the same result. For example, Pingdom is a well-known good choice.
Use WordPress specific tools
WP_DEBUG & Debug Bar
WordPress comes with specific debug systems designed to simplify the process as well as standardize code across the core, plugins and themes. Once you switch your website into Debug Mode using define( ‘WP_DEBUG’, true ), you can hunt any Warning or Error messages which could be responsible of your issue. An other less common flag, but very important is SAVEQUERIES. This saves the database queries to an array and that array can be displayed to help analyze those queries. The constant defined as true causes each query to be saved, how long that query took to execute, and what function called it.
P3, Plugin Performance Profiler
This plugin creates a profile of your WordPress site’s plugins’ performance by measuring their impact on your site’s load time. Most of the times, WordPress sites load slowly because of poorly configured plugins or because there are so many of them. This plugin will help you to determine if slowness is cause by a specific plugin or “WordPress it-self” (identified as WP Core in this profiler).
Server side troubleshooting
Optimizing MySQL databases
A database can never be too optimized, and DBAs will never be completely satisfied with the performance of their creations. As your MySQL databases grow in size and complexity, taking full advantage of the optimizing tools built into the MySQL Workbench becomes increasingly important. Attempts to optimize SQL tables automatically via the OPTIMIZE TABLE command often go nowhere. From my point of view, the first step is to use “mysqlcheck” at the command line.
$ mysqlcheck -o --all-databases
Find SQL slow queries
Time of execution of a specific SQL query can be long (over 1 or 2 seconds). PHP process will wait for the database server to send back results before sending any HTML to your client’s browser. You can monitor SQL times execution on your server by enabling slow queries log (my.cnf) :
set global log_slow_queries = 1; set global slow_query_log_file = <some file name>;
Check all SQL Queries
In some cases, the bug is not the time of execution of a query, but the number of times it happens for a unique HTTP hit. To check this issue, you will record all SQL queries and number of time it is executed. Adapt you MySQL configuration (my.cnf) this way :
log = log_file_name
Debug PHP code with X-debug
Xdebug is a free and open source project by Derick Rethans and is probably one of the most useful PHP extensions. It provides more than just basic debugging support, but also stack traces, profiling, code coverage, and so on. The log file created by Xdebug can be small or large depending on what the application is doing. Also, it’s not really reader friendly. You’ll want to use programs like KCachegrind or Webgrind to view them. KCachegrind is a profile data visualization tool for KDE, which needs a Unix environment to run, whereas Webgrind is a web-based tool. Opening the profiling long file in KCachegrind will show the cost of each PHP function call starting at ground zero.
How can I help ?
As you you read in this article, there are a lot of tools to use for performance troubleshooting. Debugging can take several days but will always end up by locating the guilty piece of software, plugin, theme or custom code. While WordPress acts differently when you are identified or not, you should not forget to test using private navigation in your browser. When working as a freelance IT consultant, my goal is to help you to locate you current problems but also to understand, and prevent it to happen again and again. I can also make sure you WordPress website is optimized the right way.