Home > Backend Development > PHP Tutorial > Performance Monitoring - How to detect how many times a certain PHP method is called in a request, and the time and memory consumption of each time?

Performance Monitoring - How to detect how many times a certain PHP method is called in a request, and the time and memory consumption of each time?

WBOY
Release: 2023-03-01 19:50:02
Original
1833 people have browsed it

How to detect how many times a certain PHP method is called in a request, and the time and memory consumption of each time?

Reply content:

How to detect how many times a certain method in PHP is called in one request, and the time and memory consumption each time?

<code class="php">function  microtime_float ()
{
    list( $usec ,  $sec ) =  explode ( " " ,  microtime ());
    return ((float) $usec  + (float) $sec );
}

  function test(){
     static $num = 0;
     $num ++;
     $memory =  memory_get_usage () ;
     $time_start  =  microtime_float ();
     // 操作过程略
     usleep ( 100 );
     $m = memory_get_usage () -$memory;//内存
     $t =  microtime_float () -$time_start;//耗时
     return [$num,$m,$t];
}
test();
test();
print_r(test());
Array
(
    [0] => 3
    [1] => 0
    [2] => 0.00016498565673828
)</code>
Copy after login

Maybe you need this

Configure xdebug + kcachegrind and it will be fine.

Performance Monitoring - How to detect how many times a certain PHP method is called in a request, and the time and memory consumption of each time?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template