技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> 系统架构 --> 前端开发中的性能那点事(二)巧用curl 并发减少后端访问时间

前端开发中的性能那点事(二)巧用curl 并发减少后端访问时间

浏览:2598次  出处信息

前言:
在我们平时的程序中难免出现同时访问几个接口的情况,而且用老的curl进行访问的时候,一般都是单个、顺序访问,假如有3个接口,每个接口耗时500毫秒那么我们三个接口加起来就是1500毫秒了,这个问题太头疼了,严重影响了页面访问速度,有没有可能并发访问来提高速度呢?今天就简单的说一下,利用curl并发来提高页面访问速度,希望大家多指导。

1、老的curl访问方式以及耗时统计

 
01 function curl_fetch($url, $timeout=3){
02     $ch = curl_init();
03     curl_setopt($ch, CURLOPT_URL, $url);
04     curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
05     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
06     $data = curl_exec($ch);
07     $errno = curl_errno($ch);
08     if ($errno>0) {
09         $data = false;
10     }
11     curl_close($ch);
12     return $data;
13 }
14 function microtime_float()
15 {
16    list($usec, $sec) = explode(" ", microtime());
17    return ((float)$usec + (float)$sec);
18 }
19 $url_arr=array(
20      "taobao"=>"http://www.taobao.com",
21      "sohu"=>"http://www.sohu.com",
22      "sina"=>"http://www.sina.com.cn",
23      );
24  $time_start = microtime_float();
25  $data=array();
26  foreach ($url_arr as $key=>$val)
27  {
28      $data[$key]=curl_fetch($val);
29  }
30  $time_end = microtime_float();
31  $time = $time_end - $time_start;
32  echo "耗时:{$time}";

耗时:0.614秒

2、curl并发访问方式以及耗时统计

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

耗时:0.316秒

帅气吧,整个页面访问后端接口的时间节省了一半!

3、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

4、下期预告:(三)php与opcode缓存

建议继续学习:

  1. 一种常见的并发编程场景的处理    (阅读:22620)
  2. Rolling cURL: PHP并发最佳实践    (阅读:10346)
  3. curl 命令使用cookie    (阅读:8759)
  4. 查看 Apache并发请求数及其TCP连接状态    (阅读:8485)
  5. 大型高并发高负载网站的系统架构分析    (阅读:7734)
  6. 大并发下的高性能编程 – 改进的(用户态)自旋锁    (阅读:7167)
  7. curl检查访问网页返回的状态码    (阅读:6587)
  8. 并发编程系列之一:锁的意义    (阅读:5907)
  9. 并发框架Disruptor译文    (阅读:5116)
  10. 学习:一个并发的Cache    (阅读:4988)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1