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: /// [email protected] //////////////////////////////////////////////////////////////////// #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: /// [email protected] //////////////////////////////////////////////////////////////////// #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。 在單線程下能達到這個性能確實很不錯了。