1/*
2 Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3
4 This file is part of libzmq, the ZeroMQ core engine in C++.
5
6 libzmq is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License (LGPL) as published
8 by the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 As a special exception, the Contributors give you permission to link
12 this library with independent modules to produce an executable,
13 regardless of the license terms of these independent modules, and to
14 copy and distribute the resulting executable under terms of your choice,
15 provided that you also meet, for each linked independent module, the
16 terms and conditions of the license of that module. An independent
17 module is a module which is not derived from or based on this library.
18 If you modify this library, you must extend this exception to your
19 version of the library.
20
21 libzmq is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24 License for more details.
25
26 You should have received a copy of the GNU Lesser General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
28*/
29
30#include "../include/zmq.h"
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34
35int main (int argc, char *argv[])
36{
37 const char *connect_to;
38 int roundtrip_count;
39 size_t message_size;
40 void *ctx;
41 void *s;
42 int rc;
43 int i;
44 zmq_msg_t msg;
45 void *watch;
46 unsigned long elapsed;
47 double latency;
48
49 if (argc != 4) {
50 printf ("usage: remote_lat <connect-to> <message-size> "
51 "<roundtrip-count>\n");
52 return 1;
53 }
54 connect_to = argv[1];
55 message_size = atoi (argv[2]);
56 roundtrip_count = atoi (argv[3]);
57
58 ctx = zmq_init (1);
59 if (!ctx) {
60 printf ("error in zmq_init: %s\n", zmq_strerror (errno));
61 return -1;
62 }
63
64 s = zmq_socket (ctx, ZMQ_REQ);
65 if (!s) {
66 printf ("error in zmq_socket: %s\n", zmq_strerror (errno));
67 return -1;
68 }
69
70 rc = zmq_connect (s, connect_to);
71 if (rc != 0) {
72 printf ("error in zmq_connect: %s\n", zmq_strerror (errno));
73 return -1;
74 }
75
76 rc = zmq_msg_init_size (&msg, message_size);
77 if (rc != 0) {
78 printf ("error in zmq_msg_init_size: %s\n", zmq_strerror (errno));
79 return -1;
80 }
81 memset (zmq_msg_data (&msg), 0, message_size);
82
83 watch = zmq_stopwatch_start ();
84
85 for (i = 0; i != roundtrip_count; i++) {
86 rc = zmq_sendmsg (s, &msg, 0);
87 if (rc < 0) {
88 printf ("error in zmq_sendmsg: %s\n", zmq_strerror (errno));
89 return -1;
90 }
91 rc = zmq_recvmsg (s, &msg, 0);
92 if (rc < 0) {
93 printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno));
94 return -1;
95 }
96 if (zmq_msg_size (&msg) != message_size) {
97 printf ("message of incorrect size received\n");
98 return -1;
99 }
100 }
101
102 elapsed = zmq_stopwatch_stop (watch);
103
104 rc = zmq_msg_close (&msg);
105 if (rc != 0) {
106 printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno));
107 return -1;
108 }
109
110 latency = (double) elapsed / (roundtrip_count * 2);
111
112 printf ("message size: %d [B]\n", (int) message_size);
113 printf ("roundtrip count: %d\n", (int) roundtrip_count);
114 printf ("average latency: %.3f [us]\n", (double) latency);
115
116 rc = zmq_close (s);
117 if (rc != 0) {
118 printf ("error in zmq_close: %s\n", zmq_strerror (errno));
119 return -1;
120 }
121
122 rc = zmq_ctx_term (ctx);
123 if (rc != 0) {
124 printf ("error in zmq_ctx_term: %s\n", zmq_strerror (errno));
125 return -1;
126 }
127
128 return 0;
129}
130