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
WBOYOriginal
2016-12-01 00:26:011834browse

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>

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?

Statement:
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