Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slow PHP script - automatic debug and diagnosis?

How can I find out whether a PHP script goes bad and runs really slow when ran by hundreds of users every second, and better yet, is there any tool that could tell me approximately which part of the code slows me down? ...

I don't wish to post the code here (mainly because this question refers to something else and because it's a waste of space) and preferably never post it anywhere because it's actually a mess!... a mess that I understand and yes, i coded it, but still a mess which would insult anyone trying to comprehend it... so if you have any creative ideas, please let me know!

Cheers!

( thank you already for your incoming answers! )

like image 841
Andrei Chirtes Avatar asked Jun 25 '11 11:06

Andrei Chirtes


People also ask

How do you debug a PHP script?

Launch the Debugger Before launching the debugger, make sure that either a breakpoint is set or the Break at first line in PHP scripts option is enabled on the Debug page of the Settings/Preferences dialog Ctrl+Alt+S . on the PhpStorm toolbar. Press Alt+Shift+F9 . Select Run | Debug from the main menu.

Can you step through PHP code?

The debugger will evaluate variables, you can set watches on variables, and you have access to the call stack. Once the debugger is paused on a breakpoint (or by manually hitting the pause button/pressing F6) you're ready to step through the code.


3 Answers

Enable XDebug profiling, and send the resulting files through WinCacheGrind (Windows) or KCacheGrind (Linux).

This will allow you to see a breakdown of which functions get called most, and where the time is spent. Learning to use XDebug is a must for any serious PHP developer.

Here is a seemingly good tutorial on getting started with XDebug profiling.

like image 167
Vegard Larsen Avatar answered Sep 21 '22 16:09

Vegard Larsen


You will need two tools

  • a profiler (Google it)

i use this one at work :

http://www.nusphere.com/products/php_profiler.htm (commercial)

  • a load tester

check this site for more info :

http://performance-testing.org/content/performance-testing-tools

like image 3
Tarek Avatar answered Sep 22 '22 16:09

Tarek


I'd recommend to use a PHP profiler. Xdebug which is both PHP debugger and profiler can help a lot. There are also other debuggers, e.g. Zend Debugger.

To analyze profiling results you could also need a special tool. I used WinCacheGrind in Windows and KCachegrind in Linux.

Profiling report shows tons of useful information e.g. which lines of the source code were called how many times and which functions took the most of the execution time.

like image 2
Vadim Belyaev Avatar answered Sep 22 '22 16:09

Vadim Belyaev