April 26, 2010 Hatem

Speed up your PHP website, a consultant’s guide

Yesterday a friend was requesting a MYSQL/PHP Consultant to speed up his websites, and today I was reading few comments and solutions on his facebook : Use APC, mem_cache for PHP, mysql query caching, use nginx instead of Apache, Use mysqli extension instead of adodb or mysql extension … etc. Of course all of these could be solutions, but unless you have previously located what the problem really is.

A website could be slow for many reasons, and to be able to fix it and make it run faster you have to first find problem. What you will need to know is :

1- Understand the business :

Before looking into PHP or MySQL, have a first look at the website(s) and answer these two questions :

– What is slow
– Why is it slow

Look at the page size, images, video, flash, … your issue might be in the client side and everything else might be good.
Use a tool like Firefox web developer extension and disable everything : CSS, Javascript, Images… and load the page as html to feel the difference.
The business here is NOT the business logic behind the website, it’s the interface that people use to interact with the website. If everything looks okay, you can go for further server-side investigations.

2- Understand the production environment : Operating system, PHP and MySQL versions, PHP extensions running, PHP and MySQL configurations.

Do not start optimizing code or database unless you know the exact problem, otherwise you have to start by understanding and optimizing the working environment. Some tweaks in PHP and MySQL settings might fix your problem and you won’t probably need to dig deeply into any code.

3- Understand the code :

If the websites you are looking to optimize are using a common framework or CMS, let’s say Drupal, WordPress, Zend Framework, Symfony… such websites you can deal with separetely since most optimization issues should be known and you will easily find your way to speed things up.

Otherwise, for custom code and custom application development, start with debugging and focus firstly on the database. That’s where most slow issues come from, especially if the concerned website was running fast in the beginning then become slow over the time due to database size going bigger, query & index issues…

4- Debug : detect and locate bottlenecks

Debuggers are your friends here, whatever the environment you are running. Answer again previous questions (What and Why) but this time with relation to the code itself.

You might consider running a stress test here to simulate real working environment. Such tests could help you understand more networking issues and make it easier to locate slowliness based on real scenarios. Test should also simulate real environment, and you will need to save speed result for comparison later.

5- Solution proposal and action plan

Now you can go ahead and set possible solutions : fixing bugs, caching code/queries, tweak your settings… Make sure your modifications will not affect other working website in the same environment – if any is somehow related or linked.

Finally you can run previous stress test with new configuration and updates to see the difference in speed gained. A report of previously mentioned steps should be written including result.

, , ,

(HBY) Consultancy