我正在使用 ZeroMQ 3.2.3 和 CZmq 1.4.1。我尝试了“Hello world”样本。该示例 ( https://github.com/imatix/zguide/tree/master/examples/C ) 在使用 10 个并发客户端时,允许我在本地主机 (Ubuntu 13.04) 上的 Intel i7(8 GB RAM,总共 8 个内核)上每秒最多交换 12500 条消息。
我读到 ZeroMQ 可以处理更多。我是做错了什么,还是遗漏了什么?
这里是示例代码:
// Hello World server
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
int main (void)
{
// Socket to talk to clients
void *context = zmq_ctx_new ();
void *responder = zmq_socket (context, ZMQ_REP);
int rc = zmq_bind (responder, "tcp://*:5555");
assert (rc == 0);
while (1) {
char buffer [10];
zmq_recv (responder, buffer, 10, 0);
//printf ("Received Hello\n");
zmq_send (responder, "World", 5, 0);
//usleep (1); // Do some 'work'
}
return 0;
}
// Hello World client
#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
int main (void)
{
printf ("Connecting to hello world server...\n");
void *context = zmq_ctx_new ();
void *requester = zmq_socket (context, ZMQ_REQ);
zmq_connect (requester, "tcp://localhost:5555");
int request_nbr;
for (request_nbr = 0; request_nbr != 100000; request_nbr++) {
char buffer [10];
// printf ("Sending Hello %d...\n", request_nbr);
zmq_send (requester, "Hello", 5, 0);
zmq_recv (requester, buffer, 10, 0);
// printf ("Received World %d\n", request_nbr);
}
zmq_close (requester);
zmq_ctx_destroy (context);
return 0;
}
谢谢!
最佳答案
您遇到的瓶颈是因为您没有异步简化您的通信。
尝试替换您的同步 REQ <-> REP
异步模式 ROUTER <-> DEALER
模式。
之所以会更快,是因为如果客户端可以发送连续的请求而不必等待其间的每个响应。处理单个请求/回复的成本有两部分:
- “通过网络”发送消息的成本
- 在客户端和服务器上处理请求和/或回复的成本
当您运行大量连续请求时,异步模式有助于大大降低 (2) 的成本。
关于zeromq - 提高 ZeroMQ REQ/REP 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17054562/