int main(int argc, char* argv[]) { void* context = zmq_ctx_new(); void* sink = zmq_socket(context, ZMQ_PULL); zmq_bind(sink, "tcp://*:5558"); char buf[10]; // Wait for start of batch zmq_recv(sink, buf, 10, 0); // Timing void* stopwatch = zmq_stopwatch_start(); // Process 100 confirmations for (int t = 0; t < 100; ++t) { zmq_recv(sink, buf, 10, 0); if (t % 10 == 0) printf(":"); else printf("."); fflush(stdout); } unsigned long elapsed_micros = zmq_stopwatch_stop(stopwatch); // Calc and report printf("Total elapsed time: %.2f ms\n", elapsed_micros*1.0/1000); zmq_close(sink); zmq_ctx_destroy(context); return 0; }
int main(){ zmq::context_t context(1); zmq::socket_t client (context, ZMQ_PUSH); client.setsockopt( ZMQ_IDENTITY, "B", 1); client.connect("tcp://127.0.0.1:5560"); std::cout << "connected!" << std::endl; sleep(1); void *watch; unsigned long elapsed; unsigned long throughput; int message_count = 1; watch = zmq_stopwatch_start (); /*for( int i = 0; i < message_count; i++){ //s_sendmore (client, "A"); //s_sendmore(client, ""); s_send (client, "This is the workload"); //s_dump(client); //std::string string = s_recv (client); //zmq::message_t message; //client.recv(&message); }*/ for (i = 0; i != message_count; i++) { rc = zmq_msg_init_size (&msg, message_size); if (rc != 0) { printf ("error in zmq_msg_init_size: %s\n", zmq_strerror (errno)); return -1; } #if defined ZMQ_MAKE_VALGRIND_HAPPY memset (zmq_msg_data (&msg), 0, message_size); #endif rc = zmq_sendmsg (s, &msg, 0); if (rc < 0) { printf ("error in zmq_sendmsg: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_msg_close (&msg); if (rc != 0) { printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno)); return -1; } } elapsed = zmq_stopwatch_stop (watch); throughput = (unsigned long) ((double) message_count / (double) elapsed * 1000000); printf ("mean throughput: %d [msg/s]\n", (int) throughput); sleep(1); return 0; }
void wait_timer_events (test_events_t &events) { void *watch = zmq_stopwatch_start (); while (events.timer_events.get () < 1) { #ifdef ZMQ_BUILD_DRAFT TEST_ASSERT_LESS_OR_EQUAL_MESSAGE (SETTLE_TIME, zmq_stopwatch_intermediate (watch), "Timeout waiting for timer event"); #endif } zmq_stopwatch_stop (watch); }
int main(){ const char *connect_to; void *ctx; void *s; int rc; void *watch; double elapsed; int n = 100000; connect_to = "tcp://127.0.0.1:5560"; ctx = zmq_init (1); s = zmq_socket (ctx, ZMQ_ROUTER); zmq_setsockopt(s, ZMQ_IDENTITY, "R", 1); zmq_bind(s, connect_to); printf("Bound!\n"); sleep(3); for(int i=0; i<n; i++){ if(i == 0) watch = zmq_stopwatch_start (); zmq_msg_t request; zmq_msg_init (&request); zmq_recv (s, &request, 0); //printf ("Received Hello\n"); zmq_msg_close (&request); } elapsed = (double) zmq_stopwatch_stop (watch) / 1000000; double throughput = ((double) n / (double) elapsed ) / 1000000; printf ("elapsed time: %.3f [s]\n", elapsed); printf ("mean throughput: %.3fM [msg/s]\n", throughput); zmq_close (s); rc = zmq_term (ctx); }
int main (void) { setup_test_environment (); void *timers = zmq_timers_new (); assert (timers); bool timer_invoked = false; const unsigned long full_timeout = 100; void *const stopwatch = zmq_stopwatch_start (); int timer_id = zmq_timers_add (timers, full_timeout, handler, &timer_invoked); assert (timer_id); // Timer should not have been invoked yet int rc = zmq_timers_execute (timers); assert (rc == 0); #ifdef ZMQ_BUILD_DRAFT_API if (zmq_stopwatch_intermediate (stopwatch) < full_timeout) { assert (!timer_invoked); } #endif // Wait half the time and check again long timeout = zmq_timers_timeout (timers); assert (rc != -1); msleep (timeout / 2); rc = zmq_timers_execute (timers); assert (rc == 0); #ifdef ZMQ_BUILD_DRAFT_API if (zmq_stopwatch_intermediate (stopwatch) < full_timeout) { assert (!timer_invoked); } #endif // Wait until the end rc = sleep_and_execute (timers); assert (rc == 0); assert (timer_invoked); timer_invoked = false; // Wait half the time and check again timeout = zmq_timers_timeout (timers); assert (rc != -1); msleep (timeout / 2); rc = zmq_timers_execute (timers); assert (rc == 0); #ifdef ZMQ_BUILD_DRAFT_API if (zmq_stopwatch_intermediate (stopwatch) < 2 * full_timeout) { assert (!timer_invoked); } #endif // Reset timer and wait half of the time left rc = zmq_timers_reset (timers, timer_id); assert (rc == 0); msleep (timeout / 2); rc = zmq_timers_execute (timers); assert (rc == 0); if (zmq_stopwatch_stop (stopwatch) < 2 * full_timeout) { assert (!timer_invoked); } // Wait until the end rc = sleep_and_execute (timers); assert (rc == 0); assert (timer_invoked); timer_invoked = false; // reschedule rc = zmq_timers_set_interval (timers, timer_id, 50); assert (rc == 0); rc = sleep_and_execute (timers); assert (rc == 0); assert (timer_invoked); timer_invoked = false; // cancel timer timeout = zmq_timers_timeout (timers); assert (rc != -1); rc = zmq_timers_cancel (timers, timer_id); assert (rc == 0); msleep (timeout * 2); rc = zmq_timers_execute (timers); assert (rc == 0); assert (!timer_invoked); rc = zmq_timers_destroy (&timers); assert (rc == 0); test_null_timer_pointers (); test_corner_cases (); return 0; }
int main (int argc, char *argv []) { const char *bind_to; int message_count; size_t message_size; void *ctx; void *s; int rc; int i; zmq_msg_t msg; void *watch; unsigned long elapsed; unsigned long throughput; double megabits; if (argc != 4) { printf ("usage: local_thr <bind-to> <message-size> <message-count>\n"); return 1; } bind_to = argv [1]; message_size = atoi (argv [2]); message_count = atoi (argv [3]); ctx = zmq_init (1); if (!ctx) { printf ("error in zmq_init: %s\n", zmq_strerror (errno)); return -1; } s = zmq_socket (ctx, ZMQ_PULL); if (!s) { printf ("error in zmq_socket: %s\n", zmq_strerror (errno)); return -1; } // Add your socket options here. // For example ZMQ_RATE, ZMQ_RECOVERY_IVL and ZMQ_MCAST_LOOP for PGM. rc = zmq_bind (s, bind_to); if (rc != 0) { printf ("error in zmq_bind: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_msg_init (&msg); if (rc != 0) { printf ("error in zmq_msg_init: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_recvmsg (s, &msg, 0); if (rc < 0) { printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno)); return -1; } if (zmq_msg_size (&msg) != message_size) { printf ("message of incorrect size received\n"); return -1; } watch = zmq_stopwatch_start (); for (i = 0; i != message_count - 1; i++) { rc = zmq_recvmsg (s, &msg, 0); if (rc < 0) { printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno)); return -1; } if (zmq_msg_size (&msg) != message_size) { printf ("message of incorrect size received\n"); return -1; } } elapsed = zmq_stopwatch_stop (watch); if (elapsed == 0) elapsed = 1; rc = zmq_msg_close (&msg); if (rc != 0) { printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno)); return -1; } throughput = (unsigned long) ((double) message_count / (double) elapsed * 1000000); megabits = (double) (throughput * message_size * 8) / 1000000; printf ("message size: %d [B]\n", (int) message_size); printf ("message count: %d\n", (int) message_count); printf ("mean throughput: %d [msg/s]\n", (int) throughput); printf ("mean throughput: %.3f [Mb/s]\n", (double) megabits); rc = zmq_close (s); if (rc != 0) { printf ("error in zmq_close: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_term (ctx); if (rc != 0) { printf ("error in zmq_term: %s\n", zmq_strerror (errno)); return -1; } return 0; }
int main (int argc, char *argv []) { #if defined ZMQ_HAVE_WINDOWS HANDLE local_thread; #else pthread_t local_thread; #endif void *ctx; void *s; int rc; int i; zmq_msg_t msg; void *watch; unsigned long elapsed; double latency; if (argc != 3) { printf ("usage: inproc_lat <message-size> <roundtrip-count>\n"); return 1; } message_size = atoi (argv [1]); roundtrip_count = atoi (argv [2]); ctx = zmq_init (1); if (!ctx) { printf ("error in zmq_init: %s\n", zmq_strerror (errno)); return -1; } s = zmq_socket (ctx, ZMQ_REQ); if (!s) { printf ("error in zmq_socket: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_bind (s, "inproc://lat_test"); if (rc != 0) { printf ("error in zmq_bind: %s\n", zmq_strerror (errno)); return -1; } #if defined ZMQ_HAVE_WINDOWS local_thread = (HANDLE) _beginthreadex (NULL, 0, worker, ctx, 0 , NULL); if (local_thread == 0) { printf ("error in _beginthreadex\n"); return -1; } #else rc = pthread_create (&local_thread, NULL, worker, ctx); if (rc != 0) { printf ("error in pthread_create: %s\n", zmq_strerror (rc)); return -1; } #endif rc = zmq_msg_init_size (&msg, message_size); if (rc != 0) { printf ("error in zmq_msg_init_size: %s\n", zmq_strerror (errno)); return -1; } memset (zmq_msg_data (&msg), 0, message_size); printf ("message size: %d [B]\n", (int) message_size); printf ("roundtrip count: %d\n", (int) roundtrip_count); watch = zmq_stopwatch_start (); for (i = 0; i != roundtrip_count; i++) { rc = zmq_sendmsg (s, &msg, 0); if (rc < 0) { printf ("error in zmq_sendmsg: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_recvmsg (s, &msg, 0); if (rc < 0) { printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno)); return -1; } if (zmq_msg_size (&msg) != message_size) { printf ("message of incorrect size received\n"); return -1; } } elapsed = zmq_stopwatch_stop (watch); rc = zmq_msg_close (&msg); if (rc != 0) { printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno)); return -1; } latency = (double) elapsed / (roundtrip_count * 2); #if defined ZMQ_HAVE_WINDOWS DWORD rc2 = WaitForSingleObject (local_thread, INFINITE); if (rc2 == WAIT_FAILED) { printf ("error in WaitForSingleObject\n"); return -1; } BOOL rc3 = CloseHandle (local_thread); if (rc3 == 0) { printf ("error in CloseHandle\n"); return -1; } #else rc = pthread_join (local_thread, NULL); if (rc != 0) { printf ("error in pthread_join: %s\n", zmq_strerror (rc)); return -1; } #endif printf ("average latency: %.3f [us]\n", (double) latency); rc = zmq_close (s); if (rc != 0) { printf ("error in zmq_close: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_term (ctx); if (rc != 0) { printf ("error in zmq_term: %s\n", zmq_strerror (errno)); return -1; } return 0; }
int main (int argc, char *argv[]) { if (argc != 3) { printf ("usage: inproc_thr <message-size> <message-count>\n"); return 1; } message_size = atoi (argv[1]); message_count = atoi (argv[2]); printf ("message size: %d [B]\n", (int) message_size); printf ("message count: %d\n", (int) message_count); void *context = zmq_ctx_new (); assert (context); int rv = zmq_ctx_set (context, ZMQ_IO_THREADS, 4); assert (rv == 0); // START ALL SECONDARY THREADS const char *pub1 = "inproc://perf_pub1"; const char *pub2 = "inproc://perf_pub2"; const char *sub1 = "inproc://perf_backend"; proxy_hwm_cfg_t cfg_global = {}; cfg_global.context = context; cfg_global.frontend_endpoint[0] = pub1; cfg_global.frontend_endpoint[1] = pub2; cfg_global.backend_endpoint[0] = sub1; cfg_global.control_endpoint = "inproc://ctrl"; // Proxy proxy_hwm_cfg_t cfg_proxy = cfg_global; void *proxy = zmq_threadstart (&proxy_thread_main, (void *) &cfg_proxy); assert (proxy != 0); // Subscriber 1 proxy_hwm_cfg_t cfg_sub1 = cfg_global; cfg_sub1.thread_idx = 0; void *subscriber = zmq_threadstart (&subscriber_thread_main, (void *) &cfg_sub1); assert (subscriber != 0); // Start measuring void *watch = zmq_stopwatch_start (); // Publisher 1 proxy_hwm_cfg_t cfg_pub1 = cfg_global; cfg_pub1.thread_idx = 0; void *publisher1 = zmq_threadstart (&publisher_thread_main, (void *) &cfg_pub1); assert (publisher1 != 0); // Publisher 2 proxy_hwm_cfg_t cfg_pub2 = cfg_global; cfg_pub2.thread_idx = 1; void *publisher2 = zmq_threadstart (&publisher_thread_main, (void *) &cfg_pub2); assert (publisher2 != 0); // Wait for all packets to be received zmq_threadclose (subscriber); // Stop measuring unsigned long elapsed = zmq_stopwatch_stop (watch); if (elapsed == 0) elapsed = 1; unsigned long throughput = (unsigned long) ((double) message_count / (double) elapsed * 1000000); double megabits = (double) (throughput * message_size * 8) / 1000000; printf ("mean throughput: %d [msg/s]\n", (int) throughput); printf ("mean throughput: %.3f [Mb/s]\n", (double) megabits); // Wait for the end of publishers... zmq_threadclose (publisher1); zmq_threadclose (publisher2); // ... then close the proxy terminate_proxy (&cfg_proxy); zmq_threadclose (proxy); int rc = zmq_ctx_term (context); assert (rc == 0); return 0; }
int main(int argc, char* argv[]) { const char* bind_to; int message_count; size_t message_size; void* ctx; void* s; int rc; int i; zmq_msg_t msg; void* watch; unsigned long elapsed; unsigned long throughput; double megabits; if (argc != 4) { printf("usage: local_thr <bind-to> <message-size> <message-count>\n"); return 1; } bind_to = argv[1]; message_size = atoi(argv[2]); message_count = atoi(argv[3]); ctx = zmq_init(1); if (!ctx) { printf("error in zmq_init: %s\n", zmq_strerror(errno)); return -1; } s = zmq_socket(ctx, ZMQ_PULL); if (!s) { printf("error in zmq_socket: %s\n", zmq_strerror(errno)); return -1; } rc = zmq_bind(s, bind_to); if (rc != 0) { printf("error in zmq_bind: %s\n", zmq_strerror(errno)); return -1; } rc = zmq_msg_init(&msg); if (rc != 0) { printf("error in zmq_msg_init: %s\n", zmq_strerror(errno)); return -1; } rc = zmq_recvmsg(s, &msg, 0); if (rc < 0) { printf("error in zmq_recvmsg: %s\n", zmq_strerror(errno)); return -1; } if (zmq_msg_size(&msg) != message_size) { printf("message of incorrect size received\n"); return -1; } zmq_pollitem_t items[] = {{s, 0, ZMQ_POLLIN, 0}}; watch = zmq_stopwatch_start(); for (i = 0; i != message_count - 1; ++i) { zmq_poll(items, 1, -1); if ((items[0].revents & ZMQ_POLLIN) == ZMQ_POLLIN) { if (zmq_msg_recv(&msg, s, 0) < 0) { printf("error in zmq_msg_recv: %s\n", zmq_strerror(errno)); return -1; } if (zmq_msg_size(&msg) != message_size) { printf("message of incorrect size received\n"); return -1; } } } elapsed = zmq_stopwatch_stop(watch); if (elapsed == 0) elapsed = 1; rc = zmq_msg_close(&msg); if (rc != 0) { printf("error in zmq_msg_close: %s\n", zmq_strerror(errno)); return -1; } throughput = (unsigned long)((double)message_count / (double)elapsed * 1000000); megabits = (double)(throughput * message_size * 8) / 1000000; printf("message size: %d [B]\n", (int)message_size); printf("message count: %d\n", (int)message_count); printf("mean throughput: %d [msg/s]\n", (int)throughput); printf("mean throughput: %.3f [Mb/s]\n", (double)megabits); rc = zmq_close(s); if (rc != 0) { printf("error in zmq_close: %s\n", zmq_strerror(errno)); return -1; } rc = zmq_term(ctx); if (rc != 0) { printf("error in zmq_term: %s\n", zmq_strerror(errno)); return -1; } return 0; }
int main (int argc, char *argv []) { const char *connect_to; int roundtrip_count; size_t message_size; void *ctx; void *s; int rc; int i; zmq_msg_t msg; void *watch; unsigned long elapsed; double latency; if (argc != 4) { printf ("usage: remote_lat <connect-to> <message-size> " "<roundtrip-count>\n"); return 1; } connect_to = argv [1]; message_size = atoi (argv [2]); roundtrip_count = atoi (argv [3]); ctx = zmq_init (1, 1, 0); if (!ctx) { printf ("error in zmq_init: %s\n", zmq_strerror (errno)); return -1; } s = zmq_socket (ctx, ZMQ_REQ); if (!s) { printf ("error in zmq_socket: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_connect (s, connect_to); if (rc != 0) { printf ("error in zmq_connect: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_msg_init_size (&msg, message_size); if (rc != 0) { printf ("error in zmq_msg_init_size: %s\n", zmq_strerror (errno)); return -1; } memset (zmq_msg_data (&msg), 0, message_size); watch = zmq_stopwatch_start (); for (i = 0; i != roundtrip_count; i++) { rc = zmq_send (s, &msg, 0); if (rc != 0) { printf ("error in zmq_send: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_recv (s, &msg, 0); if (rc != 0) { printf ("error in zmq_recv: %s\n", zmq_strerror (errno)); return -1; } if (zmq_msg_size (&msg) != message_size) { printf ("message of incorrect size received\n"); return -1; } } elapsed = zmq_stopwatch_stop (watch); rc = zmq_msg_close (&msg); if (rc != 0) { printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno)); return -1; } latency = (double) elapsed / (roundtrip_count * 2); printf ("message size: %d [B]\n", (int) message_size); printf ("roundtrip count: %d\n", (int) roundtrip_count); printf ("average latency: %.3f [us]\n", (double) latency); rc = zmq_close (s); if (rc != 0) { printf ("error in zmq_close: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_term (ctx); if (rc != 0) { printf ("error in zmq_term: %s\n", zmq_strerror (errno)); return -1; } return 0; }
void rpc_handle(rpc_io *rpcio, zmq_msg_t *request) { /* Parse the msgpack */ zmq_msg_t response; int rc; msgpack_unpacked request_msg; msgpack_unpacked_init(&request_msg); rc = msgpack_unpack_next(&request_msg, zmq_msg_data(request), zmq_msg_size(request), NULL); insist_return(rc, (void)(0), "Failed to unpack message '%.*s'", zmq_msg_size(request), zmq_msg_data(request)); msgpack_object request_obj = request_msg.data; printf("Object: "); msgpack_object_print(stdout, request_obj); /*=> ["Hello", "MessagePack"] */ printf("\n"); /* Find the method name */ char *method = NULL; size_t method_len = -1; rc = obj_get(&request_obj, "request", MSGPACK_OBJECT_RAW, &method, &method_len); msgpack_sbuffer *buffer = msgpack_sbuffer_new(); msgpack_packer *response_msg = msgpack_packer_new(buffer, msgpack_sbuffer_write); printf("Method: %.*s\n", method_len, method); if (rc != 0) { fprintf(stderr, "Message had no 'request' field. Ignoring: "); msgpack_object_print(stderr, request_obj); msgpack_pack_map(response_msg, 2); msgpack_pack_string(response_msg, "error", -1); msgpack_pack_string(response_msg, "Message had no 'request' field", -1); msgpack_pack_string(response_msg, "request", -1); msgpack_pack_object(response_msg, request_obj); } else { /* Found request */ printf("The method is: '%.*s'\n", (int)method_len, method); //msgpack_pack_map(response_msg, 2); //msgpack_pack_string(response_msg, "results", 7); void *clock = zmq_stopwatch_start(); /* TODO(sissel): Use gperf here or allow methods to register themselves */ if (!strncmp("dance", method, method_len)) { api_request_dance(rpcio, &request_obj, response_msg); } else if (!strncmp("restart", method, method_len)) { api_request_restart(rpcio, &request_obj, response_msg); } else if (!strncmp("status", method, method_len)) { api_request_status(rpcio, &request_obj, response_msg); } else if (!strncmp("create", method, method_len)) { api_request_create(rpcio, &request_obj, response_msg); } else { fprintf(stderr, "Invalid request '%.*s' (unknown method): ", method_len, method); msgpack_object_print(stderr, request_obj); msgpack_pack_map(response_msg, 2); msgpack_pack_string(response_msg, "error", -1); msgpack_pack_string(response_msg, "No such method requested", -1); msgpack_pack_string(response_msg, "request", -1); msgpack_pack_object(response_msg, request_obj); } double duration = zmq_stopwatch_stop(clock) / 1000000.; printf("method '%.*s' took %lf seconds\n", (int)method_len, method); //msgpack_pack_string(response_msg, "stats", 5); //msgpack_pack_map(response_msg, 1), //msgpack_pack_string(response_msg, "duration", 8); //msgpack_pack_double(response_msg, duration); } zmq_msg_init_data(&response, buffer->data, buffer->size, free_msgpack_buffer, buffer); zmq_send(rpcio->socket, &response, 0); zmq_msg_close(&response); //msgpack_sbuffer_free(buffer); msgpack_packer_free(response_msg); msgpack_unpacked_destroy(&request_msg); } /* rpc_handle */
int main (int argc, char *argv []) { #if defined ZMQ_HAVE_WINDOWS HANDLE local_thread; #else pthread_t local_thread; #endif void *ctx; void *s; int rc; int i; zmq_msg_t msg; void *watch; unsigned long elapsed; unsigned long throughput; double megabits; if (argc != 3) { printf ("usage: inproc_thr <message-size> <message-count>\n"); return 1; } message_size = atoi (argv [1]); message_count = atoi (argv [2]); ctx = zmq_init (1); if (!ctx) { printf ("error in zmq_init: %s\n", zmq_strerror (errno)); return -1; } s = zmq_socket (ctx, ZMQ_PULL); if (!s) { printf ("error in zmq_socket: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_bind (s, "inproc://thr_test"); if (rc != 0) { printf ("error in zmq_bind: %s\n", zmq_strerror (errno)); return -1; } #if defined ZMQ_HAVE_WINDOWS local_thread = (HANDLE) _beginthreadex (NULL, 0, worker, ctx, 0 , NULL); if (local_thread == 0) { printf ("error in _beginthreadex\n"); return -1; } #else rc = pthread_create (&local_thread, NULL, worker, ctx); if (rc != 0) { printf ("error in pthread_create: %s\n", zmq_strerror (rc)); return -1; } #endif rc = zmq_msg_init (&msg); if (rc != 0) { printf ("error in zmq_msg_init: %s\n", zmq_strerror (errno)); return -1; } printf ("message size: %d [B]\n", (int) message_size); printf ("message count: %d\n", (int) message_count); rc = zmq_recvmsg (s, &msg, 0); if (rc < 0) { printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno)); return -1; } if (zmq_msg_size (&msg) != message_size) { printf ("message of incorrect size received\n"); return -1; } watch = zmq_stopwatch_start (); for (i = 0; i != message_count - 1; i++) { rc = zmq_recvmsg (s, &msg, 0); if (rc < 0) { printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno)); return -1; } if (zmq_msg_size (&msg) != message_size) { printf ("message of incorrect size received\n"); return -1; } } elapsed = zmq_stopwatch_stop (watch); if (elapsed == 0) elapsed = 1; rc = zmq_msg_close (&msg); if (rc != 0) { printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno)); return -1; } #if defined ZMQ_HAVE_WINDOWS DWORD rc2 = WaitForSingleObject (local_thread, INFINITE); if (rc2 == WAIT_FAILED) { printf ("error in WaitForSingleObject\n"); return -1; } BOOL rc3 = CloseHandle (local_thread); if (rc3 == 0) { printf ("error in CloseHandle\n"); return -1; } #else rc = pthread_join (local_thread, NULL); if (rc != 0) { printf ("error in pthread_join: %s\n", zmq_strerror (rc)); return -1; } #endif rc = zmq_close (s); if (rc != 0) { printf ("error in zmq_close: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_ctx_term (ctx); if (rc != 0) { printf ("error in zmq_ctx_term: %s\n", zmq_strerror (errno)); return -1; } throughput = (unsigned long) ((double) message_count / (double) elapsed * 1000000); megabits = (double) (throughput * message_size * 8) / 1000000; printf ("mean throughput: %d [msg/s]\n", (int) throughput); printf ("mean throughput: %.3f [Mb/s]\n", (double) megabits); return 0; }
void rpc_service_handle(rpc_service_t *service, zmq_msg_t *request) { /* Parse the msgpack */ zmq_msg_t response; int rc; msgpack_unpacked request_msg; msgpack_unpacked_init(&request_msg); rc = msgpack_unpack_next(&request_msg, zmq_msg_data(request), zmq_msg_size(request), NULL); insist_return(rc, (void)(0), "Failed to unpack message '%.*s'", (int)zmq_msg_size(request), (char *)zmq_msg_data(request)); msgpack_object request_obj = request_msg.data; /* Find the method name */ char *method = NULL; size_t method_len = -1; rc = obj_get(&request_obj, "method", MSGPACK_OBJECT_RAW, &method, &method_len); msgpack_sbuffer *response_buffer = msgpack_sbuffer_new(); msgpack_sbuffer *result_buffer = msgpack_sbuffer_new(); msgpack_sbuffer *error_buffer = msgpack_sbuffer_new(); msgpack_packer *response_msg = msgpack_packer_new(response_buffer, msgpack_sbuffer_write); msgpack_packer *result = msgpack_packer_new(result_buffer, msgpack_sbuffer_write); msgpack_packer *error = msgpack_packer_new(error_buffer, msgpack_sbuffer_write); //printf("Method: %.*s\n", method_len, method); void *clock = zmq_stopwatch_start(); double duration; if (rc != 0) { /* method not found */ msgpack_pack_nil(result); /* result is nil on error */ msgpack_pack_map(error, 2); msgpack_pack_string(error, "error", -1); msgpack_pack_string(error, "Message had no 'method' field", -1); msgpack_pack_string(error, "request", -1); msgpack_pack_object(error, request_obj); } else { /* valid method, keep going */ //printf("The method is: '%.*s'\n", (int)method_len, method); rpc_name name; name.name = method; name.len = method_len; rpc_method *rpcmethod = g_tree_lookup(service->methods, &name); /* if we found a valid rpc method and the args check passed ... */ if (rpcmethod != NULL) { /* the callback is responsible for filling in the 'result' and 'error' * objects. */ rpcmethod->callback(NULL, &request_obj, result, error, rpcmethod->data); } else { msgpack_pack_nil(result); /* result is nil on error */ /* TODO(sissel): allow methods to register themselves */ //fprintf(stderr, "Invalid request '%.*s' (unknown method): ", //method_len, method); //msgpack_object_print(stderr, request_obj); //fprintf(stderr, "\n"); msgpack_pack_map(error, 2); msgpack_pack_string(error, "error", -1); msgpack_pack_string(error, "No such method requested", -1); msgpack_pack_string(error, "request", -1); msgpack_pack_object(error, request_obj); } } /* valid/invalid method handling */ duration = zmq_stopwatch_stop(clock) / 1000000.; //printf("method '%.*s' took %lf seconds\n", (int)method_len, method); msgpack_unpacked result_unpacked; msgpack_unpacked error_unpacked; msgpack_unpacked response_unpacked; msgpack_unpacked_init(&result_unpacked); msgpack_unpacked_init(&error_unpacked); msgpack_unpacked_init(&response_unpacked); /* TODO(sissel): If this unpack test fails, we should return an error to the calling * client indicating that some internal error has occurred */ //fprintf(stderr, "Result payload: '%.*s'\n", result_buffer->size, //result_buffer->data); rc = msgpack_unpack_next(&result_unpacked, result_buffer->data, result_buffer->size, NULL); insist(rc == true, "msgpack_unpack_next failed on 'result' buffer" " of request '%.*s'", (int)method_len, method); rc = msgpack_unpack_next(&error_unpacked, error_buffer->data, error_buffer->size, NULL); insist(rc == true, "msgpack_unpack_next failed on 'error' buffer" " of request '%.*s'", (int)method_len, method); msgpack_pack_map(response_msg, 3); /* result, error, duration */ msgpack_pack_string(response_msg, "result", 6); msgpack_pack_object(response_msg, result_unpacked.data); msgpack_pack_string(response_msg, "error", 5); msgpack_pack_object(response_msg, error_unpacked.data); msgpack_pack_string(response_msg, "duration", 8); msgpack_pack_double(response_msg, duration); rc = msgpack_unpack_next(&response_unpacked, response_buffer->data, response_buffer->size, NULL); insist(rc == true, "msgpack_unpack_next failed on full response buffer" " of request '%.*s'", (int)method_len, method); //printf("request: "); //msgpack_object_print(stdout, request_obj); //printf("\n"); //printf("response: "); //msgpack_object_print(stdout, response_unpacked.data); //printf("\n"); zmq_msg_init_data(&response, response_buffer->data, response_buffer->size, free_msgpack_buffer, response_buffer); zmq_send(service->socket, &response, 0); zmq_msg_close(&response); msgpack_packer_free(error); msgpack_packer_free(result); msgpack_sbuffer_free(error_buffer); msgpack_sbuffer_free(result_buffer); msgpack_packer_free(response_msg); msgpack_unpacked_destroy(&request_msg); } /* rpc_service_handle */