libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
local_thr.cpp
Go to the documentation of this file.
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 
34 int main (int argc, char *argv [])
35 {
36  const char *bind_to;
37  int message_count;
38  size_t message_size;
39  void *ctx;
40  void *s;
41  int rc;
42  int i;
43  zmq_msg_t msg;
44  void *watch;
45  unsigned long elapsed;
46  unsigned long throughput;
47  double megabits;
48 
49  if (argc != 4) {
50  printf ("usage: local_thr <bind-to> <message-size> <message-count>\n");
51  return 1;
52  }
53  bind_to = argv [1];
54  message_size = atoi (argv [2]);
55  message_count = atoi (argv [3]);
56 
57  ctx = zmq_init (1);
58  if (!ctx) {
59  printf ("error in zmq_init: %s\n", zmq_strerror (errno));
60  return -1;
61  }
62 
63  s = zmq_socket (ctx, ZMQ_PULL);
64  if (!s) {
65  printf ("error in zmq_socket: %s\n", zmq_strerror (errno));
66  return -1;
67  }
68 
69  // Add your socket options here.
70  // For example ZMQ_RATE, ZMQ_RECOVERY_IVL and ZMQ_MCAST_LOOP for PGM.
71 
72  rc = zmq_bind (s, bind_to);
73  if (rc != 0) {
74  printf ("error in zmq_bind: %s\n", zmq_strerror (errno));
75  return -1;
76  }
77 
78  rc = zmq_msg_init (&msg);
79  if (rc != 0) {
80  printf ("error in zmq_msg_init: %s\n", zmq_strerror (errno));
81  return -1;
82  }
83 
84  rc = zmq_recvmsg (s, &msg, 0);
85  if (rc < 0) {
86  printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno));
87  return -1;
88  }
89  if (zmq_msg_size (&msg) != message_size) {
90  printf ("message of incorrect size received\n");
91  return -1;
92  }
93 
94  watch = zmq_stopwatch_start ();
95 
96  for (i = 0; i != message_count - 1; i++) {
97  rc = zmq_recvmsg (s, &msg, 0);
98  if (rc < 0) {
99  printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno));
100  return -1;
101  }
102  if (zmq_msg_size (&msg) != message_size) {
103  printf ("message of incorrect size received\n");
104  return -1;
105  }
106  }
107 
108  elapsed = zmq_stopwatch_stop (watch);
109  if (elapsed == 0)
110  elapsed = 1;
111 
112  rc = zmq_msg_close (&msg);
113  if (rc != 0) {
114  printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno));
115  return -1;
116  }
117 
118  throughput = (unsigned long)
119  ((double) message_count / (double) elapsed * 1000000);
120  megabits = ((double) throughput * message_size * 8) / 1000000;
121 
122  printf ("message size: %d [B]\n", (int) message_size);
123  printf ("message count: %d\n", (int) message_count);
124  printf ("mean throughput: %d [msg/s]\n", (int) throughput);
125  printf ("mean throughput: %.3f [Mb/s]\n", (double) megabits);
126 
127  rc = zmq_close (s);
128  if (rc != 0) {
129  printf ("error in zmq_close: %s\n", zmq_strerror (errno));
130  return -1;
131  }
132 
133  rc = zmq_ctx_term (ctx);
134  if (rc != 0) {
135  printf ("error in zmq_ctx_term: %s\n", zmq_strerror (errno));
136  return -1;
137  }
138 
139  return 0;
140 }
static size_t message_size
Definition: inproc_lat.cpp:45
ZMQ_EXPORT void * zmq_init(int io_threads)
Definition: zmq.cpp:220
ZMQ_EXPORT int zmq_recvmsg(void *s, zmq_msg_t *msg, int flags)
Definition: zmq.cpp:501
ZMQ_EXPORT unsigned long zmq_stopwatch_stop(void *watch_)
Definition: zmq_utils.cpp:70
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
ZMQ_EXPORT int zmq_close(void *s)
Definition: zmq.cpp:255
ZMQ_EXPORT const char * zmq_strerror(int errnum)
Definition: zmq.cpp:102
Definition: zmq.h:221
ZMQ_EXPORT int zmq_bind(void *s, const char *addr)
Definition: zmq.cpp:321
int main(int argc, char *argv[])
Definition: local_thr.cpp:34
ZMQ_EXPORT int zmq_ctx_term(void *context)
Definition: zmq.cpp:162
ZMQ_EXPORT void * zmq_stopwatch_start(void)
Definition: zmq_utils.cpp:62
ZMQ_EXPORT int zmq_msg_close(zmq_msg_t *msg)
Definition: zmq.cpp:651
ZMQ_EXPORT size_t zmq_msg_size(zmq_msg_t *msg)
Definition: zmq.cpp:671
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg)
Definition: zmq.cpp:613
#define ZMQ_PULL
Definition: zmq.h:253
static int message_count
Definition: inproc_thr.cpp:47