Forum Moderators: coopster
How do I do this with php?
One obvious way to do it is to put in
echo microtime(); here and there in the code. But this is very messy and takes a lot of time to put into and out of the code. Any smart tools out there?
<?
// benchmark timing
function getmicrotime($t) {
list($usec, $sec) = explode(" ",$t);
return ((float)$usec + (float)$sec);
}
$start = microtime();
// end start benchmark
// your code here
// benchmark timing
$end = microtime();
$t2 = (getmicrotime($end) - getmicrotime($start));
// end benchmark timing
echo "<p>Total Time: <b>$t2</b>";
?>
As far as pages for random users go it should be lightning fast.
Internal company site functions, make them wait if you need to but don't be too lazy. ;)
Functions for you, just try not to pass the max execution time for the script.
Logged in members doing a specific function, it can be a little longer than you would normally allow though I try not to go beyond a couple of seconds.
It really depends on what you are doing and who you are doing it for.
<added>That isn't my code that was andreasfriedrich's I believe
The code mentioned and displayed by jatar_k/andreasfriedrich is quite similar to that in the PHP microtime() [us4.php.net] manual pages. On that page, you will now find that beginning with PHP 5, it gets even more streamlined!
When the optional argument
get_as_float is given, and evaluates to TRUE, microtime() will return a float. The get_as_float parameter was added as of PHP 5.0.0. Meaning we no longer have to script a getmicrotime user function in just to return float values. It will still return a string if you don't pass boolean TRUE to it.
$time_start = microtime(1);
// end start benchmark
//
// your code here
//
// benchmark timing
$time_end = microtime(1);
$time_elapsed = $time_end - $time_start;
echo printf("<div align=\"center\">page generated in %f seconds </div>", $time_elapsed);
Your looking for some sort of more advanced profiler,right?
One that I know of, but have not used, is Xdebug which claims in it's documentation (http://www.xdebug.org/docs-profiling.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. The profiler offers a number of output modes, that are suited for a variety of tasks when analyzing code.
I have been meaning to check it out, both as a debugger and profiler, but haven't gotten around to it. If you do, I would VERY MUCH appreciate it if you would post a report on your impressions.
There is also the DBG debugger/profiler which I have tried but never managed to get working, so I can't recommend it. If you figure out how to use it, I would love it if you would start a tutorial thread.
Another that might be promising, but haven't really looked into is..
Adepteo profiler. They have sample output on their site and it looks like it would do the job.
Provides information on number of calls to a code section, percentage and total of time spent on a section. Timing of nested code sections is possible.
Califa profiler (php 3 only, so probably using the native debugger and useless to most of us)
[califa.sourceforge.net...]
Another sourceforge project, PHP-Profiler, claims
PHP Profiler is both a Zend extension and module capable of generating profiling information (function call statistics) for PHP applications on the server side. No PHP code has to be changed, although the profiler data is available to your scripts.
but it has been discontinued. The last message is from 2001 and say "Project closed" because "DBG is the best profiler available". Like I said, I have never succeeded in getting DBG to work on my system.
Like I said, if you use one of these, REPORT BACK. If I ever get around to trying Xdebug and Adepteo, I will also report back.
Tom
I went for the first one Xdebug [xdebug.org ].
I installed it on Redhat 9, PHP 4.3.4. I had it up and running in 10 minutes so installation was straightforward.
This tool has a profiler that does what I want. It measures the time for each function executed, and you can sort the output in various ways. More info:[xdebug.org ].
You can start profiling in different ways. I used .htaccess, and included these lines in the .htaccess:
php_value xdebug.auto_profile 1
php_value xdebug.auto_profile_mode 4
php_value xdebug.output_dir "/php"
A very useful tool, as you don't have to change anything in the code. I've already found a couple of sloppy loops.
Thanks for the report. I discovered it a couple of weeks ago, but I'm not at the "performance optimization" stage on anything just now, so I haven't tried it.
I will definitely do so though. I'll probably try the adepteo one as well to see which one I like. I'll report back when I do.
By the way, based on the fact that you mention that you used the ".htaccess" solution, I trust that means that you can run this even on a server where you have limited privileges (i.e. no access to php.ini or httpd.conf)? I doubt I will, but it could come in handy somewhere along the line.
Cheers,
Tom
It seems to me that Xdebug has a lot less variability if you run a script repeatedly. I would think, therefore, that it is more accurate, not to mention that it gives data for every function.
The only problem so far is that I can not get it to write output to a file (which it is supposed to do as an option) and have it appended at the end of the script.
Lots of info... still figuring out what to do with it!
One last update. There is also a project, pretty advanced, called Advanced PHP Debugger, aka APD that looks pretty good.
Tom
I think the key thing is that I had set the options up with the PHP debug options, but you have to load the module and then set the options... Guess it didn't take a genius to figure that out.
There are two bothersome bugs with Xdebug (both on the bug list, so hopefully will be fixed).
1. doesn't aggregate object methods when choosing an output format that aggregates functions.
2. doesn't give info for the last function called.