Home » Zend Framework, css, headline, php, user experience

5 tips and tools to optimize your php application - Part 1 simple

1 December 2008 1,128 views 9 Comments

With php becoming a more mature language and frameworks becoming a standard practice, it’s increasingly important to analyze your code and test the performance where possible.

Frameworks and libraries can speed up the development time and make code maintenance simpler, but it’s important you understand how they work.  It’s not just the framework itself but the entire footprint your application leaves when a user visits a specific page.  Ask yourself what’s involved from the database, to the code/framework, and then to the client through http requests.

After experiencing some issues with an application running the Zend Framework, I realized there were a lot of things I was not doing to optimize my application.  There are simple and more complex actions you can take to speed up your application.  Since time is precious, and developers with an expertise on optimization are not always available, I’ve split this post into two sections (simple and more advanced).  This post focuses on the simple part.

Simple tips and tools

  1. Use Firebug with YSlow
    Firebug and YSlow are powerful tools to analyze the front end of your site.  Run the most common request on your site and see what YSlow says.  Expect a low grade, probably a D or lower.  If you can reach a level C or greater you’re doing really well as some of the requirements are not practical for most applications.

    How-to:
    Net: How many calls are there?  Try to minize these calls and definitely remove any 404 calls as they can eat up server resources.

    Console:  Do you have a lot of POST calls?  Some libraries, like dojo’s javascript framework, can make a lot of POST requests.  Reduce these if possible.

    YSlow: Rules I would pay attention to:
    1. Make fewer HTTP Requests
    3. Add an Expires Header
    4. Gzip components (server related)
    5. Put CSS at the top
    6. Put JS at the bottom (not always possible)
    7. Avoid CSS expressions
    10. Minify JS

    Resources:
    http://getfirebug.com/
    http://developer.yahoo.com/yslow/
    http://developer.yahoo.com/performance/rules.html

  2. Minify Javascript and CSS
    Minification is the process of compacting your scripts by removing extraneous content such as white space.   In addition, minification can make attempts at refactoring the code to remove redunancies.  If you want to play it safe, only remove white space to ensure your code is still running. The Yahoo compressor is one of the best minifiers out there and reduces both Javascript and CSS by a significant percentage.

    How-to:
    java -jar yuicompressor-x.y.z.jar -o style_minified.css style.css

    add the “–nomunge” to only do whitespace compressions.

    Resources:
    http://developer.yahoo.com/yui/compressor/
    http://dojotoolkit.org/docs/shrinksafe
    http://dean.edwards.name/packer/

  3. Merge css and js http requests- manually or automated
    Reduce the amount of calls you make to various CSS and JS files by merging them to one URL.  This is more work but will reduce the overhead.

    How-to - manually:
    This is really easy, take your css and copy and paste them together into one file.  Make sure your preserve the order of your css.  The same applies to your Javascript files.

    How-to - Automated - google or your own custom:
    Google has a php script which can take various sources and return them as one.

    Resources:
    http://code.google.com/p/minify/
    http://reinholdweber.com/?p=37

  4. CSS Background Images are bad - usually
    Using a div for all of your images with a background image is not that great for performance as it requires more calls.  I’ll be the first to admit it’s my favourite way to design, but you should try to avoid them where possible.  In addition, for hover states try using one large image with background-position calls to show specific parts.

    How to:
    Use background-position to select which portion of the large image you want to show.  There isn’t much more to explain.

  5. GZip
    Gzipping files on the server side will reduce the amount of overhead significantly.  It adds a bit more on the server side, but generally the pay off should be well worth it.

    How to:
    It depends on your server situation, but here’s a snippet:
    mod_gzip_on Yes
    mod_gzip_item_include mime ^application/x-javascript$

    Refer to http://snipplr.com/view/9017/gzip-your-components/ for details

    Resources:
    http://developer.yahoo.com/performance/rules.html#gzip
    http://snipplr.com/view/9017/gzip-your-components/

Conclusion
That wraps up the simple part of this tutorial… really, that’s it!  The focus of these posts is to keep them simple and easy to use.  You may be surprised at how much faster your application runs when you apply these techniques.  I hope you found these useful or at least agree that these are good practices to apply.  In the next part I will examine more advanced methods of optimizing your application.

Feedback
Your feedback is always appreciated.  In addition to commenting, if you have more resources, please add them to my growing list of links through this buncholink

It’s a simple way to organize a group of links together.

Share/Save/Bookmark

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...

9 Comments »

  • david said:

    Another way that is becoming popular is to use subdomains for static files. This loads the page faster because the browser parallel loads from the different domains.

  • andre said:

    interesting… I guess that would be like what facebook does with static.facebook.com?

    Thanks for the tip!

  • Matthew Weier O'Phinney said:

    Just a note, but these aren’t specific to PHP — they’re good rules for *any* website. :)

  • andre said:

    Hi Matthew,

    Good point, the second part will have more php specific tips but when I first created this list it was one big one that I broke down. I guess it just happens that the simpler more generic ones ended up being non php. The only one that is somewhat php specific is using a php script to combine various assets (JS, CSS) together. But of course you could easily have an application running on another language calling a server running that php script.

  • Adrian said:

    Interesting point with #4, “using one large image with background-position calls to show specific parts” - I never thought much about the benefit of this technique in terms of reducing the number of calls sent to the server but that seems to make sense. That CSS technique, BTW, also known as “CSS Sprites” was covered well by Dave Shea in AListApart: http://www.alistapart.com/articles/sprites/

  • Andre Liem’s Blog: 5 tips and tools to optimize your php application - Part 1 simple : WebNetiques said:

    [...] Liem has posted five simple tips (part one of a two-part series) on ways to optimize your PHP [...]

  • Andre Liem’s Blog: 5 tips and tools to optimize your php application - Part 1 simple : Dragonfly Networks said:

    [...] Liem has posted five simple tips (part one of a two-part series) on ways to optimize your PHP [...]

  • Wesley Mason said:

    Minify (http://code.google.com/p/minify/) isn’t actually a Google PHP app, it was originally written by Ryan Grove (wonko.com) who now works for Yahoo!, and is maintained by Steve Clay (mrclay.org), it is merely hosted on Google Code, Google’s code repository and project tracking service (similar to github).

    It also does more than resource concatenation, it also does minification similar to YUI Compressor, but also does conditional serving of the files (plus automatically handling compression algorithms and timestamps to make sure the right content is served to the right client), and is fully extensible to include your own minification methods or use as yo

  • andre said:

    Thanks Wesley, I’ll update the post with those details. I guess we all owe Yahoo even more thanks then. Funny how I just assume that anything on google’s repository is google’s creation…

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.