function curl($ip){
$ch = curl_init('http://'.$ip);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$rs = curl_exec($ch);
curl_close($ch);
return $rs;
}
function co_http($ip){
$cli = new Swoole\Coroutine\Http\Client($ip, 80);
$cli->get('/');
return $cii->body;
}
$server = new swoole_http_server('0.0.0.0',9501);
$server->set([
'daemonize' => true,
'worker_num' => 4,
'backlog' => 128,
'max_request' => 5000,
'dispatch_mode' => 1,
'max_conn' => 65535,
]);
$server->on('request',function(swoole_http_request $request,swoole_http_response $response){
$ip = '151.101.1.69';
co_http($ip); //协程http
#curl($ip); //curl
$response->end('Hello World2');
});
$server->start();
在1g 1cpu 1core 1m/s centos环境的测试
(1)4个worker进程
(2)开启轮询模式
测试性能比较: ab -c 10 -n 100 -k http://120.27.141.163/
curl:
Swoole/coroutine/http/client
测试结果 :
Curl : 6.47/s
Coroutine: 162.77/s