How can i benchmark my SQL and PHP code? - php

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.

Related

database and php debugger

I was wondering whether there is a tool available that one can use to debug their database and php applications. Equivalent of firebug or chrome developer extension for databases and php. (I would like to know whether some queries are executed or not exected)
Use xdebug or a similar extension to debug PHP, in addition to the traditional var_dump and similar techniques.
Turn on query logging to debug what your database does or doesn't do.
To debug individual query execution, have the database EXPLAIN the queries to you.
PhpEd is awesome! Can hardly get out of bed without it these days, lol

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.

Tool to profile php code

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?

What tool can I use to generate a PHP class usage report for my application?

I have a fairly large object-oriented php 5 project, and as part of a change impact analysis, I'd like to compile a report on the usage of each existing class throughout the project.
It would help me immensely if I could find an existing tool that will analyze all the files in my project and generate some sort of report that lists, for example, all the class names of objects instantiated for each class in the project, and allow me to at least search this easily and quickly.
Any help here would be appreciated!
Check out nWire for PHP. It analyzes your code and recognizes such associations. It is built as an interactive tool, not as a reporting tool, but, if you insist, you can still connect to its' database (it uses H2, which is SQL compatible) and use an external reporting tool.
IMO Zend has some profiling tools that do just that, Or you can extrapolate this information from their Accelerator log.
Or try this with XDEBUG
Xdebug can trace your code and create code coverage statistics. There are additional tools like Spike PHPCoverage, which can generate nicely formatted reports, but since these are intended for test-coverage, it'll just give you a boolean result (eg. line of code is used or not used). You probably want a more detailed view (eg. how many times is it used).
Another option is to use the function trace feature of Xdebug. This will give you a detailed report of the actual call graph. You can determine which files was used the most from this. You'll need to write a parser for the data manually, but that shouldn't be too hard.
Finally, you could do the same thing with a static call graph. There are some tools available for php. Here are a few:
http://www.doxygen.nl/
http://phpcallgraph.sourceforge.net/
http://www.bytekit.org/
Again, you probably need to do some additional manual parsing on the output from those tools, to get something that applies to your use case.
The clever guys at Particletree, the same people behind the functionally and aesthetically gorgeous Wufoo often publish and release their PHP toolsets and utilities, the most recent of which being their PHP Quick Profiler. As you can probably tell, I have a huge amount of respect for those guys and love the stuff that they do.
A good PHP profiler is often hard come by, and PQP is most certainly the best I've come across. That said, nearly all of the various application frameworks have some form of profiling system, humble or otherwise, but none as nearly as in-depth and helpful as PQP. However, I usually find that the framework profiling tools are more linked into the code automatically, and if you use the framework's standard libraries then you'll have to do a lot less implementation with the profiling tool (this is definitely the case with CodeIgniter). But if you want that extra bit of power and flexiblity, PQP is great.
Let me know if you find anything better - I'd love to see it!
Jamie

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