php curl 也可以這樣用

php curl 也可以這樣用 @ 不大會寫程式 :: 隨意窩 Xuite日誌

今天和同事討論一個監控的問題, 發覺 CURL 原來也可以這樣來用, CURL 可以透過 curl_getinfo 來把連線過程中, 一些數據詳細的記錄下來, 裏面有幾個時間的值, 可以當做監控服務的回應時間, 大家可以參考看看 是個滿有趣的作法 http://docs.cacti.net/usertemplate:graph:curl_bwtest_response_time

CURLINFO_TOTAL_TIME – Total transaction time in seconds for last transfer
CURLINFO_NAMELOOKUP_TIME – Time in seconds until name resolving was complete
CURLINFO_CONNECT_TIME – Time in seconds it took to establish the connection
CURLINFO_PRETRANSFER_TIME – Time in seconds from start until just before file transfer begins
CURLINFO_STARTTRANSFER_TIME – Time in seconds until the first byte is about to be transferred
CURLINFO_REDIRECT_TIME – Time in seconds of all redirection steps before final transaction was started

大家可以簡單寫支程式, 看看這些數據的變化

<!–?php
// Create a curl handle
$url = $argv[1];
$ch = curl_init();

curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, ‘Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Foxy/1; Foxy/1; .NET CLR 2.0.50727)’);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt ($ch, CURLOPT_COOKIEJAR, ‘/tmp/cookie.txt’);
curl_setopt ($ch, CURLOPT_COOKIEFILE, ‘/tmp/cookie.txt’);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array(‘Expect:’));
$store = curl_exec ($ch);

// Check if any error occured
if(!curl_errno($ch))
{
$info = curl_getinfo($ch);
print_r($info);

echo ‘Took ‘ . $info[‘total_time’] . ‘ seconds to send a request to ‘ . $info[‘url’] . “\n";
}

// Close handle
curl_close($ch);
?>

跑了之後發現同樣設定的幾組伺服器, CURLINFO_STARTTRANSFER_TIME, CURLINFO_PRETRANSFER_TIME 這兩個數值差了滿多的, 不知道在這兩個數據相減後, 這其間的時候 server 在做什麼 ?? 有幾組伺服器這個 delta 滿大的, 該怎麼消減他才好呢 ?? 看起來和 loading 也無關, 因為找了一台把他從 SLB 切開後, 這個數值並沒有很顯著的變化 !!

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *

What is 12 + 8 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)