Of WordPress Caches and Fast PHPs

More attempts to improve the performance of my Wordpress site through new Caching plugins (Hypercache and DB Cache) and Dreamhost’s FastCGI.

Example of a Plug-In Framework
Image via Wikipedia

Improving the speed of this and my two other sites has always been a major issue for me. Ever since I’ve switched to WordPress I’ve never been fully satisfied with the loading time and it seems I’ve been trying since forever to improve it.  My main methods were through the use of caching plugins such a WP-Cache and later on WP-Supercache and through manual performance tweaking. For a while it seemed to have worked to a degree until again my performance started dropping without any apparent reason.

It was at this point that I jumped to the VPS offering in a desperate attempt to get a site which loads in this century. Again, for a while things looked to be working well but now and then I would get horrible site b0rks which would take me hours to troubleshoot and resolve. The latest one was the reason I discovered that WP-Supercache didn’t play nice with VPS and thus I had to find something else, or live with it in a state of half-on.

To my delight, it seems that now there are new caching plugins available which I can try. I already mentioned Hyper Cache last time and today I discovered DB Cache (h/t  diTii.com) which seems particularly promising, especially because it works not by caching the fully loaded page, but rather by caching the Database queries themselves. This is an interesting take on caching since it now can improve performance for web crawlers as well as normal users. It also provides an extra benefit to me since I’m proving a gallery through the wordpress interface, and that means that the database queries for that are also cached.

So I ditched Super Cache from all my blogs and installed DB Cache on the Division by Zer0 and the Wesnoth Journals and Hyper Cache on the ACP. It’s of course always difficult to figure out how much difference a caching plugin has done to your site. As of now, I can’t say I notice a significant difference on loading times with DB Cache, however I did notice that the number of SQL queries that are made each time the page loads have dropped from >60 to about 15 which means that there some difference.

I  have also noticed anotther thing. In the past it could take a few seconds before my site even started loading (I guess while it was running the SQL queries) but after that it would be displayed very quickly (especially if it had been supercached) whereas now, the site starts loading very quickly but it takes more time to actually finish loading the content, in effect loading in parts (first the header, then the content etc) but in a way that is much more exaggerated than before.

Another thing I also decided to do is to finally activate Fast CGI for PHP. I hadn’t done this before as it wouldn’t have made much difference when Supercache was in use but now that the code is executed every time, it seems like a good idea. There’s also the added bonus that for VPS, the Xcache opcoder is available which further improves php performance when on high load. I do not think it will make much of a difference as my problem is not one of traffic but it may come in handy for those rare reddit moments.

As of now, the performance seems comparable to SuperCache times and I am hoping that this time I will not have any more random Internal Server Errors. Unfortunately my WordPress admin panel is still quite slower than I’d like with loading times randing frmo 5 to 15 seconds or more on occasion. I honestly don’t know what I can do to fix that but at least the admin panel is not something that is used very often.

Next step will be to see if Hyper Cache is better than DB Cache and if they can both play well together for a combined improvement.

So what do you think of the current speed?

Reblog this post [with Zemanta]

This is why I like dreamhost

About a week ago I noticed a sudden spike in my Private Hosting memory consumption. And when I say a spike, I mean a spike

dreamhost ps

Literally within a day I was through the roof and I had absolutely no idea why. I had attributed my previous increase in consumption to a recent traffic increase but a jump like that was ridiculous.

I was fairly certain that this must be a malfunctioning script running somewhere but a quick ps -e didn’t give me any hints. So I turned to Dreamhost support. I fired a quick ticket and got my first reply within 2 days and unfortunately it wasn’t what I wished for.

Basically, the support told me that they cannot know what I am running on my site and thus cannot really help me. I should figure it out myself. Needless to say I was a bit disappointed.

Thanks for the reply Brian but I’m a bit dissapointed. Your reply was basically “we can’t help you. Figure it out yourself” which is a bit of a letdown compared to what I’m used from Dreamhost support.

The thing is, that in the Dreamhost PS information, you mention that 300Mb should be enough for a top 100 blog. I don’t even have a top 100k blog and I do not run any custom code. I run 3 wordpress blogs and 2 galleries and that’s it.

While I can imagine that some plugin is malfunctioning or possibly a site being hacked and running custom code, I’m not an expert on this stuff and without full access it get pretty impossible for me to figure it out.

I can understand that Dreamhost cannot be expected to troubleshoot everyone’s usage, but on the other hand for someone now really knowledgeable this sounds just wrong. From what my point of view, the “usage”, which I cannot verify in any way independently and does not have any visible basis, could just steadily keep increasing to 2000Mb and I couldn’t do anything about it other than just fork out money.

In any case, I’ll see if I can find anything myself

One more day passed and then I had a much better response. Another support person took over and gave me some tips to try as well as some possible command. ps auxe > serverlog

So I gave it a try and, lo and behold, now 4 misbehaving scripts were revealed. One was an instance of my very own complexlife plugin taking up 15% of the ram, while the other 3 where instances of the photodropper plugin, each taking a 10% . Altogether pretty much explained why my usage was so extreme.

So it was time to bring out my good friend –kill– and get me rid of some unruly processes.


Whoop-de-doop, my memory consumption is back to sane levels. This is why it pays to have some good support at your company. I’m happy once more 😀

Dreamhost PS activated!

Engage Warp speed…3!

I just got in Dreamhosts’ new shiny and exciting Private Server thingy after just 2 short days. In all honesty I was expecting at least a few weeks of waiting so I was quite surprised when I got an email informing me that it has been activated 😀

It may have to do with me sending a question their way from the support page, to ask them a few questions relating to this. You see, I was not certain how much of a difference that would make for me, if it was worth it and if I should go for MySQL or Web Server hosting.

You see, I’m still quite a small fry at the moment and between the 3 blogs I’m hosting, I’m not averaging a lot of visitors. I always assumed that by being this small, a shared hosting plan is the best choice. Unfortunately I kept running into performance issues and even though I optimized and tweaked, they just went on and I was sick and tired of having a single page take on average 5 seconds to load and, too often for my liking, upwards of 10. I was even afraid that I was starting to get on Dreamhost’s support nerves with all the support tickets I opened for performance 🙁

It was at the point where I was considering moving to another hosting service (possibly on a virtual server) that my eyes fell on the new Dreamhost PS option, suddenly appearing in my Dashboard. Quite the timing. The reason why I decided to stick with Dreamhost still is that I just love the merry way with which they do business and their support has been very nice in my experience so far. I just felt I should give them another chance.

The PS option mentioned that this is by invitation only however and googling around a bit, I found a few people talking about it but none had any real experience. They were mostly saying that it sounds like a good idea but they were not going to use it because so-and-so hosting was better. Nothing tangible. Since I exchanged a few emails with Dreamhost support and I am now live with it, I thought I might write a few things about it.

Dreamhost PS Performance

I’ve only had the thing running for a few hours now so I can’t make much of a comparison. It does feel much faster for me but it could just be the placebo effect.

PHP

I did try to run my heaviest page just to see the experience: From an average of 10 seconds (due to the heavy php involved) it dropped down to 2.5 secs. This seemed a major improvement, however on a second test I did 8 hours later, the speed was 12 seconds again. Not certain if it’s affected by mysql, slow feed replies or whatnot. Nevertheless, doing a few more tests, the average seems to be 2.2 seconds. This should be in part because of Simplepie’s feed cache.

Gallery

Here the difference is much more striking. My Gallery2 installation used to be unbelievably slow more often than not. I would be lucky if I could get times below 5 seconds per page. Now the average seems to hover at around 2 seconds again and this is, frankly, a very pleasant surprise. Even The Wesnoth Journals gallery seems to have picked up unbelievable speed and that was the one that drew quite a few complaints. Awesome!

WordPress

Here the speed difference did not seem so striking initially. Average speeds hovered at around 2 seconds again which is a speed that I was reaching after my own optimization quite often. The difference now of course is that the speed seems to stay at that average instead of spiking to 5 – 10 seconds without reason. I have not seen one page actually going higher than 5 sec. Same thing persists in both the ACP (which has a heavy theme and is not theme optimized at all) and the Wesnoth Journals.

However a susprise awaited me on my more thorough testing this afternoon. For the first time Ev4r, I get sub second speeds, consistently! And this is without WP-Cache activated! If anything makes the whole page exceed one second, it is the Intense Debate comment script who’s speed is dependent on external servers, and even then the delay is never more than one more sec. This is truly amazing. With WP-Cache, the speed seems to stay consistently subsec!

The WordPress Dashboard speed, as expected, has improved considerably as well and finally I don’t have to wonder why it sometimes takes so goddamn long to load :D. Average speed of Admin Panel items seem to be:

  • Dashboard ~ 2 sec
  • Plugins ~ 3 sec
  • Automated Plugin upgrade ~ 1 sec (!!!)
  • Settings ~ 1 sec
  • Write ~ 1 sec (!!!)

The multiple exclamation marks are on items that truly impressed me since they always seemed to take a inordinate amount of time to load (around 5 secs) and the change is most striking.

Drupal

Here the speed confused me a bit. In my single Drupal installation averga speed hovered at around 3 seconds where suddenly it dropped to subsec again. Cannot really say if the server had a momentary slowdown at the start.

And with that, my little performance check ended. I think the change is quite obvious and for now I am quite happy with the results. I am still curious to see how this will fare during the more active afternoon – night hours (for me, for you Americans it will be morning – afternoon). If things turn around considerably, I will write about it.

Some Dreamhost PS Q&A

During my email conversation with the Dreamhost support, I had the opportunity to ask a few more questions about the service in order to make sure it is right for my needs. I will place the answers here in case anyone else has similar queries.

  • Q: I experience considerable slowdowns with my wordpress installation and it’s certainly not my setup. I think it might be MySQL as I don’t see anything loading for a few seconds and then everything comes up together. However every time I tried to put a support ticket for this, I see the web server under heavy load. I’m now not certain if I should request MySQL or Web Server hosting.
  • A: Go for Web Server. From my results I can see that MySQL hardly plays much of a role after all. My speed improved dramatically from just a small Web Server hosting plan.
  • Q: If I request Web Server and see that it does not make much of a difference, can I switch to MySQL and vice-verca.
  • A: When you request either a web or MySQL PS, since it must be physically setup, you are requesting one or the other and they are not interchangeable. If you decide you want either, you must visit the provisioning page to request whichever you decide you would like and if you want both, you need to request both individually at the same page of the panel-
  • Q: How do I request both a Web Server and a MySQL PS? I can only select one through the radio buttons
  • A: Request one, then go back and request the other.
  • Q: If I decide I don’t want this service anymore (say, because it does not make much of a difference) can I return to my previous shared hosting?
  • A: They won’t be able to move you back if your usage is above what is expected for a shared server account which is the base 150 level of the PS service. As long as you are below that level consistently, then there shouldn’t be a problem moving you back to shared, or at least giving you a discount on it.
  • Q: The information also mentions that I can adjust my RAM and CPU usage in real time. However, if I adjust my usage within the month one or more times, how much am I going to be charged?
  • A: You will only be charged at that rate for as long as you keep the slider AT that rate. So it’s all pro-rated. If you keep it at 150 and then move it up to 1000 for an hour, then back down to 150, you will only be charged the 1000 rate for that hour. No more, no less. 🙂
  • Q: I see the slider gives me the max burst I can get as double. Does that mean that if my rate is not enough I can increase the slider only up to that burst?
  • A: No, once activated you can increase your rate to the max of 2300 at any time, even if you started at 150.
  • Q: I ordered it. How long does it take usually?
  • A: No idea. In my case it took 1 day 😀
  • Q: The rates are vague. How much should I order for a small site with just a few thousand per month?
  • A: Low. I have four worpdress and two gallery2 installations hosted at the moment and my load is around 100Mb of RAM. My CPU is still at 10. My biggest spike was when I disabled WP-Cache and loaded my lifestream 5 times where my RAM jumped to 155.

So, that was my initial review of Dreamhosts shiny new upcoming Private Servers. I hope I have provided you some information to make the correct choices 😉

How to hunt for WordPress performance hogs

So after my previous post on how I discovered my major causes of wordpress pain, I kept looking on what is causing my other delays. Eventually, while looking at my source code, I discovered that my wordpress page was always reporting how many SQL queries it took and how long they took to complete. Looking at the footer, this was done by this command:

<?php echo get_num_queries(); ?> queries. <?php timer_stop(1); ?> seconds

Now, I understood that this was very helpful but I couldn’t understand why it was returning a time like 10 seconds when the page was loading for 4-5. A quick google search for this code however did lead me to a page that was explaining how to Keep WordPress overhead down. Through there, I realized that those seconds were some sort of CPU time taken to draw those queries.

Through this then, I now had a semi-accurate way to monitor the impact of each plugin on my site. I only needed to figure out which parts of the site were hogging down the queries and increasing the load time.

I created a quick table in a Spreadsheet and started logging down the differences.

  • First, I made the performance text visible, so that I don’t have to look at the source code each time (You can still see it by scrolling to the bottom of the page)
  • I disabled the WP-Cache so that the queries are done every time
  • I then started disabling each plugin in turn and reloading a page. Then I compared that with the previous loading time (where the plugin was enabled.) I noted down the difference in queries and the average/approximate CPU time difference.

Unfortunately since the CPU time tended to vary from load to load, I couldn’t get an accurate number of the difference. The table I made in the end looked something like this

Plugin name Queries CPU Time Taken
Bad Behaviour 1 5
Hide Text 0 Negligible
Greeklish 0 Negligible
One click install 0 Negligible
SimplePie WordPress Plugin 4 Negligible
Security Scan 1 1

After I went through all the plugins, then I started doing the same thing with my various widgets and snippets of code in my theme that might be causing this.

Once this was done, I had one likely suspect plugin and a few widgets that were probably contributing to this slowdown. You see, my theme, a very heavily hacked HemingwayEx, was using some custom widgets in order to draw from WordPress the latest comments, recent posts and whatnot. These widgets queried the database each and every time a page would load which was quite frankly impractical.

Fortunately a short time before, I had discovered the very useful bundle of post plugins. They were also there to hunt for recent comments & posts and related posts and they also included a built-in caching mechanism! As sweet as it gets.

I quickly modified my widgets to run this code instead and the results were wonderful. By disabling also the single other plugin I found that was slowing things down, Bad Behaviour, I’ve now managed to drop the loading time considerably (or so it seems for the time being). Not only that, but I’ve managed to put a few new toys to use that do a better job than HemingwayEx’s built in functionalities.

Currently my site seems to load lightning fast compared to before but I’m not absolutely convinced this will not change once my shared hosting environment gets bogged down. Hopefully, even if that happens I will still have a much much better performance than before.

I am still stuck at around 65-80 queries per page load but I can’t figure out where they come from. I am hoping that I’ll eventually be able to trace it but for now it seems enough.

Btw, I just loved the functionality of the Post Plugins to measure their own execution time and report it back to you. This is something that all plugins should be doing in order to give the web designer an idea on what is going on with his site. I’m going to work to see if I can implement that in the Lifestream plugin I’m working on and then see if I can place it on other plugin as well.

Btw (2), Can anyone explain why more people don’t take advantage of the Plugin cache plugin? I am fairly certain that a number of heavy plugins (like Popularity contest) could make good use of it.

As always, if you have any more ideas on where to look and tweak to improve my WP performance even more, I’ll be glad to hear them (AKA Whining because no one is commenting on my blog :P).

PS: It’s a pity that I had to disable Bad Behaviour as it worked quite well until now. I also don’t like to reply too much on just a single anti-spam plugin. Unfortunately, the performance hit was undeniable and I’d rather my site doesn’t take an extra 3-4 seconds to load every time.

Squashing Wodrpess Performance Bugs

I think I’ve finally managed to trace down and smite all the things that were making my site so Gawd awfully slow. I had finally been annoyed enough by 30 sec loading times that I just had to do something about it.

Initially I thought it was with my host, and I did contact support about it via Email. They did check out the MySql slow logs and noticed some strange queries from BadBehaviour, but when this little plugin blocks around 1000 bots per week, it’s not worthwhile to disable it :).

In Dreamhosts’s opinion I should try and disable my plugins and then switch to another theme to check. Well, even though it was painful, I did it but it didn’t really make much of a difference. My site still seemed to be slow. However a test site I created had a quite normal speed. Perplexing

Initially I thought that it might just be the difference in DB size and indeed, my other WP sites, which have much less content that this, seemed to be loading faster (albeit slightly).

Fortunately a few google searches led me to some good content. Initally I discovered Firebug from a post explaining how it can help you diagnose WordPress performance issues. That was quite helpful to tell the truth. I managed to immediately spot that the WordPress Automatic Upgrade (WPAU) and the Popularity Contest were severely slowing down my Dashboard. I also found a few items in my theme that were just taking too long to load (like my FSF badge for some reason). Disabling them dropped the load time but I still had a problem.

You see, for some reason there is a 5-9 second loading time from the time I click on a link, to the time the page refreshes and starts to load. Firebug, unfortunately, will not give you any information on what is causing this, other than to tell you that the link is loading. It will not even take into account this initial loading time when giving you the total load time of the page (thus a page taking 13 secs will be reported as taking 4.5). Since this loading time dissapeared

This meant that I had no idea what was causing it, so back to the search I went. This time I managed to find a more interesting post on Improving WordPress performance which not only gave some great info, it linked to some other sources. Unfortunately, most of the tips given were to be used on a private server and were not really applicable to a shared hosting service like mine. Of course, this person was talking about receiving 500.000 hits a day, whereas I’m barely receiving 3000…a month. Still I applied the wp_config.php edit although it didn’t seem to make much of a difference.

My final hint came from wolfie who advised that I remove all widgets and links that point to other addresses as these may increase the loading time. I did that and I also took down the Popularity contest as well, just to see the results.

It seems it worked. The initial loading time has dropped to around 4-5 seconds. Although I don’t have as much bling in my site anymore, I am hoping that the increase in speed will make up for it. Unfortunately 4-5 secs is still quite bad and while I still don’t have any idea where to look in order to fix that, it’s at least better than 6-9 secs + 4 secs of extra stuff. With the WP-Cached page, the load seems to be instantanteous OTOH so at least that helps. Still for a ~100 hits per day, it certainly should not happen.

As an aftereffect, I decided to finally update this site to WP 2.5 as the functionality of Popularity contest was the only thing preventing it. At least I now have a snazzier Dashboard 🙂

After improving my performance I decided to also start a WordPress Performance page on the dreamhost service wiki. I would urge all of you, Dreamhost customers or not, to take a look and improve this page so that others that have similar issues can find a focal point to troubleshoot. I also hope to see tips specific for Dreamhost shared hosting customers eventually.

If there is a good WordPress improvement page or tips that I should check btw, feel free to let me know. Also, if you have any idea what causes these ~4 secs of initial loading delay, I would be really glad to hear it.