libzmq master
The Intelligent Transport Layer

remote_lat.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2009-2011 250bpm s.r.o.
00003     Copyright (c) 2007-2009 iMatix Corporation
00004     Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
00005 
00006     This file is part of 0MQ.
00007 
00008     0MQ is free software; you can redistribute it and/or modify it under
00009     the terms of the GNU Lesser General Public License as published by
00010     the Free Software Foundation; either version 3 of the License, or
00011     (at your option) any later version.
00012 
00013     0MQ is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU Lesser General Public License for more details.
00017 
00018     You should have received a copy of the GNU Lesser General Public License
00019     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 
00022 #include "../include/zmq.h"
00023 #include "../include/zmq_utils.h"
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 
00028 int main (int argc, char *argv [])
00029 {
00030     const char *connect_to;
00031     int roundtrip_count;
00032     size_t message_size;
00033     void *ctx;
00034     void *s;
00035     int rc;
00036     int i;
00037     zmq_msg_t msg;
00038     void *watch;
00039     unsigned long elapsed;
00040     double latency;
00041 
00042     if (argc != 4) {
00043         printf ("usage: remote_lat <connect-to> <message-size> "
00044             "<roundtrip-count>\n");
00045         return 1;
00046     }
00047     connect_to = argv [1];
00048     message_size = atoi (argv [2]);
00049     roundtrip_count = atoi (argv [3]);
00050 
00051     ctx = zmq_init (1);
00052     if (!ctx) {
00053         printf ("error in zmq_init: %s\n", zmq_strerror (errno));
00054         return -1;
00055     }
00056 
00057     s = zmq_socket (ctx, ZMQ_REQ);
00058     if (!s) {
00059         printf ("error in zmq_socket: %s\n", zmq_strerror (errno));
00060         return -1;
00061     }
00062 
00063     rc = zmq_connect (s, connect_to);
00064     if (rc != 0) {
00065         printf ("error in zmq_connect: %s\n", zmq_strerror (errno));
00066         return -1;
00067     }
00068 
00069     rc = zmq_msg_init_size (&msg, message_size);
00070     if (rc != 0) {
00071         printf ("error in zmq_msg_init_size: %s\n", zmq_strerror (errno));
00072         return -1;
00073     }
00074     memset (zmq_msg_data (&msg), 0, message_size);
00075 
00076     watch = zmq_stopwatch_start ();
00077 
00078     for (i = 0; i != roundtrip_count; i++) {
00079         rc = zmq_sendmsg (s, &msg, 0);
00080         if (rc < 0) {
00081             printf ("error in zmq_sendmsg: %s\n", zmq_strerror (errno));
00082             return -1;
00083         }
00084         rc = zmq_recvmsg (s, &msg, 0);
00085         if (rc < 0) {
00086             printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno));
00087             return -1;
00088         }
00089         if (zmq_msg_size (&msg) != message_size) {
00090             printf ("message of incorrect size received\n");
00091             return -1;
00092         }
00093     }
00094 
00095     elapsed = zmq_stopwatch_stop (watch);
00096 
00097     rc = zmq_msg_close (&msg);
00098     if (rc != 0) {
00099         printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno));
00100         return -1;
00101     }
00102 
00103     latency = (double) elapsed / (roundtrip_count * 2);
00104 
00105     printf ("message size: %d [B]\n", (int) message_size);
00106     printf ("roundtrip count: %d\n", (int) roundtrip_count);
00107     printf ("average latency: %.3f [us]\n", (double) latency);
00108 
00109     rc = zmq_close (s);
00110     if (rc != 0) {
00111         printf ("error in zmq_close: %s\n", zmq_strerror (errno));
00112         return -1;
00113     }
00114 
00115     rc = zmq_term (ctx);
00116     if (rc != 0) {
00117         printf ("error in zmq_term: %s\n", zmq_strerror (errno));
00118         return -1;
00119     }
00120 
00121     return 0;
00122 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines