CURL vs MULTI CURL vs File Get Content benchmark test

测试循环12个URL 类似采集请求的结果

CURL: total 5.11485290527 s
MULTI CURL: total 0.369098901749 s
FILE GET CONTENTS: total 9.39328789711 s

file_get_content函数最慢,CURL次之 在可以忍受的范围类
不得不说下CURL的多线程并发请求 并行请求队列

相当于请求1个url的时间 并不是队列方式的请求

测试脚本

<?php
 
$data = array(
  'http://www.marca.com',
  'http://www.wowebook.be',
  'http://www.meneame.net',
  'http://www.google.com',
  'http://www.elpais.es',
  'http://www.reddit.com',
  'http://www.digg.com',
  'http://www.elmundo.es',
  'http://www.itbooks.com',
  'http://www.as.com',
  'http://www.sport.es',
  'http://www.elmundodeportivo.es'
);

/*******************************************************/
/************************* file_get_contents ************************/
/*******************************************************/
/*
$first = microtime(true);
foreach ($data as $url1) {
	$file = file_get_contents($url1);
    //$ch = curl_init($url);
	//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	//$content[] = curl_exec($ch);
	//curl_close($ch);
}
$total = microtime(true) - $first;
echo "File GET Content: total " . $total . " s";
*/
echo "<br>";

/*******************************************************/
/************************* CURL ************************/
/*******************************************************/
$first = microtime(true);
foreach ($data as $url) {
    $ch = curl_init($url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$content[] = curl_exec($ch);
	curl_close($ch);
}
$total = microtime(true) - $first;
echo "CURL: total " . $total . " s";
echo "<br>";

/*******************************************************/
/********************** MULTI CURL *********************/
/*******************************************************/
$first = microtime(true);
 
// array of curl handles
$curly = array();
// data to be returned
$result = array();
// multi handle
$mh = curl_multi_init();
 
foreach ($data as $id => $d) {
 
	$curly[$id] = curl_init();
 
	curl_setopt($curly[$id], CURLOPT_URL,            $url);
	curl_setopt($curly[$id], CURLOPT_HEADER,         0);
	curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
 
	if (is_array($d)) {
	  if (!empty($d['post'])) {
	    curl_setopt($curly[$id], CURLOPT_POST,       1);
	    curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
	  }
	}
 
	curl_multi_add_handle($mh, $curly[$id]);
}
 
// execute the handles
$running = null;
do {
	curl_multi_exec($mh, $running);
} while($running > 0);
 
 
// get content and remove handles
foreach($curly as $id => $c) {
	$result[$id] = curl_multi_getcontent($c);
	curl_multi_remove_handle($mh, $c);
}
 
curl_multi_close($mh);
 
$total = microtime(true) - $first;
echo "MULTI CURL: total " . $total . ' s';
 
echo "<br>";
 
/*******************************************************/
/****************** FILE GET CONTENTS ******************/
/*******************************************************/
$first = microtime(true);
foreach ($data as $url) {
	$content[] = file_get_contents($url);
}
$total = microtime(true) - $first;
echo "FILE GET CONTENTS: total " . $total . " s";

?>

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注