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.
Related
Can any one here provide me a good tutorial on compiling PHP using HipHop? i saw this link . But i cannot understand the linux commands. I developed the application in windows. I was not able to run those commands in linux
$HPHP_HOME/src/hphp/hphp test.php
This command gave me error bash command not found.
in the source i got, i do not have anything named hphp inside the folder hphp, is that a method inside main.cpp???
Can any one here provide me a good tutorial on compiling PHP using HipHop? [...] i cannot understand the linux commands.
HipHop and the companion/successor HHVM, are for advanced users that are trying to resolve specific performance edge cases. Normal PHP developers will not ever benefit from any of the things that HipHop provides. In fact, in many cases, you can get just as much of a performance benefit through simple performance profiling and targeted optimizations combined with a bytecode cache, like APC.
If you do not understand the commands needed to get it running, you do not fall into the class of user that should attempt to use it. Break out your profiling tools instead, as they will be far, far more valuable to you.
It's been clarified that the actual intent is to use HipHop as a PHP code protection mechanism. That's an even worse idea!
HipHop is designed to run as an application server. That is, it contains a web server.
HipHop binaries are huge, often greater than 500 MB. They are not appropriate for redistribution.
HipHop memory use is significantly larger than the binary.
HipHop is designed and intended to run on machines dedicated to the task.
It's not intended for that purpose... and everything can be decompiled. That includes the commercial products that are designed for the task, ionCube and Zend Guard. All it takes is a bit of time or money, and your source is mine. Your best defense against intellectual property theft is a good license and a good lawyer, not a technological solution that makes it harder to run your code.
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
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.
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.
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?