PHP:获取ImageMagick / GraphicsMagick的CPU使用率

时间:2010-09-11 07:26:15

标签: php benchmarking

在PHP中,有没有办法可以捕获我执行的每个ImagaMagick / GraphicsMagick方法的CPU使用情况?内存使用情况也不错。

基本上,我正在尝试对每个库在我的应用程序中使用的资源进行基准测试。

尝试失败:

exec('convert a.jpg a.png');

$result = array();

// Loop until process is detected
do
{
    exec('ps -eo comm,%cpu,%mem | grep ' . "convert", $result);
}
while($result === array());

// Display the changing CPU and memory usage of the process
do
{
    print_r($result);
    exec('ps -eo comm,%cpu,%mem | grep ' . "convert", $result);
}
while($result !== array());

1 个答案:

答案 0 :(得分:1)

内存使用情况应该是在您要测量的每次通话之前和之后记录memory_get_usage(true)memory_get_peak_usage(true)的输出,然后减去它们。

CPU使用率我不确定。在unix系统上,可能需要执行exec()来获取ps -ef | grep $phpPid的输出,然后解析它。您可以获得$phpPid = getmypid();