前端開發中的性能那點事(二)巧用curl 併發減小後端訪問時間

前言:
在咱們平時的程序中不免出現同時訪問幾個接口的狀況,平時咱們用curl進行訪問的時候,通常都是單個、順序訪問,假若有3個接口,每一個接口耗時500毫秒那麼咱們三個接口就要花費1500毫秒了,這個問題太頭疼了嚴重影響了頁面訪問速度,有沒有可能併發訪問來提升速度呢?今天就簡單的說一下,利用curl併發來提升頁面訪問速度,
但願你們多指導。
一、老的curl訪問方式以及耗時統計 php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
functioncurl_fetch($url,$timeout=3){
    $ch= curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data= curl_exec($ch);
    $errno= curl_errno($ch);
    if($errno>0) {
        $data= false;
    }
    curl_close($ch);
    return$data;
}
functionmicrotime_float()
{
   list($usec,$sec) =explode(" ", microtime());
   return((float)$usec+ (float)$sec);
}
$url_arr=array(
     "taobao"=>" http://www.taobao.com",
     "sohu"=>" http://www.sohu.com",
     "sina"=>" http://www.sina.com.cn",
     );
 $time_start= microtime_float();
 $data=array();
 foreach($url_arras$key=>$val)
 {
     $data[$key]=curl_fetch($val);
 }
 $time_end= microtime_float();
 $time=$time_end-$time_start;
 echo"耗時:{$time}";
?>

耗時:0.614秒
二、curl併發訪問方式以及耗時統計 node

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
functioncurl_multi_fetch($urlarr=array()){
    $result=$res=$ch=array();
    $nch= 0;
    $mh= curl_multi_init();
    foreach($urlarras$nk=>$url) {
        $timeout=2;
        $ch[$nch] = curl_init();
        curl_setopt_array($ch[$nch],array(
        CURLOPT_URL =>$url,
        CURLOPT_HEADER => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT =>$timeout,
        ));
        curl_multi_add_handle($mh,$ch[$nch]);
        ++$nch;
    }
    /* wait for performing request */
    do{
        $mrc= curl_multi_exec($mh,$running);
    }while(CURLM_CALL_MULTI_PERFORM ==$mrc);
 
    while($running&&$mrc== CURLM_OK) {
        // wait for network
        if(curl_multi_select($mh, 0.5) > -1) {
            // pull in new data;
            do{
                $mrc= curl_multi_exec($mh,$running);
            }while(CURLM_CALL_MULTI_PERFORM ==$mrc);
        }
    }
 
    if($mrc!= CURLM_OK) {
        error_log("CURL Data Error");
    }
 
    /* get data */
    $nch= 0;
    foreach($urlarras$moudle=>$node) {
        if(($err= curl_error($ch[$nch])) =='') {
            $res[$nch]=curl_multi_getcontent($ch[$nch]);
            $result[$moudle]=$res[$nch];
        }
        else
        {
            error_log("curl error");
        }
        curl_multi_remove_handle($mh,$ch[$nch]);
        curl_close($ch[$nch]);
        ++$nch;
    }
    curl_multi_close($mh);
    return $result;
}
$url_arr=array(
     "taobao"=>" http://www.taobao.com",
     "sohu"=>" http://www.sohu.com",
     "sina"=>" http://www.sina.com.cn",
     );
functionmicrotime_float()
{
   list($usec,$sec) =explode(" ", microtime());
   return((float)$usec+ (float)$sec);
}
$time_start= microtime_float();
$data=curl_multi_fetch($url_arr);
$time_end= microtime_float();
$time=$time_end-$time_start;
 echo"耗時:{$time}";
?>

耗時:0.316秒
帥氣吧整個頁面訪問後端接口的時間節省了一半
三、curl相關參數
來自:http://cn2.php.net/manual/en/ref.curl.php
curl_close — Close a cURL session
curl_copy_handle — Copy a cURL handle along with all of its preferences
curl_errno — Return the last error number
curl_error — Return a string containing the last error for the current session
curl_exec — Perform a cURL session
curl_getinfo — Get information regarding a specific transfer
curl_init — Initialize a cURL session
curl_multi_add_handle — Add a normal cURL handle to a cURL multi handle
curl_multi_close — Close a set of cURL handles
curl_multi_exec — Run the sub-connections of the current cURL handle
curl_multi_getcontent — Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set
curl_multi_info_read — Get information about the current transfers
curl_multi_init — Returns a new cURL multi handle
curl_multi_remove_handle — Remove a multi handle from a set of cURL handles
curl_multi_select — Wait for activity on any curl_multi connection
curl_setopt_array — Set multiple options for a cURL transfer
curl_setopt — Set an option for a cURL transfer
curl_version — Gets cURL version information 後端

相關文章
相關標籤/搜索