簡體   English   中英

測量連接時間是否像php這樣的好習慣? 有沒有更好的辦法?

[英]Is measuring connection time like this good practice for php? Is there a better way?

我需要連續多次測量Web服務的響應時間,我過去經常對單個連接執行此操作(沒有循環),但是現在這樣做似乎不合適,盡管看起來正在工作。 任何建議將不勝感激。

$servername = "x.x.x.x";
$username = "xxx";
$password = "xxx";
$db = "xxx";

for($i=0; $i<10;++$i){

  $ch = $_SESSION['cURL'];
  $time_pre = microtime(true);
  $data = curl_exec($ch);
  $time_pro = microtime(true);
  $exec_time[$i] = $time_pro - $time_pre;
 }

$conn = mysqli_connect($servername, $username, $password, $db);

//Check connection
if ($conn->connect_error){
    die("Connection failed: " . $conn->connect_error);
}

date_default_timezone_set('Europe/Madrid');
$date = date('Y-m-d H:i:s');
var_dump($date);

for($i=0; $i<10;++$i){
    $query = "INSERT INTO DB(time, date) VALUES('$exec_time[$i]', '$date')";
    $result2=mysqli_query($conn, $query);
}

我最終在評論中使用了勞倫斯·謝羅內的建議。 它比我正在使用的更快,更干凈。 http://php.net/manual/zh/function.curl-multi-exec.php

//add the two handles
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);

$active = null;
//execute the handles
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}

//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM