Forum Moderators: coopster

Message Too Old, No Replies

Getting page "build time" in php

Looking for a method

         

trillianjedi

3:47 pm on Mar 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've got to move a site to a new server and I'm interested in doing a performance comparison.

Is there a way I can pull total page build time out of PHP? Most pages are created from several included scripts and I'm not sure if that's an issue.

Alternatively, I could set a variable to some clocktime in header.php, and subtract that from current time in footer.php, and echo that out.

To do that, is there some php equivilent to C's "tickcount"?

I'm looking for millisecond accuracy, ideally.

Thanks!

TJ

Tastatura

3:57 pm on Mar 29, 2006 (gmt 0)

10+ Year Member



I think that you can use microtime().

[php.net ]

As you said, obtain start time and stop time as first and last (well almost) lines on the page, and then subtract them

trillianjedi

4:04 pm on Mar 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Microtime : "float value in microseconds" - that'll do nicely.

Thanks!

TJ

coopster

4:07 pm on Mar 29, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Hey TJ, here is a thread with some example code in it.

[webmasterworld.com...]

The original poster of the thread probably long forgot this example ;-)

trillianjedi

4:11 pm on Mar 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



LOL!

<--- hangs head in shame ;)

I had completely forgotten about that one (although in my defence I wasn't so interested in the method of timing as the results!).

coopster

4:34 pm on Mar 29, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Ah, I'm just having fun with ya ;)
All in good sport. That is PHP5 though, they made it easier to use microtime in PHP5. PHP4 requires your own function more or less.
<?php 
// PHP5 version:
$time_start = microtime(true);
// Sleep for a while
usleep(1000);
$time_end = microtime(true);
$time = $time_end - $time_start;
printf("<pre>Did nothing in %0.15f seconds\n\n\n</pre>", $time);
// PHP4 version:
function getmicrotime($mt) {
list($msec, $sec) = explode(' ', $mt);
return ((float)$msec + (float)$sec);
}
$time_start = microtime();
for ($i=0; $i < 1000; $i++){
// do nothing, 1000 times
}
$time_end = microtime();
$time_elapsed = getmicrotime($time_end) - getmicrotime($time_start);
printf("Did nothing in %0.15f seconds<br />\n", $time_elapsed);

trillianjedi

7:00 pm on Mar 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks coop, useful bit of php for a copy/paste coder like me ;)

TJ

trillianjedi

12:38 pm on May 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, I was a bit late in testing this, but I'm getting a strange result using the exact code shown above by coopster (for PHP4):-

This page built in 1146919887.045933961868286 seconds

That can't be right ;)

I assume something is wrong in the format statement? Couple of significant digits out. Gut feeling says the figure after the decimal point is about right (1/2 second or so).

trillianjedi

10:43 am on May 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Bump :)

Could it be that the format is inverted? i.e. the bit after the decimal point, should actually be at the front?

eelixduppy

11:17 am on May 8, 2006 (gmt 0)



microtime -- Returns current Unix timestamp in microseconds

Are you sure that it is in seconds? :)

eelix

trillianjedi

11:27 am on May 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, that was a typo :)

I believe it should be divided by 1 million(?) which would show the figure to be wildly wrong - about 18 minutes.

When actually it's taking about 1/2 second (my best guess).

So something is wrong either in the function, in the format of the output or in my maths....

Romeo

3:03 pm on May 8, 2006 (gmt 0)

10+ Year Member



Hi TJ,

this looks like one full microtime timestamp, not a difference between timestamps.
Are you sure you did printf $time_elapsed? Or is your $time_start == 0 for some unknown reason?

You may print out all 3 $time_ values (_start, _end, _elapsed) to debug this.

The "1146919887.x" part looks like the common Unix epoch time in seconds since 1970-01-01 00:00 and translates to 2006-05-06 12:51:27.

Kind regards,
R.

trillianjedi

4:33 pm on May 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The "1146919887.x" part looks like the common Unix epoch time

Good spot Romeo - thanks. That might point me in the right direction to find the bug. I'll double check code...

:)

trillianjedi

7:30 pm on May 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, I can't find any obvious errors, so I'm a bit stumped.

Here's the exact code, snipped as necessary.

Function code:-

function getmicrotime($mt) {
list($msec, $sec) = explode(' ', $mt);
return ((float)$msec + (float)$sec);
}

In the header bit I do this:-


$time_start = microtime();

In the footer I do:-


$time_end = microtime();
$time_elapsed = getmicrotime($time_end) - getmicrotime($time_start);

printf("<p>This page built in %0.15f seconds</p>\n", $time_elapsed);

Result is as per my post above - clearly a math error somewhere.

Any thoughts?

I'm using php4.

I'm sure it's simple, but I don't know enough php to spot it...

Thanks ;)

coopster

7:45 pm on May 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Looks fine to me. What happens if you dump the $time_start and $time_end variables to the browser before you do the math? What do you get for values in those variables?

eelixduppy

7:47 pm on May 9, 2006 (gmt 0)



Hmmm.... I'm not sure but this script seems to work taken from [us3.php.net...]


<?php

$startTime = array_sum(explode(" ",microtime()));

//php code

$endTime = (array_sum(explode(" ",microtime())) - $startTime);
echo $endTime.' seconds';
?>

Hope this helps

eelix

trillianjedi

8:29 pm on May 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



dump variables before maths

This:-

echo "<p>start : ".$time_start." end : ".$time_end."</p>";

Produces this:-

start : end : 0.92069000 1147209248

Bit odd?

eelixduppy - thanks, haven't had a chance to try that one yet - plus I'm curious as to why this one doesn't work ;)

TJ

coopster

8:45 pm on May 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Bit odd?

Yes, indeed. But what that is telling us is that there is nothing in $time_start. Remember that when microtime() is called without the optional argument (which you can't do unless you are using PHP5 anyway), then the function returns the string "msec sec" where sec is the current time measured in the number of seconds since the Unix Epoch (0:00:00 January 1, 1970 GMT), and msec is the microseconds part. Both portions of the string are returned in units of seconds. So, there is nothing being returned in $time_start, therefore no value displayed, and then the "end: " and $time_end value is displayed.

You don't have $time_start in a function or something do you? Turn up your error_reporting for a bit too, I bet you have an undefined variable error there.

trillianjedi

4:39 pm on May 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Makes perfect sense Coop.

I tried changing the var names to something pretty random, but that hasn't helped.

How do you turn up error_reporting....?

:)

coopster

5:21 pm on May 10, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Place this at the top of your script.

error_reporting [php.net](E_ALL);