2010-09-11 17 views
1

在PHP中,有沒有一種方法可以捕獲我執行的每種ImagaMagick/GraphicsMagick方法的CPU使用情況?內存使用情況也會很好。PHP:獲取CPU使用ImageMagick/GraphicsMagick

基本上,我想基準每個庫在我的應用程序中使用多少資源。

失敗的嘗試:

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

內存使用量應該是在每次打電話給你想要測量,然後減去他們之後記錄memory_get_usage(true)memory_get_peak_usage(true)輸出的問題。

CPU使用率我不確定。在UNIX系統上,可能需要執行exec()以獲得ps -ef | grep $phpPid的輸出,然後解析該輸出。你可以得到$phpPid = getmypid();

+0

如何獲得轉換過程的PID(我通過exec()從PHP調用的庫)?我想這就是我感興趣的CPU使用率。 – StackOverflowNewbie 2010-09-11 09:22:30

+0

如果它是php進程本身,那麼'getmypid()'會給你它的進程ID。如果它是一個單獨的庫,請爲它找到它的二進制可執行文件名和'grep':'ps -ef | grep $ binaryName'。 – Fanis 2010-09-11 09:52:25

+0

ps -ef沒有給我我需要的東西,但ps -eo comm,%cpu,%mem是。試圖按照你的建議,我創建了一個腳本,但仍然不適合我。我原來的文章已經被編輯,以向你展示我做了什麼。你看到我在做什麼錯了嗎? – StackOverflowNewbie 2010-09-11 12:57:00