Queries Made & Load time Code

Discussion in 'PHP' started by richbate, Jun 6, 2007.

  1. #1
    What is the code used for displaying howmany SQL Querys was sent and how long its taken to generate the page?

    Example:

    - [ Best Viewed At 1024x768 In Firefox ] - [ Queries Used: 21 ] - [ Generation Time: 0.00036 ] -

    I want to be able to use this in an aid on optimizing my site and looking at pages giving the most server strain.

    Rich
     
    richbate, Jun 6, 2007 IP
  2. damonp

    damonp Peon

    Messages:
    97
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    For page generation time you just need to mark the page start time at the top of your script and stop time at the end and subtract. There are some examples at:
    http://php.net/microtime

    Number of queries will depend on how you are accessing the DB. If through an abstraction class, it may have a function to return this info. If directly through PHP, you will need to count the queries as they happen and output the total.
     
    damonp, Jun 6, 2007 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    count mysql queries something like

    
    <?php
    function counted_mysql_query( $sql, $link )
    {
    	global $sql_queries ;
    	if( ( $res = mysql_query( $sql, $link )) )
    	{
    		$sql_queries++;
    		return $res;	
    	}	
    }
    ?>
    <?
    // plenty of code with lots of queries
    ?>
    <div id="blah">So far <?=$sql_queries ?> sql querie(s) have been executed</div>
    
    PHP:
    assuming you do not have the afforementioned abstraction class to hand

    time your script with something like

    
    <?php
    
    /** wasting time **/
    function do_massive_loop( )
    {
    	for( $i = 0; $i < 1000; $i++ )
    	{
    		printf(".\n", $i );
    	}
    }
    $start = microtime( );
    do_massive_loop( );
    echo "<br />\n";
    printf("Script executed in %s seconds<br />\n", round( microtime( ) - $start , 5 ) );
    
    PHP:
     
    krakjoe, Jun 6, 2007 IP