简体   繁体   English

如何计算执行时间(php指令“ max_execution_time”?)

[英]How to calculate execution time (php directive 'max_execution_time'? )

A few days ago wrote a php script that goes through all my music and reads id3 tags for each song and inserts those in a mysql database. 几天前写了一个php脚本,它遍历了我所有的音乐,并读取每首歌曲的id3标签,并将这些标签插入mysql数据库。 Here's a snippet for context: 这是上下文的代码段:

exec ( "find /songs -type f -iname '*mp3'", $song_path );
$number_of_songs = count($song_path);
for($i=0; $i<$number_of_songs; $i++){
    //read id3 tags
    //store id3 tags into database
}

I changed the php directive max_execution_time in apache2/php.ini to gain a better understanding of what this directive does. 我更改了apache2 / php.ini中的php指令max_execution_time ,以更好地了解此指令的作用。

When I set max_execution_time = 10 , my php script runs for about 45 seconds and successfully reads the id3 tags for about 150 songs (Out of thousands of songs) and inserts those tags into a mysql database before terminating the script and outputting the following to the screen: 当我设置max_execution_time = 10 ,我的php脚本运行了大约45秒,并成功读取了大约150首歌曲(数千首歌曲中的歌曲)的id3标签,并将这些标签插入mysql数据库中,然后终止脚本并将以下内容输出到屏幕:

Fatal error: Maximum execution time of 10 seconds exceeded in /websites/.../public_html/GetID3()/getid3/module.audio.mp3.php on line 1894 致命错误:在1894行的/websites/.../public_html/GetID3()/getid3/module.audio.mp3.php中,超过10秒的最大执行时间

From the documentation, 'The maximum execution time is not affected by system calls, stream operations etc' http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time 从文档中, “最大执行时间不受系统调用,流操作等影响” http://www.php.net/manual/zh/info.configuration.php#ini.max-execution-time

  1. What can I infer from the difference between the maximum_execution_time being set at 10 seconds and the script running for a total of 45 seconds before terminating? maximum_execution_time设置为10秒与脚本在终止之前运行总共45秒之间的差异可以推断出什么? Does this mean out of the 45 seconds, 35 were spent doing non-php related activities like reading the id3 tags, inserting data into mysql etc..., while 10 were spent doing php related activities like iterating the loop? 这是否意味着在45秒中,有35人花费在与非PHP相关的活动上,例如读取id3标签,将数据插入mysql等...,而10人花费在与PHP相关的活动(如迭代循环)上?

  2. Is there a way I can calculate the execution time and print it to the screen? 有什么方法可以计算执行时间并将其打印到屏幕上吗?

EDIT Using the timer Dagon suggested, I called the getTime() function at the end of the loop, there were about 100+ iterations of the loop. 编辑使用建议的计时器Dagon,我在循环结束时调用了getTime()函数,该循环大约进行了100多次迭代。 Here is the output to my browser: 这是我的浏览器的输出:

0.1163 seconds 0.1163秒

0.8142 seconds 0.8142秒

1.1379 seconds 1.1379秒

1.5555 seconds 1.5555秒

... ...

76.7847 seconds 76.7847秒

77.2008 seconds 77.2008秒

77.6071 seconds 77.6071秒

Fatal error: Maximum execution time of 10 seconds exceeded in /websites/.../public_html/GetID3()/getid3/module.audio.mp3.php on line 505 致命错误:在第505行的/websites/.../public_html/GetID3()/getid3/module.audio.mp3.php中,超过10秒的最大执行时间

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

<!-- Write Your script(executable code) here  -->

enter code here

<!-- Paste  this code at the bottom of the page -->
<?php
   $mic_time = microtime();
   $mic_time = explode(" ",$mic_time);
   $mic_time = $mic_time[1] + $mic_time[0];
   $endtime = $mic_time;
   $total_execution_time = ($endtime - $start_time);
   echo "Total Executaion Time ".$total_execution_time." seconds";
?>

i don't believe the script is actully running for more than 10 seconds, you need to put a proper timer in it 我不认为该脚本可以正常运行10秒钟以上,您需要在其中放置一个适当的计时器

<!-- put this at the top of the page -->
<?php
function getTime() {
    $timer = explode( ' ', microtime() );
    $timer = $timer[1] + $timer[0];
    return $timer;
}

$start = getTime();
?> 

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

<!-- put this code at the bottom of the page -->
<?php
$end = getTime();
$time_took=  round($end - $start,4).' seconds';
echo $time_took;
?>

This type of script should really be executed in the CLI environment, not in a php process executed by your web server. 这类脚本实际上应该在CLI环境中执行,而不是在Web服务器执行的php进程中执行。 As per the manual docs on how the PHP command line environment differs from other PHP SAPIs: 根据有关PHP命令行环境与其他PHP SAPI有何不同的手册文档

PHP in a shell environment tends to be used for a much more diverse range of purposes than typical Web-based scripts, and as these can be very long-running, the maximum execution time is set to unlimited . 与典型的基于Web的脚本相比,shell环境中的PHP倾向于用于多种用途,并且由于它们运行时间很长, 因此最大执行时间设置为无限制

While it doesn't answer your question, it does solve your problem :) 虽然它不能回答您的问题,但可以解决您的问题:)

Seems like you not only try to measure the script duration, but also try to limit it. 似乎您不仅尝试测量脚本持续时间,还尝试限制脚本持续时间。 And in your case max_execution_time is not what you want. 在您的情况下, max_execution_time不是您想要的。

Basically, "The maximum execution time is not affected by system calls, stream operations etc" is correct. 基本上, “最大执行时间不受系统调用,流操作等影响”是正确的。 If you need to limit real-time script length, you need to implement your own time measurement. 如果需要限制实时脚本长度,则需要实施自己的时间测量。 People usually write some benchmark class for it, which after all will be helpful in optimizing the script, but simple 人们通常会为此编写一些基准测试类,毕竟这将有助于优化脚本,但很简单

$timer['start'] = time();
$timer['max_exec_time'] = 10; // seconds

at start and 在开始和

if (time() > $timer['start'] + $timer['max_exec_time'])
    break; // or exit; etc

at the end of the loop or anywhere else you want should be enough. 在循环结束时或您想要的其他任何地方都应该足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM