Forum Moderators: coopster

Message Too Old, No Replies

PHP code performance testing

Is there a smart way?

         

johannes

11:36 am on Jan 16, 2004 (gmt 0)

10+ Year Member



When you have written a large piece of code, you want to test it for performance. If you find a loop that steal a lot of time, you can tune it for best performance.

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?

seomike2003

1:48 pm on Jan 16, 2004 (gmt 0)



Not that I know of. PHP way faster than other dynamic languages. but if you are worried about time tack a zend engine on the server. It will increase your output speed very much.

jatar_k

4:46 pm on Jan 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I use this

<?
// 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>";

?>

WibbleWobble

5:13 pm on Jan 16, 2004 (gmt 0)

10+ Year Member



That's pretty sweet, jatar. While I appreciate this is like asking the length of a piece of string, what do you consider to be a good result, using that?

jatar_k

5:38 pm on Jan 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It completely depends on what you are testing.

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

coopster

8:37 pm on Jan 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Just a side note:

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

Nice.

johannes

10:06 am on Jan 17, 2004 (gmt 0)

10+ Year Member



Thanks for the input, but none of this was what I was looking for.

ergophobe

5:30 pm on Jan 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Johannes,

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.

[adepteo.net...]

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

ergophobe

5:32 pm on Jan 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




PHP way faster than other dynamic languages

Is this true? Last time I saw a "shoot out" of scripting languages, PHP was pretty slow. It got creamed by PERL for many tasks. This is probably not the place to bring up that topic, but I'm not sure it's true.

Tom

johannes

7:33 am on Jan 19, 2004 (gmt 0)

10+ Year Member



Thanks a lot Tom, this saved me a ton of time!

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.

ergophobe

4:07 pm on Jan 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Johannes,

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

johannes

6:35 pm on Jan 19, 2004 (gmt 0)

10+ Year Member



You can start profiling by issuing commands in the php code. So if you can get xdebug installed, you don't need access to .htaccess or php.ini.

g1smd

12:18 am on Jan 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When debuggung stuff like this I always output the results into HTML <!-- comment --> tags so that the results do not appear on the webpage itself.

ergophobe

7:02 pm on Feb 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Incidentally, I have started using Xdebug. Just for kicks, I decided to time a script using both old-fashioned microtime() and Xdebug.

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

mipapage

5:47 pm on Feb 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Great find!

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.

Any luck with this?

ergophobe

7:42 pm on Feb 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Oh yeah, I did get it to work. Can't remember why it didn't work at first.

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.