How do you show page load time in php?

What is the code showing how long it took to process the PHP page?

Also, how many sql queries were done to create this page?

+3
source share
2 answers

For the first question.

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

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

The second is a bit more complicated. You need common gateways that keep track of all your requests and keep track of the progress counter. The simplest example would be a wrapper around mysql_query, which increments a static variable and then passes the query to mysql_query.

Most modern ORM implements this feature if you are already using one of them.

+12
source

mysql, , $m- > qcount; .

<?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

?>
0

Source: https://habr.com/ru/post/1711015/


All Articles