PHP Performance Profiling With APD On Windows

When writing code, its important to keep system performance in mind. No matter how useful your software is, if it places too much stress on the server, it won’t be used. How can you tell if your code is efficient? Well, experience helps, but it also pays to have good tools.

Most of the code I write is fairly simple and should only place minimal load on the server, but I wanted to make sure that its as efficient as possible. I went looking for a PHP performance profiling tool to help me.

My search led me to Jonathan Oxer’s PHP Performance Profiling article. This mentions several tools, but focuses on Advanced PHP Debugger (APD). This looked good, so I tried to give it go. I eventually got it working, but it wasn’t easy – the problem was that I wanted to run it on Windows XP.

The focus of this post is how to get APD running on Windows. If you don’t write PHP code or use Windows, then this article is probably not for you!

What Is Performance Profiling?

Before I get into it, I’m aware that some of you will be asking “What’s performance profiling?” Here’s Jonathon’s description, which says it perfectly:

Performance profiling runs your code in a controlled environment and returns a report listing such statistics as time spent within each function, how long each database query takes and how much memory has been used.

By doing performance profiling on your code, you quickly can see where you may be wasting time with slow database queries or inefficient code. Having this information then allows you to spend your time tuning PHP and SQL where it needs it most. No more guessing what’s going on internally: performance profiling gives you hard figures.

APD is a tool which helps you to do this.

The Problem With Installing APD on Windows

To run APD in the Windows environment, you need the php_apd.dll file. Unfortunately, the installation package (from the APD site) doesn’t include this file. The readme notes indicate that you need to compile APD yourself.

Well, I don’t have a C++ compiler installed and I wasn’t interested in downloading one and messing around with setting it up to compile PHP. That would take more time than I was willing to spend.

Surely the compiled file must be available somewhere on the Internet? Well, yes, but information on where to get it is buried amongst an avalanche of pages with no information (people asking “where do I get“, but no answer) or pages with misleading advice, leading to dlls that don’t work.

So Where DO I Get The Php_apd.dll File?

The php_apd.dll file is contained in the PECL extension file.

If you’re using the latest version of PHP, go to the Windows Binaries section of the PHP Downloads page and download the PECL 5.x.x Win32 binaries file (where 5.x.x is the latest version). The php_apd.dll file is in this zip file.

If you’re using an older version of PHP5, go to the Unsupported Historical Releases page, locate the version of PHP you are using and download Collection of PECL modules for PHP 5.x.x file (where 5.x.x is the version you’re using). The php_apd.dll file is in this zip file.

If you’re using PHP4, I’m afraid I can’t tell you where to find the file. There is no separate PECL download for PHP4 versions, because most PECL modules were included with PHP4 (rather than in a separate file as with PHP5). Unfortunately, it seems the APD extension wasn’t one of those included.

Setting APD Up

Once you’ve got the php_apd.dll file, here’s how to set APD up:

First, make sure you have the PEAR extension installed, as APD uses its Console\Getopt.php script. If it is installed, there will be files and folders in the C:\php\pear folder (assuming PHP is installed in C:\php). If the folder is empty (there may be one file) or doesn’t exist, you need to install PEAR.

Installing PEAR should be as simple as running go-pear.bat in the C:\php folder. This asks a few questions, connects to the PEAR site and downloads the appropriate files. If PEAR support is not included in your version of PHP, you will have to download it from the PEAR website and install it yourself.

Second, copy the php_apd.dll file to the C:\php\ext folder – assuming this is where your extensions live. If you are unsure where they live, locate your php.ini file and check the extension_dir setting. This is the folder you need to copy php_apd.dll to.

Third, edit php.ini and add the following to the Dynamic extensions section:

extension=php_apd.dll

I’ve seen it reported that you need to add:

zend_extension = c:\php\php_apd.dll
apd.dumpdir = .
apd.statement_trace = 1

but I didn’t need to, probably because I’m not using the Zend extension.

To check if APD is loaded, run the phpinfo() command. If you’re not sure how to do this, read the following tutorial on creating a page that displays phpinfo(). If you find an APD section somewhere on the page, it’s loaded.

Using APD

For more information on using APD, see Jonathon’s article, but here’s what I did:

1. I added the following to the top of the script I wanted to profile:

apd_set_pprof_trace('C:\temp');

    where C:\Temp is the path I want the output file to be created in.

2. I ran the script once to create the output file.

3. I opened the command prompt, changed to the directory with the
    output file and typed the following:

c:\php\php.exe pprofp -u <filename> >output.txt

    (you need to replace <filename> with the name of the output file).

If you get an error saying there is no pprofp file, try downloading a copy of mine and put it into the folder containing the output file. Note: It should have no extension.

There you go – PHP performance profiling… The information is in output.txt.

APD Not For Profiling WordPress Plugins

I was hoping to use APD to profile my WordPress plugins. Unfortunately, its not suitable for this, because plugins hook into the WordPress core – so APD picks up everything else that’s going on as well. In hindsight, I should have realised this before I tried!

APD could be used to profile the performance of WordPress as a whole. People are becoming more aware of WordPress performance and are tweaking it to improve performance. Using APD while making changes could provide valuable insight into the effectiveness of the changes.

It’s also worth mentioning Lucia’s PluginHogDetector plugin for WordPress. This works by reporting how long it takes to process various parts of the page. To test a plugin (or change), simply compare the times before and after the plugin is activated (or the change is made). This isn’t as exact as performance profiling, lacking a breakdown of functions, how many times they’re called, how much memory they use, etc, but its simple and useful.

The Final Word

Evaluating the performance of your code is very important. I have a few projects on the go where APD will help me do that, so it was worth the struggle to get it working.

If you have any other tips for measuring PHP performance let me know!

26 responses on “PHP Performance Profiling With APD On Windows

  1. Stephen Cronin Post author

    Hi Terence,

    I’ve heard very good things about Zend Studio, but I’ve never tried it. I do have Dreamweaver, but don’t use it often. Most of the time I use a simple text editor called SciTE. This is for two reasons:

    1. I prefer doing things by hand
    2. Most of my projects are fairly small / simple

    If I were writing large scale applications, on an ongoing basis, then I’d definitely invest in a comprehensive IDE with all the bells and whistles (and I wouldn’t need APD).

  2. Terence Chang

    Stephen:

    Yeah. I am a hard-coder. I don’t like dreamweaver. I hand code everything. I have been using Zend Studio for years. I love it. especially it give you a way to find the definition of class your code is calling.

    You can also try the Eclipse for PHP.

    http://www.eclipse.org/pdt/

  3. Stephen Cronin Post author

    Terence,

    Dreamweaver’s not so great with PHP is it?

    Thanks for the link to Eclipse for PHP. I tried out Eclipse when I was flirting with Java a few years back, but that was before there was a PHP framework. I’ll have a look at it – it might be the halfway house between a plain text editor and Zend Studio (which isn’t free).

  4. K-IntheHouse

    Stephen,

    I use HTML-Kit in Windows XP and I love. It is very versatile and compares with a lot of professional IDEs. It supports remote folders via ftp and has a great number of plugins like Firefox or WordPress and you know why I like it now. :-)

    I have linked to a review I did on HTML-Kit a while back, just in case.

  5. Stephen Cronin Post author

    K,

    Thanks for the tip (yet again!). I’ve had a quick look at their site and I’m downloading it – It looks impressive and I’ll give it a whirl. I’ve also got Notepad++ installed and that also seems more like an IDE than a simple text editor.

    However, although these are IDE’s, I’m pretty sure they don’t offer performance profiling. That sort of thing is only included in the top end IDEs – but then it’s really only necessary if you’re writing complex applications. I don’t need it for most of what I do and I can use APD when I do need it.

    Anyway, thanks for the tip – I’ll let you know how it works out.

  6. K-IntheHouse

    Stephen.. you are very welcome. It’s the least I could to add to the conversation you started. You are right about these editors. They are just that and don’t offer the advanced stuff you get with professional IDE’s.

    I use Notepad++ (Portable version) for most tweaking and use HTML-Kit for from-the-scratch projects.

    I am still learning the ropes with PHP and I am having more fun with building WordPress blogs for people from the scratch and customizing it. :-)

  7. Stephen Cronin Post author

    K,

    I made my previous comment just before going to bed and then I lay awake thinking about it! What I haven’t said, anywhere, is that performance profiling is pretty damn difficult for PHP (so it’s no wonder that HTML-Kit, etc don’t include it).

    Because PHP code gets interpreted by the PHP server (rather than compiled), to measure performance, the profiler has to hook into the PHP server itself (ie get inside PHP so it can watch what’s going on). It’s beyond the scope of most IDE’s to do this. It’s worth noting that Dreamweaver doesn’t have PHP performance profiling either.

    Anyway, I’ve had a quick look at HTML-Kit now and I will have more of a look at it (and Notepad++ too). It’s seems really useful – although there are too many plugins to sort through! :)

    I’m glad you’re having fun – personally I enjoy playing with WordPress more than I like writing posts! Be careful! :)

  8. Jeff_

    Hi Stephen,

    Sweet article! I definitely need to sharpen my focus on performance evaluation and code optimization. PHP is my preferred programming language, although I have yet to accomplish any significant works of brilliance. — But when I do.. I will be referring to this fine article ;)

    Thanks for writing it, and also thanks for the link!

  9. Stephen Cronin Post author

    Hi Jeff,

    Thanks! Sorry I’m a bit slow responding – I’ve been offline quite a bit lately (Happy New Year!).

    Re the link to your article: I’d been thinking about doing something to measure performance for a long time, but it was your article that made me actually go and find a way to do it. So thanks!

    To be honest, I don’t really need the full performance profiling that APD offers, because what I’m doing is pretty straightforward, but I like to make sure my code’s not too inefficient…

  10. Jeff_

    Yes, APD does seem like some powerful stuff — probably more than I would need at this point in time, however, excellent tip about Lucia’s PluginHogDetector.. I will definitely have to give that a spin for my next plugin, which I hope to release here within the next couple of weeks.

    In any case, great to see you back online — Happy New Year!

  11. Stephen Cronin Post author

    Hi Jeff,

    Happy New Year to you too – although you spoke too soon! I’ve been pretty much offline again for a week… I think it’s all back to normal now.

    A new plugin? I look forward to checking it out.

  12. Jeff_ @ Perishable Press

    Well it certainly is good to see you back (again)! :)

    In the meantime (in case you weren’t busy enough), check out that new plugin I was telling you about! Released fresh just yesterday!

  13. Stephen Cronin Post author

    Jeff,

    Thanks. The new plugin sounds good. I’ve checked out the screenshots and it seems a thing of beauty! My current contact form plugin uses tables (yuk)!

    I won’t promise to try it right now, as I am so far behind in so many things, but I’ll put it on my list of things to do. Congratulations!

  14. Kailash Badu

    Stephen,

    I got APD installed on my local Windows machine. But I just don’t seem to be able to run pprofp from the command line (it says it doesn’t identify the filename pprofp). Can you please explain how you got pprofp working in your case?

    Thanks,

  15. Stephen Cronin Post author

    Hi Kailash,

    For me it worked when I followed the instructions above, so it’s hard to tell what’s gone wrong for you. Are you typing:

    c:phpphp.exe pprofp

    from the command line? Pprofp isn’t a separate .exe file in Windows – it’s actually built into php.exe (I guess through php_apd.dll).

    If you are typing the php.exe part, then it would appear that PHP isn’t aware of pprofp. That probably means that the APD dll isn’t loaded properly. Do you get an APD section when you run the phpinfo() command?

  16. Kailash Badu

    Hi Stephen,

    Well I am sure I have installed APD properly. it does show up as a module in the phpinfo() page and I am able to generate the trace file with the function apd_set_pprof_trace(); .
    However, since the generate trace file is not user-friendly (its supposed to be machine readable) I need to parse it into a useful format. I can’t use kCaceGrind because I am Windows. So perhaps I am going to have to use pprofp shell script . But I can’t for life of me find where exactly is the is script? when I try to run it from command line like you said
    c:\php\php.exe pprofp it says something along the line of pprofp not being identified. Do you want me to post the exact message?

    Thanks for taking care of this.

  17. Stephen Cronin Post author

    Hi Kailash,

    I’ve replicated the problem now.

    I thought pprofp must have been compiled into the dll somehow, but I was wrong. I seem to have a pprofp file (no extension) which contains the script. This file is in the same directory as the output files. If I remove the file I get the following message:

    Could not open input file: pprofp

    I’m not sure how the file got there and I’m out of time to investigate now. I will look into in the next day or two and if I can’t find the answer, I’ll post a copy of the file here (though it may not work with your version).

  18. Kailash Badu

    Yeah stephen, I would appreciate if you could find how to get a pprofp script in the first place. If that still turns out to be a mystery, you might as well get me a copy of the script from your machine.

    Thanks for taking care of this.

  19. Stephen Cronin Post author

    Kailash,

    I’m out of time for the moment, so here is a copy of mine:

    pprofp

    Copy it into the directory where the output file is, then the commands above should work. Note, there is no extension for this file.

  20. ak@chikka

    I also encountered the problem saying:

    Could not open input file: pprofp

    I understand I have to copy your pprofp file in the folder where the output is located. I did that but I the message still keeps coming out saying could not open input file.

    Maybe you could help me with this? I think I committed a mistake in saving your pprofp file (I’m not sure if it had an extension and I don’t know how to save a file without file extension).

    Many Thanks in advance!

  21. Victor Guerra

    I solved it and the command generate the file.

    First I copyed the pprofp.php script and the file generated by APD to the Folder:

    C:phpPEARConsole

    Then I changed to that folder.

    CD phpPEARConsole

    Then I ran the command in the next

    c:phpphp.exe pprofp.php -u >output.txt

    You need to add .php to the script name.

    And It worked!!!

    Good Luck!!!

  22. MegaS

    Recently I’ve tried to use apd for profiling, but i wasn’t able to run it properly on linux (fedora core 8 – i know there’s a good .deb package for php+apd).
    Just want to help if anybody got the same problem – use Xdebug – I guess it is much more stable

  23. jeff@High yield savings account

    Great post. Most people never worry about inefficient code. You see some sites that take so long to load i imagine alot of people leave the site before it is done loading.

  24. Andreas@Webdesign Preise

    Hey dude,

    nice post. May i should really all my “dirty” codes so that I can gain perfomance without limits – I am just joking ;)

    Regards, Andreas

  25. shahu

    hi,
    Thanks to alll for givin d info.
    I had managed to create the trace file as well as o/p file .
    I Had used the command :
    c:xamppphp php.exe pprofp -u pprof.04016.8 >apd.txt

    where pprof.04016.8 is trace file & apd.txt is o/p file.
    But my o/p file doesnt contains anything. IT IS BLANK.
    What should i do? where I am goin wrong?

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Anti-Spam Quiz: