zeromq是啥玩意儿?
通俗地说,ZMQ是一个开源的、跨语言的、非常简洁的、非常高性能、非常灵活的网络通讯库。它的官方网址为http://www.zeromq.org/
它的性能非常高,下面是网友给出的几款常用消息中间件性能比较:
性能测试
我一直很好奇zeromq真的能跑出那么高的性能来吗?从上面网友给出的性能图看来,这个性能简直逆天了。
俗话说好奇害死猫,我就是怀着这个好奇写了一个测试小程序进行验证。
这里写了个echo_server用于把收到的请求数据原封不动的返回给客户端,这样通过一收一发验证它的性能。
写个echo_client端,用于批量给echo_server发大量的请求消息,以压测的方式查看和server之间的传输性能。
具体实现代码如下:
echo_client.cpp
[cpp]
/////////////////////////////////////////////////////////////////////
/// File:
/// echo_client.cpp
/// Description:
/// impliment a echo client with zeromq lib
/// Author:
/// cangyingzhijia@126.com
////////////////////////////////////////////////////////////////////
#include <zmq.hpp>
#include <string>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdio.h>
long long int gettime(){
struct timeval tv = {0, 0};
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000 + tv.tv_usec;
}
int main (int argc, char *argv[]) {
if(argc != 4){
printf("Usage:%s <message_size> <total_count> <server_host>\n", argv[0]);
return -1;
}
int message_size = atoi(argv[1]);
int total_count = atoi(argv[2]);
char *server_host = argv[3];
zmq::context_t context (3);
zmq::socket_t socket (context, ZMQ_REQ);
socket.connect(server_host) ; //tcp连接到server端
std::string data; //构造传输消息
for(int i=0; i<message_size; i++){
data.append("A");
}
int data_size = data.size();
zmq::message_t request((void *)data.c_str(), data_size, NULL);
zmq::message_t reply (data_size);
long long int start = gettime();
for(int i=0; i<total_count; i++) {
request.rebuild((void *)data.c_str(), data_size, NULL);
socket.send (request); //send data to server
reply.rebuild(data_size);
socket.recv (&reply, 0);//recive data from server
}
long long int end = gettime();
long long total_time = end - start;
printf("message_size=%d total_count=%ld total_time=%ldus qps=%lf rt=%lfus band_width=%ldM\n", message_size, total_count, total_time, (double)(total_count/(total_time/1000000)), (double)(total_time/total_count), (data_size*total_count)/(total_time/1000000)/(1024*1024));
return 0;
}
echo_server.cpp
[cpp]
/////////////////////////////////////////////////////////////////////
/// File:
/// echo_server.cpp
/// Description:
/// impliment a echo server with zeromq lib
/// Author:
/// cangyingzhijia@126.com
////////////////////////////////////////////////////////////////////
#include <zmq.hpp>
#include <assert.h>
int main () {
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REP);
socket.bind("tcp://*:5555"); //bind
int data_size = 4096;
char buff[4*1024*1024] = "";
zmq::message_t reply (buff, sizeof(buff), NULL, NULL);
for(;;) {
reply.rebuild(buff, sizeof(buff), NULL, NULL);
assert(socket.recv (&reply, 0)); //recive
assert(socket.send (reply)); //send
}
return 0;
}
编译并执行
编译client端:g++ echo_client.cpp -o echo_client -g -lzmq -O3
编译server端:g++ echo_server.cpp -o echo_server -g -lzmq -O3
启动server端:./echo_server
启动client端:./echo_client 32 50000 192.168.1.11
运行结果
message_size=1 total_count=50000 total_time=6210013us qps=8333.000000 rt=124.000000us band_width=0M
message_size=2 total_count=50000 total_time=6367047us qps=8333.000000 rt=127.000000us band_width=0M
message_size=32 total_count=50000 total_time=6421204us qps=8333.000000 rt=128.000000us band_width=0M
message_size=64 total_count=50000 total_time=6184839us qps=8333.000000 rt=123.000000us band_width=0M
message_size=128 total_count=50000 total_time=6786448us qps=8333.000000 rt=135.000000us band_width=1M
message_size=256 total_count=50000 total_time=7883116us qps=7142.000000 rt=157.000000us band_width=1M
message_size=512 total_count=50000 total_time=7877550us qps=7142.000000 rt=157.000000us band_width=3M
message_size=1024 total_count=50000 total_time=9842729us qps=5555.000000 rt=196.000000us band_width=5M
message_size=2048 total_count=50000 total_time=10940243us qps=5000.000000 rt=218.000000us band_width=9M
message_size=4096 total_count=50000 total_time=13938887us qps=3846.000000 rt=278.000000us band_width=15M
message_size=8192 total_count=50000 total_time=17498888us qps=2941.000000 rt=349.000000us band_width=22M
测试环境为把echo_server、echo_client分别部署在同一局域网的不同的机器上,机器之间用千兆以太网连接,机器配置为内存16G、因特尔至强8核cpu。
从上面的运行结果得到,在单线程的情况下最高可以达到8000多的qps,随着传输的消息大小增加qps有所减小,在消息大小为8k的时候qps为3000左右,此时的
传输带宽为22MB。
在单线程下能达到这个性能确实很不错了。