1

我的网站在共享主机上超过 CPU 使用率时遇到问题。

我只是在做一个测试,似乎对于同一个文件,我可以获得不同的 CPU 值。我正在插入一些简单的 php 来测试简单的 htm,并且大多数情况下我得到 0 CPU 使用率。但有时这个值会增加到 25、35 甚至 85 !

同一脚本具有不同的 CPU 使用率是否正确?一个简单的脚本有多达 85 个或大约是正常的吗?

谢谢

编辑:我使用脚本来检查用法:

public static function onRequestStart() {
    $dat = getrusage();
    define('PHP_TUSAGE', microtime(true));
    define('PHP_RUSAGE', $dat["ru_utime.tv_sec"]*1e6+$dat["ru_utime.tv_usec"]);
}

public static function getCpuUsage() {
    $dat = getrusage();
    $dat["ru_utime.tv_usec"] = 
        ($dat["ru_utime.tv_sec"]*1e6 + $dat["ru_utime.tv_usec"]) - PHP_RUSAGE;
    $time = (microtime(true) - PHP_TUSAGE) * 1000000;

    // cpu per request
    if($time > 0) {
        $cpu = sprintf("%01.2f", ($dat["ru_utime.tv_usec"] / $time) * 100);
    } else {
        $cpu = '0.00';
    }

    self::add_session_data($cpu); // this add data to db or display on the screen

    return $cpu;
}

onRequestStart() 进入脚本的开头, getCpuUsage() 进入最后

4

1 回答 1

0

If you think your script is slow and you want to know which areas are best for improvements, you should not try to build your own cpu usage meter. Instead you should use a profiler such as XDebug or XHProf. They will be able to tell you which functions and methods take long to run. Those are the things you should focus on.

于 2013-02-02T15:48:53.773 回答