The background
A few weeks ago, one of the companies I'm partly involved in purchased a very popular website in the Swedish real estate field. The website we bought was essentially a Wordpress site with some interesting customizations. The new website ranks very good in Google and the other major search engines and the amount of organic traffic that flows in to that site is simply fantastic.
As soon as the deal was done, the team copied the website to a new production environment and started working with the graphic appearance so that the new site would resemble the existing old site. Half way through the work, the CEO of started asking questions about the performance. In short, it was terrible. A page would take anything from 4 to 10 seconds to load and sometimes it would even time out entirely. A few different efforts was made to explain and remedy the problem, but it turned out that there was a lot of guessing involved and none of the guesses made was the right one. Yesterday, I did the right thing and took it to the test bench.
Take it to the lab
They say that once you know how to use a hammer really well, all problems starts to look like a nail. I guess that's starting to become true for me, only in my case the hammer is loadimpact.com. My idea was to copy the Wordpress blog in question to an environment where I could examine it under load, so I did just that. I looked in the back of my closet and found this old 2.1 GHz Quad code with 8Gb RAM machine that was just collecting dust (kidding, I use it all the time). The server has a complete LAMP stack so both the web server and the mysql server resides on the same hardware.
A few short words about how to move a Wordpress installation. It's not complicated, but you can end up with a few really non intuitive problems if you don't get it right. However, if you do it right, it's a 5 minute task (plus the actual time to move large files across the Internet). Here's how I got it right the second time:
Move all Wordpress files to the test environment. There isn't really that many ways to fail but you need to make sure that the web server(I'm assuming everyone uses Apache2 here) on the new host has read access to the destination folder.
Get a DNS entry for the new destination. In my case, all the customizations made to the blog was assuming that the installation resided in the root folder of the webserver, all images was referred to with src="/images/foo.jpg" etc. So I setup a subdomain that pointed to the web server and set up a new virtual host that pointed directly to the Wordpress installation on the new web server
Copy the database. There's some room for error here. I used phpmyadmin to create a database dump from the production environment with the intention to restore it on a database accessible from the test environment. When restoring it in the test database, I used the command line:
mysql dbname -u username -p < dumpfile.sql
That went really well, the database was recreated in no time and everything looked really good. But. That doesn't work. Wordpress uses uft-8 as default character set and in the above line the mysql command line tool will assume that the file is ISO-Latin-1. Chances are that it won't really hurt you at all but if it does, the errors will be far from intuitive. I ended up with error messages like:
Warning: array_keys() [function.array-keys]:
The first argument should be an array in wp-includes/widgets.php on line 657
The fact that you're looking at a character encoding problem here isn't really obvious right? But the explanation is farily simple. Worpress uses the PHP internal functions serialize and unserialize to transform complex structures like arrays and objects into strings that are suitable for storing in a database. The format used will for instance describe a string like this: s:10:"räksmörgås". The number 10 tells PHP how many characters to expect in the following string. When exporting the above example into a text file, the file on disk will look something like this: s:10:räksmörgÃ¥s. Now, if mysql thinks that the file you are importing is encoded in ISO-Latin-1, the content of the database will look exactly as it did on disk. Later when Wordpress is asking PHP to unserialize the expression s:10:räksmörgÃ¥s PHP will kindly reply that it can't be done and Wordpress will end up feeding funny looking strings into functions that would expect for instance and array as input parameter. The above error message I got was one of those examples. But the remedy is really simple, import the dumped file with this command instead:
mysql dbname -u username -p --default-character-set utf8 < dumpfile.sql
Adjust to the new environment. The last thing that needs to be done when moving a Wordpress installation is to make sure Wordpress knows that it have moved now. There are two important places to change. First, the file wp-config.php needs to be updated with the new database credentials. Second, the table wp_options (could use a different prefix than "wp_" in your installation) stores the URL of the blog in two places, you will find them both using the following query:
SELECT * FROM wp_options WHERE option_name in('siteurl','home');So, with the above steps taken care of, the Wordpress installation now works as expected in my test envornment. It could have been done in 5 minutes, but I ended up spending a little more due to the database character encoding experiment, but at least I learned something from it and hopefully so did you by reading about it.
Take it to the lab
Time to bring out the tool box. In my case, it's loadimpact.com account and a ssh account on the target machine. What I like to do is to put load on the application and wait for white smoke to appear somewhere. Typically, I'm looking for signs showing that the application is too hard on the database, is using too much CPU or is memory hungry. Of we go:

Yep, there's a problem with this blog alright. Even at 10 concurrent users, we have response times around 7 seconds. Interestingly though, the load times seem farily constant as we put additional load. At 50 concurrent users, we see about the same average response time as with 30 or 40 users. As this test was running, I was watching the server to see where the white smoke would appear. I didn't really have a favorite suspect so I began with using top:

The screenshot shows what top looked like with 50 concurrent users (remember that the response times here was in the range of 10 seconds). There's a couple of apache2 processes that's actually not that hard on resources. 4% CPU each and 0.3% memory (on a fairly powerful machine though). The mysqld process seem calm, it shows up in the list but uses very little CPU and memory resources. This actually looks fairly good. But let's go over the ususal suspects:
RAM memory actually looks really full, only 200 Mb free, but that's actually just how Linux works with memory (the same numbers on a Windows machine would be awful). Before I see the kswapd process showing up in top, I won't really consider available RAM to be a problem. So conclusion #1 is that we don't have a problem with RAM memory.
CPU also looks good. None of the visible processes in top uses that much CPU. Also, take a look at the header. 95.1%id actually means that the server CPU is idling 95.1% of the time. Conclusion #1 is also easy, we don't have a problem with CPU usage.
Database usage looks good as well. Most of the work is carried out by the mysqld process that we already saw was very quiet. Since we actually have the web server and the database server on the same hardware, excess database usage would also show up as higher total CPU usage. But to be sure, I'll have a look using the tool mytop:

mytop shows the currently running queries that mysql is handling at the moment. If we had any queries that took a really long time without consuming a lot of CPU, we'd catch them here. The screenshot above is takes while there was 50 concurrent users hitting the blog and the response times was in the 10 second range. It's clear that the delay in response times has nothing to do with poor database performance.
So, none of my three usuals suspects seem to be guilty this time. What else could there be?
The fourth suspect
The fourth suspect that I came to think about was network performance. Long response times paired with little activity on the server could indicate that there is some other resource on the network that is taking the load. My test server isn't showing any signs of activity, but what about other servers or the network itself? The next handy tool would be to bring out iftop. Here's what the screenshot looks like:

iftop will tell you what other hosts your server is connected to at the moment and how much bandwidth each of those connections are using at the moment. Looking at the list I could explain the reason behind each and everyone of the foreign hosts except for two of them. The last one on the list is an internal host on the same network and that's simply a file server that shouldn't come in to play at all. snik1.gatorhole.com is the server that's putting the load on my machine, so it's only natural that it shows up here. One of the other hosts is the one I'm using ssh from, so that one was also expected. The two I'm curious about was lillamy.ballou.se and s114.loopia.se.
Ballou.se is the domain name of our hosting provider for the new site, I find it interesting that we'd be hitting it with traffic. Loopia.se is the domain name of the OLD hosting provider for the site we're trying to test. That's even more interesting! Perhaps the application have some hard coded configuration that makes it look up things in the old environment!
Any such configuration would either be stored in the database or in the file system so I went there to look. First, I was using grep to search the file system for any references to the old domain name or hosting provider but it turned up blank. Next I was a bit lucky. Knowing that Wordpress stores a lot of information in the wp_options table I did a search for it in phpmyadmin:
SELECT * FROM `wp_lo_options` WHERE option_value like '%lokaler.nu%'
"lokaler.nu" is part of the domain name of this application and the DNS records are actually still pointing to the old server. I got roughly 10 rows back and after looking at them a bit the one that caught my attention was a the configuration for a RSS Widget. This widget would get the latest 6 news items from the blog itself. Instead of using a Widget such as "Recent Posts" or similar, the administrator setting this up had opted to get the news via RSS. Just replacing that Widget for the more natural "Recent posts" was exactly what I was looking for. The next test was much better:

By removing the RSS Widget and getting the results directly from the internal database was the key. The graph is now much more normal and for a normal 10-20 concurrent users we're in the comfortable 1-2 second range. We still have issues to look at and the next step is to install one of the better Wordpress cache solutions, but the most critical performance issue is resolved. Done.
Conclusions
So, did we learn anything new today? Well, yes I'd like to think so. First and foremost, using a load testing tool to find out what's wrong with an application is actually a very good idea. Putting load on a web application is a good way to make the performance problems stand out a lot more. In this case, the tools used to examine what's going on on the server all takes a little time to produce interesting numbers. A single snapshot of top is useful, but looking at it for 60 seconds is an order of magnitude better. The same goes for all the other tools involved. Even getting to see the s114.loopia.se name show up in iftop would have been impossible or at least required a lot of luck if we had to generate the traffic using manual refresh in Firefox/Firebug.
Second, if you're using an RSS Widget in your Wordpress blog, please be careful. If you're using it to draw news from your own blog, consider using a Widget that can get data directly from the database instead. If you are pulling news from an external source, make sure you use a working cache module.







