PHPでスクリプトの実行時間を追跡する 質問する

PHPでスクリプトの実行時間を追跡する 質問する

PHP は、max_execution_time 制限を適用するために、特定のスクリプトが使用した CPU 時間の量を追跡する必要があります。

スクリプト内でこれにアクセスする方法はありますか? 実際の PHP で CPU がどれだけ消費されたかについてのログをテストに含めたいと思います (スクリプトがデータベースを待機している間は時間は増加しません)。

私はLinuxボックスを使用しています。

ベストアンサー1

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

おすすめ記事