Many blog software has such a function, such as "generated this page altogether spent xx milliseconds, XX times database Query" and so on. So how is this function implemented, and below I probably say the idea.
1. Declaring a global variable in the constructor of a class
Defines a global variable $queries used to count the number of database queries generated by the page.
2. Modify the query () encapsulated in the database class
You should use the database class to find the method that encapsulates the query (), such as the following:
2 |
public function query( $query ) |
4 |
//echo $query.‘<br />‘; |
6 |
return $this ->result = mysql_query( $query , $this ->link); |
Then each time Query is executed, the global variable queries will increment by 1.
3. Write this in the method body:
1 |
public function content( $id = 0) |
3 |
$GLOBALS [ ‘queries‘ ] = 0; |
5 |
echo $GLOBALS [ ‘queries‘ ]; |
That's the way it can be done.
4. A function to calculate PHP script execution
The previous blog post describes the calculation of the PHP script execution time function, here to paste a bit.
02 |
public function runtime( $mode = 0) { |
09 |
//list($m0,$s0) = split(" ",$t); |
10 |
list( $m0 , $s0 ) = explode ( " " , $t ); |
11 |
//list($m1,$s1) = split(" ",$t1); |
[ |
; list ( $m 1 $s 1 ) = explode ( $t 1 |
13 |
return sprintf( "%.3f ms" ,( $s1 + $m1 - $s0 - $m0 )*1000); |
Use the following:
1 |
public function content( $id = 0) |
4 |
$GLOBALS [ ‘queries‘ ] = 0; |
6 |
echo $GLOBALS [ ‘queries‘ ]; |
7 |
echo $this -> runtime(1); |
PHP gets the number of database queries that generate a page (RPM)