【问题标题】:Determining why a php script is slow to execute确定 php 脚本执行速度慢的原因
【发布时间】:2016-04-26 14:12:18
【问题描述】:

我有一个 php 脚本,它运行一个 bash 脚本来终止一个进程并启动一个新的进程实例。不幸的是,在尝试运行它时,它需要很长时间才能运行。我如何确定原因?它是 Debian 7 专用服务器上的 apache2 服务器。

php 脚本:

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');  
$start = microtime(TRUE);  
if (isset($_GET['resetnow'])) {
   $myreset = $_GET['resetnow'];
   if ($myreset == "now") {
         $cmd=getcwd()."/resetut.sh  2>&1";
         exec($cmd, $output, $return_var);
         $outputstr=implode(",", $output);
         $finish = microtime(TRUE);  
         $totaltime = $finish - $start;      
         $timetaken = " This script took ".$totaltime." seconds to run"; 
         print $cmd." ".$outputstr.$timetaken;
   } else {
         print "Didnt receive correct parameters";
   }
} else {
  print "Undefined";
}
?>

shell脚本:

#!/bin/bash
cd pathto/utorrent
killall utserver
./utstart start
a=$(pgrep utserver)
echo Utorrent restarted as new process $a

不幸的是,输出看起来像这样:

/pathto/resetut.sh 2>&1 Starting utorrent server,
Utorrent restarted as new process 25044 25381 
This script took 275.10148310661 seconds to run

太长了。如果从命令行执行 shell 脚本,则只需不到一秒的时间即可完成。

【问题讨论】:

  • 从命令行运行 php 脚本与从浏览器启动的运行时间是否相同?
  • 它被称为profiling,并且有一些工具可以做到这一点。谷歌了解更多
  • @Samsquanch 是的,我愿意
  • 这个问题可能有很多原因,其中一个是硬件问题,您必须询问原因列表并对其进行处理以忽略其中的每个项目然后剩下的原因应该是原因问题的根源。

标签: php time


【解决方案1】:

在 Linux 上,您可以使用一些工具。您可以使用“ps”或“top”命令查看所有正在运行的进程。这些将显示所有正在运行的进程的列表。我的猜测是其他程序正在利用 CPU,您可能想杀死它们。

如果不是这样,您可以使用“时间”命令进行一些时间测试。由于 PHP 和 Bash 都是 shell 脚本,与 C++ 程序相比,它们的运行速度会慢一些,因为每次运行它们时都必须解释 PHP 和 Bash。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 2022-07-22
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多