zeromq - 提高 ZeroMQ REQ/REP 性能

标签 zeromq

我正在使用 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模式。

之所以会更快,是因为如果客户端可以发送连续的请求而不必等待其间的每个响应。处理单个请求/回复的成本有两部分:

  1. “通过网络”发送消息的成本
  2. 在客户端和服务器上处理请求和/或回复的成本

当您运行大量连续请求时,异步模式有助于大大降低 (2) 的成本。

关于zeromq - 提高 ZeroMQ REQ/REP 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17054562/

相关文章:

c++ - 系统托盘图标没有响应

python - ZeroMQ 2 REQ/REP 以允许嵌套对话

node.js - nodejs zmq - 收到的缓冲区数据比实际消息

php - zeromq - 总是丢失第一条消息

c++ - zeroMQ:zmq_recv() 不起作用

c# - 如何使用 ZeroMQ 解决 'NetMQ.AddressAlreadyInUseException'

tcp - 使用 ZeroMQ ZMQ_STREAM 作为 tcp 客户端。为什么我会收到额外的信息?

python - Python 中的 ZMQ - PULL 端进程能否知道 PUSH 端进程是否已关闭?

apache-camel - Apache Camel : connecting to a ZeroMQ server

c - 编译 C 代码时出现异常错误