本文主要与前面的Node.js做压测对比,关于扩展安装可以看下面链接。
【Node.JS创建RabbitMQ生产者】https://www.jianshu.com/p/5a498e5134ea
swoole扩展安装:https://wiki.swoole.com/wiki/page/6.html
RabbitMQ的PHP扩展安装:http://blog.csdn.net/qq_30502699/article/details/78007991
PHP-RabbitMQ系列教程:http://blog.csdn.net/demon3182/article/details/77335123
'127.0.0.1',
'port' => '5672',
'login' => 'admin',
'password' => 'admin',
'vhost'=>'/'
);
// 队列自己先创建好 指定路由key
$e_name = 'php_e'; //交换机名
$k_route = 'php_key'; //路由key
//创建连接和channel
$conn = new AMQPConnection($conn_args);
if (!$conn->connect()) {
die("Cannot connect to the broker!\n");
}
$channel = new AMQPChannel($conn);
//创建交换机对象
$ex = new AMQPExchange($channel);
$ex->setName($e_name);
date_default_timezone_set("Asia/Shanghai");
$http = new swoole_http_server("0.0.0.0", 8008);
$http->on('request', function ($request, $response) use($ex,$k_route)
{
$ex->publish(json_encode($request->get), $k_route);
$response->end(json_encode($request->get));
});
$http->start();
composer安装php-amqplib的话用起来简单些(详细参考上面系列教程链接)
channel();
$q_name = 'amqplib_test';
$channel->queue_declare($q_name, false, false, false, false);
$http = new swoole_http_server("0.0.0.0", 8008);
$http->on('request', function ($request, $response) use($channel,$q_name)
{
$msg = new AMQPMessage(json_encode($request->get));
$channel->basic_publish($msg, '', $q_name);
$response->end("Hello Swoole-MQ.
");
});
$http->start();
下面是第一份代码的压测截图
ab -n 30000 -c 600 http://127.0.0.1:8008/?a=6
ab -n 200000 -c 1000 http://127.0.0.1:8008/?a=6
ab -n 500000 -c 2000 http://127.0.0.1:8008/?a=6