Archive for the ‘Web Development’ Category

Before You Implement Caching On Your Website

Thursday, October 8th, 2009

Ideally you would have designed your website or web app from the ground up to take advantage of caching. That is not always possible in the real world so before you implement caching on the site its useful to actually find out exactly what is causing the site to be slow. This is called profiling.

What is Profiling?

When we profile our application we are trying to find the functions, methods or sections which are taking too long. Nobody writes perfect code first time so it is useful to investigate what happens in your code, when it happens and how long that takes. It could be one loop is happening too often or maybe there’s an SQL query that is really killing the performance of your website. It could of course be anything and that is why we need to profile.

If you want more background, have a look at the profiling Wikipedia article.

Some languages have a built in methods of profiling, we’re going to profile a PHP script with Xdebug’s built in profiler. You are going to need to install XDebug.

How Do I Install Xdebug?

I’ve included basic instructions below for Mac OS X and MacPorts, FreeBSD, Ubuntu or Windows (which you use at your own risk) but if you’re using something else Google “install xdebug [platform]” and you’ll probably find something. But you already knew how to use Google anyway, didn’t you?

MacPorts

Kevin Hallmark’s instructions Getting xDebug Working on Mac OS X with MacPorts

FreeBSD

Do this as root (or sudo)

# cd /usr/ports/devel/php-xdebug
# make install

The instructions the port gives you after it installs didn’t work for me. I had to put:

zend_extension=/usr/local/lib/php/20060613/xdebug.so

in /usr/local/etc/php/extensions.ini which worked.

Ubuntu

HOWTO: Install xdebug for PHP5 at the Ubuntu forums.

Windows

If your doing your PHP development on Windows you should consider using a development environment that resembles the live environment you are deploying to. See my previous post about developing in virtual machines. However you can use Xdebug with PHP running on Windows by installing the .dll extension. Instructions here.

Finally…

If you haven’t already done so, put the following in php.ini and alter the path of xdebug.profiler_output_dir to something you can access from your desktop development environment.

xdebug.profiler_enable=1
xdebug.profiler_output_dir=/tmp

Remember to restart PHP and/or your web server to ensure your configuration changes have taken effect.

Using the Xdebug Profiler

The Xdebug profiler outputs files in cachegrind format. You now need something to read these files.

The interface of each is slightly different but they essentially show in a tree format the execution of each function and method and how long it takes. It is then possible to find the bottlenecks in your code.

Lets try an example. Run the following script:

<?php

for ($i = 0; $i < 1000; $i++) {
echo "$i\n";
}

?>

Now open WinCacheGrind, MacCallGrind or KCacheGrind and open the cachegrind file from wherever your specified as xdebug.profiler_output_dir. You will see that the execution hardly takes any time at all. Now alter your test script to the following:

<?php

function echo_i($i)
{
echo "$i\n"; } for ($i = 0; $i < 1000; $i++) {
echo_i($i);
}
?>

The cachegrind files are named with the process id so the later dated one with (probably) the higher number at the end will be the most recently executed script. The above change is a simple example but when you load the second cachegrind file you will see that calling a function has a considerable overhead. Now I’d recommend executing a more complicated script, maybe the homepage of your website. You can follow exactly what happens, what takes time and see what can be improved.

Development Environments

Tuesday, May 26th, 2009

For us web developers it’s advantageous to have a completely functional local setup of the site we are working on. It means we can continue to work (at least to some degree) when the Internet goes down. It means we can work on the train. It means we don’t need to wait around for our Systems Administrators to add an extension or new feature to the server.

When I’m concentrating on a particularly difficult problem, I like to pick up my laptop and go and sit in an unused meeting room where I can properly concentrate on the issue at hand – unplugged from the network – without distractions from colleagues, Facebook, Twitter or whatever the latest online fad is.

But having a development environment on your box is not without it’s flaws. Windows users often end up developing on something which is completely different from their production environment. Mac OS X and Linux users have to basically learn system administration and install their environment with MacPorts. Not a bad skill to learn but often not all members of the team are capable of doing this without losing many man hours. It would be much better if one of your more senior developers could do this once for everyone.

This is where virtualization comes in. Virtualization is essentially running another Operating System like an application on your Operating System. Sounds like an overkill doesn’t it? Well the benefits are pretty stark:

  1. You don’t  lose all those man hours as each developer on your team installs,  updates and then reconfigures their development environment multiple times during the project
  2. You can better match the development environment to production environment. Run FreeBSD on your live web servers? Run FreeBSD on a virtual machine.
  3. New starters can simply copy the virtual machine (essentially a file) and start working straight away.
  4. When one of your key developers makes massive changes, he can just redistribute a modified virtual machine and everyone can start working again.
  5. Hardware failure? Bust hard disk – just copy the virtual machine

You can even continue to use your favourite text editor. Just setup a share between the host and guest machine. There are quite a few software options to run your virtual machine, but I’d recommend VMware. It does a sterling job. A free alternative from Sun is VirtualBox.


Thoughts & Ideas About Web Development is Digg proof thanks to caching by WP Super Cache!