/* For testing ZeroMQ speed: pull N empty messages over 0MQ from a * push server zmqpushn.c, then tell it to exit. * * Times from a single run: * - 1000 messages: 56 ms * - 100000 messages: 132 ms * - 10 000 000 messages: 3878 ms * - 20 000 000 messages: 7805 ms * - 30 000 000 messages: 12252 ms * - 120 000 000 messages: 49090 ms * * So it looks pretty consistently like about 2.5 million messages per second. * * Almost all of this was user time, and all four cores were pegged, * so 0MQ (or the way I’m using it) is the bottleneck here, not the * kernel or system call interface or anything. * */ #include #include #include #include #include #define S(x) #x #define S_(x) S(x) #define LINE S_(__LINE__) #define req(x) do { if (!(x)) { perror(__FILE__ ":" LINE ": " #x); abort(); } } while (0) int main(int argc, char **argv) { int n = atoi(argv[1]); void *context = zmq_ctx_new(); req(context); void *quit_socket = zmq_socket(context, ZMQ_REQ); req(quit_socket); req(0 == zmq_bind(quit_socket, "tcp://127.0.0.1:2821")); void *pull_socket = zmq_socket(context, ZMQ_PULL); req(pull_socket); req(0 == zmq_connect(pull_socket, "tcp://127.0.0.1:2822")); for (int i = 0; i != n; i++) { zmq_msg_t pushmsg; zmq_msg_init(&pushmsg); req(0 <= zmq_msg_recv(&pushmsg, pull_socket, 0)); req(0 == zmq_msg_close(&pushmsg)); if (0 == (i & 0xfffff)) { printf("Got %d messages...\n", i); } } printf("Received %d messages, sending exit command.\n", n); char *msg = "Rest."; zmq_msg_t request; req(0 == zmq_msg_init_size(&request, strlen(msg))); memcpy(zmq_msg_data(&request), msg, strlen(msg)); req(0 <= zmq_msg_send(&request, quit_socket, 0)); req(0 == zmq_msg_close(&request)); zmq_msg_t reply; zmq_msg_init(&reply); req(0 <= zmq_msg_recv(&reply, quit_socket, 0)); printf("Got response: "); fwrite(zmq_msg_data(&reply), zmq_msg_size(&reply), 1, stdout); printf("\n"); req(0 == zmq_msg_close(&reply)); struct timespec ten_milliseconds = { 0, 10*1000*1000 + 76 }; nanosleep(&ten_milliseconds, NULL); // This hangs: req(0 == zmq_ctx_destroy(context)); return 0; }