PHP에서 스크립트 실행 시간 추적 접근 할 수있는 방법이 있습니까? 실제

PHP는 max_execution_time 제한을 적용하기 위해 특정 스크립트가 사용한 CPU 시간을 추적해야합니다.

스크립트 안에서 이것에 접근 할 수있는 방법이 있습니까? 실제 PHP에서 CPU가 얼마나 많이 태워 졌는지에 대한 테스트와 함께 로깅을 포함하고 싶습니다 (스크립트가 앉아서 데이터베이스를 기다리는 경우 시간이 증가하지 않음).

Linux 상자를 사용하고 있습니다.



답변

유닉스 시스템 (및 Windows의 PHP 7 이상)에서는 다음 과 같이 getrusage 를 사용할 수 있습니다 .

// Script start
$rustart = getrusage();

// Code ...

// Script end
function rutime($ru, $rus, $index) {
    return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
     -  ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
}

$ru = getrusage();
echo "This process used " . rutime($ru, $rustart, "utime") .
    " ms for its computations\n";
echo "It spent " . rutime($ru, $rustart, "stime") .
    " ms in system calls\n";

모든 테스트에 대해 PHP 인스턴스를 생성하는 경우 차이를 계산할 필요가 없습니다.


답변

CPU 실행 시간이 아닌 벽시계 시간 만 있으면 다음과 같이 간단하게 계산할 수 있습니다.

//place this before any script you want to calculate time
$time_start = microtime(true);

//sample script
for($i=0; $i<1000; $i++){
 //do anything
}

$time_end = microtime(true);

//dividing with 60 will give the execution time in minutes otherwise seconds
$execution_time = ($time_end - $time_start)/60;

//execution time of the script
echo '<b>Total Execution Time:</b> '.$execution_time.' Mins';
// if you get weird results, use number_format((float) $execution_time, 10) 

여기에는 PHP가 max_execution_time에 사용되지 않는 디스크 또는 데이터베이스와 같은 외부 리소스를 기다리는 시간이 포함됩니다.


답변

talal7860의 더 짧은 버전의 답변

<?php
// At start of script
$time_start = microtime(true);

// Anywhere else in the script
echo 'Total execution time in seconds: ' . (microtime(true) - $time_start);

지적했듯이, 이것은 ‘cpu 시간’이 아닌 ‘wallclock time’입니다.


답변

가장 쉬운 방법 :

<?php

$time1 = microtime(true);

//script code
//...

$time2 = microtime(true);
echo 'script execution time: ' . ($time2 - $time1); //value in seconds


답변

<?php
// Randomize sleeping time
usleep(mt_rand(100, 10000));

// As of PHP 5.4.0, REQUEST_TIME_FLOAT is available in the $_SERVER superglobal array.
// It contains the timestamp of the start of the request with microsecond precision.
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];

echo "Did nothing in $time seconds\n";
?>


답변

즉시 사용 가능한 phihag 답변에서 ExecutionTime 클래스를 만들었습니다.

class ExecutionTime
{
     private $startTime;
     private $endTime;

     public function start(){
         $this->startTime = getrusage();
     }

     public function end(){
         $this->endTime = getrusage();
     }

     private function runTime($ru, $rus, $index) {
         return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
     -  ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
     }

     public function __toString(){
         return "This process used " . $this->runTime($this->endTime, $this->startTime, "utime") .
        " ms for its computations\nIt spent " . $this->runTime($this->endTime, $this->startTime, "stime") .
        " ms in system calls\n";
     }
 }

용법:

$executionTime = new ExecutionTime();
$executionTime->start();
// code
$executionTime->end();
echo $executionTime;

참고 : PHP 5에서 getrusage 함수는 Unix-oid 시스템에서만 작동합니다. PHP 7부터는 Windows에서도 작동합니다.


답변

developerfusion.com의 Gringod는 다음과 같은 좋은 답변을 제공합니다.

<!-- put this at the top of the page -->
<?php
   $mtime = microtime();
   $mtime = explode(" ",$mtime);
   $mtime = $mtime[1] + $mtime[0];
   $starttime = $mtime;
;?>

<!-- put other code and html in here -->


<!-- put this code at the bottom of the page -->
<?php
   $mtime = microtime();
   $mtime = explode(" ",$mtime);
   $mtime = $mtime[1] + $mtime[0];
   $endtime = $mtime;
   $totaltime = ($endtime - $starttime);
   echo "This page was created in ".$totaltime." seconds";
;?>

( http://www.developerfusion.com/code/2058/determine-execution-time-in-php/ )에서