Reputation:
What is the best way for me to test my PHP code's performance?
Upvotes: 15
Views: 13012
Reputation: 50832
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.");
Upvotes: 7
Reputation: 9974
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
Upvotes: 0
Reputation: 862
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.
Upvotes: 1
Reputation: 48357
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.
Upvotes: 7
Reputation: 28074
If you want to test a specific part of the code, consider using the Benchmark package from the PHP PEAR library.
$timer = new Benchmark_Timer();
$timer->start();
// Code to test here
$timer->stop();
$timer->display();
Upvotes: 2
Reputation: 7854
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.
Upvotes: 4
Reputation: 2405
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.
Upvotes: 12