Slow PHP script - automatic debug and diagnosis? - php

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! )

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.

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

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.

Related

What can delay "Time to First byte" by almost 2s?

I have enabled a PHP profiler, well magento, but it is still a profiler.
This is the standard magento compiler that records all the processing including all db queries to create the page, from receiving the request.
I am testing with a the php built-in server hosted locally.
The results show pretty decent server response times, but on the chrome developer tools the Time to first byte is much higher. Why is this?
Take Note...the screenshots below of the timings are from the SAME REQUEST...CLEARLY
Profiler (www.mysite.com/index.php):
Developer tools (www.mysite.com/index.php):
Edit
I had written a bit bigger answer but then I realized that you are using the built-in PHP server. Honestly, you should give a real webserver a go. You might be chasing a problem that does not exist (although one can argue that there is something going on because it's not a normal situation - you might be encountering a PHP bug) ;)
Found the issue.
The profiler is not recording included libraries. In the magento/lib folder nothing is recorded even if you explicitly set it to profile.
It seems that we need to use an all-encompassing php profiler

Php profiling - need some advice

I'm pretty new to php and absolutely new to profiling, so please forgive me if my question is a little bit "simple".
So enabled xdebug and downloaded Kcachegrid to begin profiling, read through what I could find. My problem is that I'd like to improve speed, but whenever I view my xdebug reports in Kcachegrid, the same script with the same hardware runs with a merely different execution time (form 1.1 to 1.9 sec). I'm executing it on localhost so that I do not have to deal with network speed.
i've read this pretty good article from John Lim http://phplens.com/lens/php-book/optimizing-debugging-php.php and in his article he's dealing with fix execution time. I don't know what I am doing wrong or how could i get fix times so I can see if I get actually better results.
Any advice would be appriciated, or any resource you have read and found useful on php profiling. Thanks for replies!
xdebug is good but I found XHprof an extension written by Facebook for its production profiling works a lot better. It shows you the Wall times of all functions and the stack of the call and can drill down to many levels and see the wall time on each call. You can also compare and consolidate similar calls and compare them over a period of time. Have a look at the demo on XHprof and its a very simple extension to implement. Its a matter of enabling a header and footer and you are off to the races.
http://xhprof.io/
Cheers,
Thusjanthan

measuring php performance

I'm trying to track down issues with an application [modx] I have several of these sites [about 10] on my server & was wondering how I can see what php is doing.
Pages on these sites are extremely slow while the same sites in dev are fine as are other php applications on the server.
I tried using xdebug to get an idea of what php was doing while processing these pages & where the bottleneck was occurring, but it only appeared to want to do anything on an error [there are no errors being thrown]
Any suggestions on how to track this down?
[linux/Centos5/php5.2.?/apache2]
Xdebug and webgrind are a nice way to see where your bottel necks are...
Read XDEBUG_PROFILE and Webgrind
Set up the php.ini to have xdebug profile your code on every run or if a special param is passed, then setup webgrind to read from the same directory xdebug writes its profile dumps to.
Webgrind will show you what functions and set of functions require the most time, it breaks it down and makes it easy to find slow and/or inefficient code. (eg. your script is calling "PDOStatement->execute" 300 times on a fast query [Or calling it once and a massively slow one] taking up 90% of the execution time).
The most commonly used tool, for finding bottlenecks in PHP, would be Xdebug. But you should also manually examine the codebase.
There are three different areas where you will have to focus on:
frontend performance
SQL queries
php logic itself
.. and the impact on the perceived speed is in this order.
You should start by running ySlow, and make sure that your site follows the guidelines as closely as possible.
The next step would be tracking down what SQL queries are executed, and (assuming you are using mysql) try to run them with EXPLAIN. Also, check the queries themselves. There might be some extremely stupid code there, like ORDER BY RAND() or use of LIKE in huge tables.
And the last stage would fixing it all would a hard looks at the code itself. Both on PHP and JavaScript side of things.
Also , you should upgrade to PHP 5.3, because your version is extremely outdated.
Usually when you don't know what you're looking for, you cannot spot it with tools like xdebug or other plugins/debug bars etc built into CMS/Framework, new relic is the simplest solution - you'll be able to spot bottlenecks after few min.
while new relic is a paid app, you can test if for free for first 14 days - it's more than enough to find problem.
It's great because it integrates all other tool's and data sources you usually use:
xdebug, cpu & i/o monitoring, mysql slowlog, queries log.
It will also show you if your app is slow on php/DB/frontend/network.
You should try it out instead of wasting time for debugging with other tools.
here is a guide for centos installation: https://newrelic.com/docs/php/php-agent-installation-redhat-and-centos

Step by step guide for benchmarking PHP project

Can anyone guide me how to load test/benchmark a project written in plain procedural PHP (no framework) and MySQL to identify the bottleneck ?
The project uses SESSION to store some values.
I've the last version of WAMP !
[On SO i found JMeter to do the job, but there was no step by step guide, neither i found it on the JMeter's site. Looking for help from you.]
If you want to profile your code to find out, which part of it takes all the time, you're looking for a profiler.
With WAMP, I'd recommend the xdebug profiler with webgrind to visualilze the data.
On the other hand, if you want to know, how much your site can take, you should take a look at Apachebench, which should come with WAMP.
You didn't specify, how complex your benchmarking needs are, but apachebench should make it possible.
Use PHPUnit for testing:
http://jsdoodnauth.wordpress.com/2008/11/05/installing-wamp-and-phpunit-on-windows/
Unit Test based development.
This allows you to produce bug free code. It also benchmarks for time.
And PHPUnit definitely is the way to go

function to profile / performance test PHP functions?

I'm not experiencing any performance issues, however I'd like to take a look at what takes how long and how much memory cpu it uses etc.
I'd like to get a firsthand understanding of which things can be bottle necks etc and improve any code i might reuse or build upon... (perfectionist)
I'm looking to create a little function that i can call at the begining and end of each function that records:
execution time
memory used
cpu demand
any ideas?
i haven't used things like memory_get_usage(), or methods of recording time() before so would love to get some tips on their combined implementation
There are already a host of solutions made just for that, you might want to have a look at some of these:
XDEBUG EXTENSION FOR PHP
Xdebug's Profiler is a powerful tool
that gives you the ability to analyze
your PHP code and determine
bottlenecks or generally see which
parts of your code are slow and could
use a speed boost.
Other Resource:
PHP Quick Profiler
I haven't tested it a lot, but friend of mine recomended http://xdebug.org/ for profiling PHP
Try using XDebug to debug your code flow. XDebug will generate some file that tell how well your codes, you can use Kcachegrind to visualize that files.

Categories