接口1: php -S 127.0.0.1:8080 -t /home/eechen/www
接口2: php -S 127.0.0.2:8080 -t /home/eechen/www
/home/eechen/www/index.php:
<?php
header('Content-Type: application/json; charset=utf-8');
echo json_encode(array('SERVER_NAME' => $_SERVER['SERVER_NAME']));
//串行访问需要sum(2,1)秒,并行访问需要max(2,1)秒.
($_SERVER['SERVER_NAME'] == '127.0.0.1') ? sleep(2) : sleep(1);
串行和并行对比:
串行:
<?php
$ch1 = curl_init();
$ch2 = curl_init();
curl_setopt($ch1, CURLOPT_URL, 'http://127.0.0.1:8080/');
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_URL, 'http://127.0.0.2:8080/');
curl_setopt($ch2, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
$starttime = microtime(true);
$json[] = curl_exec($ch1);
$json[] = curl_exec($ch2);
echo microtime(true) - $starttime."\n"; //耗时约3秒
curl_close($ch1);
curl_close($ch2);
var_export($json);
//输出:
3.002946138382
array (
0 => '{"SERVER_NAME":"127.0.0.1"}',
1 => '{"SERVER_NAME":"127.0.0.2"}',
)
并行:
<?php
$url[] = 'http://127.0.0.1:8080';
$url[] = 'http://127.0.0.2:8080';
$mh = curl_multi_init();
foreach($url as $k => $v) {
$ch[$k] = curl_init($v);
curl_setopt($ch[$k], CURLOPT_HEADER, 0); //不输出头
curl_setopt($ch[$k], CURLOPT_RETURNTRANSFER, 1); //exec返回结果而不是输出,用于赋值
curl_multi_add_handle($mh, $ch[$k]); //决定exec输出顺序
}
$running = null;
$starttime = microtime(true);
//执行批处理句柄(类似pthreads多线程里的start开始和join同步)
do {
//CURLOPT_RETURNTRANSFER如果为0,这里会直接输出获取到的内容.如果为1,后面可以用curl_multi_getcontent获取内容.
curl_multi_exec($mh, $running);
//阻塞直到cURL批处理连接中有活动连接,不加这个会导致CPU负载超过90%.
curl_multi_select($mh);
} while ($running > 0);
echo microtime(true) - $starttime."\n"; //耗时约2秒
foreach($ch as $v) {
$info[] = curl_getinfo($v);
$json[] = curl_multi_getcontent($v);
curl_multi_remove_handle($mh, $v);
}
curl_multi_close($mh);
var_export($json);
var_export($info);
//输出:
2.0015449523926
array (
0 => '{"SERVER_NAME":"127.0.0.1"}',
1 => '{"SERVER_NAME":"127.0.0.2"}',
)
array (
0 =>
array (
'url' => 'http://127.0.0.1:8080/',
'content_type' => 'application/json; charset=utf-8',
'http_code' => 200,
'header_size' => 107,
'request_size' => 53,
'filetime' => -1,
'ssl_verify_result' => 0,
'redirect_count' => 0,
'total_time' => 2.0013990000000002,
'namelookup_time' => 5.3999999999999998E-5,
'connect_time' => 0.00015799999999999999,
'pretransfer_time' => 0.000194,
'size_upload' => 0,
'size_download' => 27,
'speed_download' => 13,
'speed_upload' => 0,
'download_content_length' => -1,
'upload_content_length' => 0,
'starttransfer_time' => 0.00079699999999999997,
'redirect_time' => 0,
'certinfo' =>
array (
),
'primary_ip' => '127.0.0.1',
'primary_port' => 8080,
'local_ip' => '127.0.0.1',
'local_port' => 57653,
'redirect_url' => '',
),
1 =>
array (
'url' => 'http://127.0.0.2:8080/',
'content_type' => 'application/json; charset=utf-8',
'http_code' => 200,
'header_size' => 107,
'request_size' => 53,
'filetime' => -1,
'ssl_verify_result' => 0,
'redirect_count' => 0,
'total_time' => 1.0012369999999999,
'namelookup_time' => 1.1E-5,
'connect_time' => 4.6999999999999997E-5,
'pretransfer_time' => 6.3E-5,
'size_upload' => 0,
'size_download' => 27,
'speed_download' => 26,
'speed_upload' => 0,
'download_content_length' => -1,
'upload_content_length' => 0,
'starttransfer_time' => 0.00063699999999999998,
'redirect_time' => 0,
'certinfo' =>
array (
),
'primary_ip' => '127.0.0.2',
'primary_port' => 8080,
'local_ip' => '127.0.0.1',
'local_port' => 43645,
'redirect_url' => '',
),
)