Open Source Goodie: Simple PHP profiler

I mentioned that this blog would cover the Open Source engine that was designed for and is powering

This is the first real chunk of goodness: a quick explanation of a simple PHP profiler written for the engine.  There will be links at the bottom of the page to get the source, but I’ll explain it a bit first.

What is a profiler and why do we need it?

A profiler helps you watch your code and time how long different pieces take. It can easily be turned on or off so that you don’t have a bunch of timers running on your live site. The reason that you profile code is to help find out which pages are running slowly, and more specifically which functions or queries are the real bottlenecks.

The first results of using this new profiler can be seen on the homepage of doItLater. The page has four sections with random content in them (pictures, movies, flash videos, and games) and one section which is a random mixture of all of those. This page has been slow as long as I can remember. I figured that it was just because I was running on a cheap shared host and there is a decent amount of images on the page. Later it became clear that other pages were loading much faster, so I jumped at the chance to use this newly-created profiler to track down the problem. Within minutes I could see that the first section was taking 15 seconds (WOAH!) to generate, and the entire rest of the page took about one tenth of one second.

After investigating, I found out that the query is inherently slow (now that I think of it… there is one trick I could do to speed it up, but that would take four queries), so I decided to cache just the results of that query for 15 minutes. This led to writing a simple caching class that I’ll probably release here also (the whole project is open source so you can get any of this any time you want from SVN).

The result: After the first slow page (each 15 minutes when we re-cache) we go from 15 seconds for a page-load down to 2/10 of a second. Full of win!

How do I use it?

Glad you asked! It’s actually really simple.

  1. Include the file in every other file… if you have a global file, that’s a great place to put the code:
    include_once 'profiler.php'
  2. Add the starting and ending code to every function you want to profile. That means that the first line of a function should be:
    and the last line (before the return statement if there is one) should be:
    PROTIP: The __METHOD__ macro fills out a good name for the function, including the class-name if there is one. If you would like to profile individual parts of functions (as I did on the main page) just wrap profiler_startSection([some unique name here]), profiler_endSection([same unique name here]) around the section of code you want to profile.
  3. Make sure the profiler is enabled. You can set RUN_PROFILER to true at the top of profiler.php (currently line 33) or use the URL trick mentioned near the bottom of this post.
  4. Call the profiler_printResults() function where you want to see the results. Personally, I just put this in the footer section of my page and leave it there all the time. If the profiler isn’t enabled it won’t output anything.
  5. Run your code & look at the results!

Bonus-pretzel-for-you: You can profile on your live site on-demand! If you leave the default RUN_PROFILER set to false (currently line 33), the profiler also allows you to add &RUN_PROFILER=true to the URL of any page to turn profiling on for just that run. I use this constantly now.

If you’re as excited as I am (I’m pretty excited): head over and grab the code!

One Response to “Open Source Goodie: Simple PHP profiler”

  1. […] a whole barrel of fun if you’re into having blazing-fast code: check out my post on the doItLater […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: