What is the best way for me to test my PHP code's performance?
-
Please specify if you want to test a block of PHP code, a block of code that uses the database or the overall performance of your site under load. All three use cases have their own tools, which is reflected in the answers here. – chiborg May 29 '12 at 08:56
-
Here's [a great answer](http://stackoverflow.com/a/1202746/4233593) – Jeff Puckett Jul 21 '16 at 22:12
7 Answers
You should consider using ab (apache benchmark tool) to run a large number of queries and xhprof to profile/analyse your code. In my opinion, these just are the basics but give great results.

- 2,587
- 16
- 21
You can use microtime() to messure the execution time of your code. Here is a basic code snippet for that:
$start_timestamp = microtime(true);
...
---your Code---
...
$end_timestamp = microtime(true);
$duration = $end_timestamp - $start_timestamp;
error_log("Execution took ".$duration." milliseconds.");

- 50,002
- 13
- 138
- 166
-
2time() returns seconds, not milliseconds. I believe you're looking for something like [microtime()](http://ca3.php.net/manual/en/function.microtime.php) – Rémi Breton May 31 '13 at 20:19
-
3I always get negative numbers doing this. Is my program faster than time itself? – Ray Sep 10 '13 at 13:20
-
@Raymond: you might have proven the possibility of particles moving faster than light :) – Thariama Sep 11 '13 at 11:28
-
1In order to make this work correctly (and not get a negative number like @Ray) is [calling it with the parameter `true`](http://php.net/manual/en/function.microtime.php#refsect1-function.microtime-parameters), i.e. `microtime(true)`, so it returns a float instead of a string. – Sebastian Zartner Aug 12 '15 at 12:16
xDEBUG (see Neil Aitken's answer) is useful for identifying poor performance issues in PHP code - but it can only be used under very controlled and restrictive conditions - not least its difficult to see what effect concurency has on the performance.
While as Patrick MARIE suggests, you could use ab - its not a viable approach if the transaction you are trying to measure spans more than page (e.g. log in to application and create session, add a random product to basket, repeat add random product N times...).
AFAIK there's no PHP based solution for recording/scripting interactions - but there is Perl + WWW:Mechanize + HTTP:recorder. Or if you're extremely rich you could buy HPs loadrunner product.
But its very difficult to implement testing which is truly representative of how the application is used - and the performance of the application (at least the data related parts) will vary over time - so you need to build proper performance metrics into your code.
...and even then, the time taken for the PHP to generate an HTML page is only a very small part of the story of how long it takes for a page to render on the browser.
HTH
C.

- 47,736
- 6
- 59
- 94
-
1Very nicely put, it's difficult to measure how your app may perform under heavy load. You can use Selenium instead of Mechanize to automate browsers, but as far as I know it's not designed for load testing. – Neil Aitken May 27 '10 at 17:43
-
xDebug has a profiler built in, once configured it will dump some files that you can read with a program like kCacheGrind or WinCacheGrind
This will then allow you to see the all the function calls, average and cumulative call times and the total script execution time. Very helpful for finding bottlenecks in your code.

- 7,856
- 3
- 41
- 40
As a new look at an old issue, consider using a commercial tool such as NewRelic to assist in profiling. I personally just use them to get a small sample of data for either free or a nominal price. I do take full advantage of their fully featured product trial though.

- 772
- 7
- 13
Also You could use APD (Advanced PHP Debugger).
It's quite easy to make it work.
There is a nice tutorial how to compile APD and make profiling with it : http://martinsikora.com/compiling-apd-for-php-54

- 9,844
- 6
- 58
- 68