Solved

This page was executed in this time...

Posted on 2003-10-21
5
203 Views
Last Modified: 2008-02-26
I've been quite curious for a while on how sites like phpmyadmin it says things like "SQL-query executed in .0095 seconds" etc. and on PHP pages things like "Page rendered in .94 seconds".

Also, when pages say "Executed 8 queries, and a total of 980 queries".

Just wondering how to do simple little footer details that might spruce up a site footer.

Thanks in advance.
0
Comment
Question by:drakkarnoir
5 Comments
 
LVL 13

Expert Comment

by:lozloz
ID: 9595611
hi,

this should work

<?
$start = microtime();
$starta = explode(" ", $start);
$start = $starta[1] + $starta[0];
?>
your content here
<?
$end = microtime();
$enda = explode(" ", $end);
$end = $enda[1] + $end[0];
$overall = $end - $start;
print "Page rendered in " . $overall . " microseconds";
?>
0
 
LVL 1

Accepted Solution

by:
paStiSSet earned 250 total points
ID: 9595762
test this one (it works succesfully):

<?
function getmicrotime() {  
    list($usec, $sec) = explode(" ",microtime());  
    return ((float)$usec + (float)$sec);  
}
$start_time = getmicrotime();
?>

you can put this anywhere (header its a good idea)

<?
$end_time = getmicrotime();
$total_time = round($end_time - $start_time,3);  # the "3" returns: X.XXX, if put "2" this will return X.XX
echo "page generated in $total_time seconds";
?>

and this other one prints the x.xxx secons you talking about
0
 
LVL 2

Assisted Solution

by:ASCII_Man
ASCII_Man earned 250 total points
ID: 9597866
The option from paStiSSet will work a jiffy for page execution time. As for the number of SQL queries, I alias the mysql_query function for that:
----------------------------------
function query($sql){
      global $sql_num_queries, $sql_num_rows, $sql_num_fields, $sql_time;

      if(!isset($num_queries))
            $num_queries = 1;
      else
            $num_queries++;

      $start_query_time = getmicrotime();
      $query = mysql_query($sql);
      $end_query_time = getmicrotime();

      if(!isset($sql_num_rows))
            $sql_num_rows = mysql_num_rows($query);
      else
            $sql_num_rows += mysql_num_rows($query);
      if(!isset($sql_num_fields))
            $sql_num_fields = mysql_num_fields($query);
      else
            $sql_num_fields += mysql_num_fields($query);

      if(!isset($sql_time))
            $sql_time = ($end_query_time - $start_query_time);
      else
            $sql_time += ($end_query_time - $start_query_time);
}
----------------------------------

Then embed something more like this at the end of your page:
<?php
$total_page_time = round(getmicrotime() - $start_time, 3);
$total_sql_time = round($sql_time, 3);
echo <<<END
Statistics:
 Page generated in $total_page_time seconds
 Read $sql_num_fields within $sql_num_rows rows from $sql_num_queries queries in $total_sql_time seconds
END;
?>

You can add all sorts of useless but interisting server stats if you're bored enough - current number of visitors, bandwidth usage, server uptime, number of SETI data packets crunched...
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9598796
ignore my script, its poo
0
 

Author Comment

by:drakkarnoir
ID: 9599150
Thank you so so much everybody for your input.

ASCII_Man, when you said I could calculate bandwith, how would I go about doing that? I've seen it done on cPanelX (like that Bandwith used this month) but how would I go about doing that via PHP?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now