bigsurf
bigsurf

Reputation: 105

how do you show page load time in php?

What is the code to show how long it took for PHP to process a page?

Also, how many sql queries were performed to generate that page?

Upvotes: 4

Views: 5512

Answers (2)

pjau
pjau

Reputation: 925

For the query count, in this mysql class i have made, you would echo $m->qcount; to display how many querys have been run.

<?php

class mysql
{
      var $db_connection = 0;
      var $qcount = 0;

      function connect( $host='', $user='', $password='', $database='' )
      {
            $this->db_connection = @mysql_connect( $host, $user, $password );

            if(!$this->db_connection)
            {
                  die("Cannot connect to mySQL Host.");
            }
            else
            {
                  $select = mysql_select_db($database, $this->db_connection);

                  if(!$select)
                  {
                        die("Cannot connect to DB.");
                  }
                  else
                  {
                        return $select;
                  }
            }
      }

      function query($info)
      {
            $this->qcount++;

            return mysql_query($info, $this->db_connection);
      }

      function fetch($info)
      {
            return mysql_fetch_array($info);
      }
}

$m = new mysql;

$m->connect("hostname","username","password","database");

$m->query("blah blah blah");
$m->query("blah blah blah");

echo $m->qcount; // returns a count of 2

?>

Upvotes: 0

Simone Carletti
Simone Carletti

Reputation: 176552

For the first question.

$start = microtime(true);
$end = microtime(true);

printf("Page was generated in %f seconds", $end - $start);

The second one is a little more complicated. You need an shared gateways that keeps tracks of all your queries and stores an execution counter. The most simple example can be a wrapper method around the mysql_query that increments a static variable and then passes the query to mysql_query.

The most part of modern ORM implements that feature, if you already use one of them.

Upvotes: 13

Related Questions