Tool to profile php code - php

Looking for some [freeware/opensource] tool in order to make it easy to profile a big php project on win32 platform. Need to find out which part of code is most time consuming.
It's hard to manually put timing function for each function, loop...

You'll want to install and configure Xdebug. It's sort of the de-facto standard PHP debugging and profiling tool.
WinCacheGrind can crunch the profiling output. It's a bit buggy, but it does the job.

xdebug works quite well http://xdebug.org
Also wincachegrind is a good tool for looking through the profiler's output. http://sourceforge.net/projects/wincachegrind/
As well as (if the profile file is small) webgrind http://code.google.com/p/webgrind/

In addition to the excellent xdebug (as mentioned by others), you can also look at xhprof.

Yes, use XDebug, and once you're in it, use this technique, which works on any platform.
Don't think of it as measuring time.
Think of it as trying to ask, predominantly, What is it doing, and Why is it doing it?

Related

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

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.

Remote Debugging and Memory Profiling in PHP

Two Questions
How can I debug PHP code remotely which is lying on the server, currently I am using var_dump's to check the value of variables ?
How can I see how much memory and how much time particular function is taking to execute remotely which is lying on the server, any suggestion for good profiler for this ?
Note: I am talking about code which is lying remotely on the Server.
Like others said, Xdebug is your best bet. I'll just chime in here and mention a couple profilers. If you're on Linux you can't go wrong with KCacheGrind and on Windows it appears WinCacheGrind is what you'd use. I was not aware of remote profiling being an option, it dumps to a file which you'd need to have access to. Although, it appears you are accustomed to that already.
Komodo is a pretty nice IDE with Xdebug support (debug requires paid version), although there is no reason to not just install Eclipse and pick up the Zend PDT.
My suggestion, program in Python and be a happier developer (more developer-friendly modules/tools) :P
I think Xdebug is what you're looking for. If you don't mind spending the money, Zend IDE for Eclipse comes with Zend Debugger and it works quite well also.
As bender said: XHProf [1]: http://pecl.php.net/package/xhprof [2]: http://mirror.facebook.net/facebook/xhprof/doc.html is much lighter then XDebug and I prefer it for taking profiling info about the live site. Facebook uses it for profiling their live servers. It has nice features like aggregating the profiles and making diffs between them so you can see the regression in your code as well. If you need step by step debugger then XDebug is your call. Just be aware that XDebug on live site turned on by default imposes important degradation of performance, and output cachegrind files that site generate could fill the disk very fast.
I suggest not using XDebug on live site and if needed configuring it to activate on demand through trigger as described in docs http://xdebug.org/docs/
Configure your server to allow XDebug remote debugging and pick a client, although it can be a bit involved to set it up. I've personally used Geben for emacs as a front end for XDebug, but there are many choices.
As far as you're allowed to install stuff on the server Xdebug is the way to go for both debugging and profiling.
If you are on a shared server forget about it (unless you are a good friend of the server admin). Your best chance seems to be FirePHP - for debugging only. You may want to take a look at dBug too; it's basically a prettied up var_dump, but your eyes will be grateful for it.

How can i benchmark my SQL and PHP code?

I have problems because i don't really know how I can test my code and sql ( mysql ) to see how long SQL queries and PHP functions /code take to run.
Does anyone here know where I can find these tools?
For PHP code, you can use xdebug for profiling. You'll need a frontend to read the trace data as well. If you're on windows, you can use WinCacheGrind. For other OS'es you can use either kCacheGrind (Lots of features, but a bit scary for beginners) or WebGrind (Much simpler and nicer looking).
For Mysql, you can use maatkit for profiling multiple queries, and to analyse a specific query, you should use EXPLAIN.
This is called profiling. XDebug, APD and probably some other packages provide this functionality.
The search keyword you want is "profiler".
There's several PHP profilers out there; XDebug, PHPEd Profiler, APD, DBG, and PQP, just to name a few.
I've used XDebug on several occasions and though it can be a little annoying to get up and running it does a pretty decent job. Just follow the installations docs and you should be fine.

Determining the performance consequences of PHP code

How can you determine the performance consequences of your PHP code if you are not familiar with the internals? Are there ways to figure out how your code is being executed (besides simply load testing it)? I am looking for things like memory usage, the execution time for algorithms.
Perhaps Joel would say, "learn C, then read the internals", but I really don't have time to learn C right now (though I'd love to, actually).
Use the Xdebug extension to profile PHP code.
If you're not familiar with valgrind or similar, then to add to #Jordi Bunster's answer...
When you've had profiling on in Xdebug, you can open the dumped profile files in KCacheGrind or WinCacheGrind to get a graphical view of what is taking the time in your code.
Fortunately the xdebug documentation also explains this in detail as well as how to interpret the results: http://xdebug.org/docs/profiler
Even if you are familiar with the internals, you should still load test your assumptions. I like to use the PEAR Benchmark package to compare different code.
If you can isolate your code, you can keep your load testing simple. A typical technique is to run each option some number of times and see which one is faster. For example, if you have a class, you can write a test case and that puts it through it's paces and run it several times.
You can use low-level approach such as sticking microtime() and memory_get_usage() calls into the code or you can use one of existing profiling solutions:
Xdebug (free, opensource)
Zend Studio/Debugger profiling (commercial)
Zend Server Code Tracing (commercial)
xhprof (free, opensource)
As usual, commercial tools have nice GUIs and pretty pictures, but cost money, free ones are free, but you'd probably have to invest a bit more time.
Also, PHP CGI binary has a benchmark mode with -T option, you many try running php-cgi -T 100 yourscript.php to do a poor man's benchmark.
See SD PHP Profiler for a tool that can show you graphically where your PHP applications spends its time.

Categories