2
Copyright (c) 2009-2011 250bpm s.r.o.
3
Copyright (c) 2007-2009 iMatix Corporation
4
Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
6
This file is part of 0MQ.
8
0MQ is free software; you can redistribute it and/or modify it under
9
the terms of the GNU Lesser General Public License as published by
10
the Free Software Foundation; either version 3 of the License, or
11
(at your option) any later version.
13
0MQ is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU Lesser General Public License for more details.
18
You should have received a copy of the GNU Lesser General Public License
19
along with this program. If not, see <http://www.gnu.org/licenses/>.
22
#include "../include/zmq.h"
23
#include "../include/zmq_utils.h"
29
#include "../src/platform.hpp"
31
#if defined ZMQ_HAVE_WINDOWS
38
static int message_count;
39
static size_t message_size;
41
#if defined ZMQ_HAVE_WINDOWS
42
static unsigned int __stdcall worker (void *ctx_)
44
static void *worker (void *ctx_)
52
s = zmq_socket (ctx_, ZMQ_PUSH);
54
printf ("error in zmq_socket: %s\n", zmq_strerror (errno));
58
rc = zmq_connect (s, "inproc://thr_test");
60
printf ("error in zmq_connect: %s\n", zmq_strerror (errno));
64
for (i = 0; i != message_count; i++) {
66
rc = zmq_msg_init_size (&msg, message_size);
68
printf ("error in zmq_msg_init_size: %s\n", zmq_strerror (errno));
71
#if defined ZMQ_MAKE_VALGRIND_HAPPY
72
memset (zmq_msg_data (&msg), 0, message_size);
75
rc = zmq_sendmsg (s, &msg, 0);
77
printf ("error in zmq_sendmsg: %s\n", zmq_strerror (errno));
80
rc = zmq_msg_close (&msg);
82
printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno));
89
printf ("error in zmq_close: %s\n", zmq_strerror (errno));
93
#if defined ZMQ_HAVE_WINDOWS
100
int main (int argc, char *argv [])
102
#if defined ZMQ_HAVE_WINDOWS
105
pthread_t local_thread;
113
unsigned long elapsed;
114
unsigned long throughput;
118
printf ("usage: thread_thr <message-size> <message-count>\n");
122
message_size = atoi (argv [1]);
123
message_count = atoi (argv [2]);
127
printf ("error in zmq_init: %s\n", zmq_strerror (errno));
131
s = zmq_socket (ctx, ZMQ_PULL);
133
printf ("error in zmq_socket: %s\n", zmq_strerror (errno));
137
rc = zmq_bind (s, "inproc://thr_test");
139
printf ("error in zmq_bind: %s\n", zmq_strerror (errno));
143
#if defined ZMQ_HAVE_WINDOWS
144
local_thread = (HANDLE) _beginthreadex (NULL, 0,
145
worker, ctx, 0 , NULL);
146
if (local_thread == 0) {
147
printf ("error in _beginthreadex\n");
151
rc = pthread_create (&local_thread, NULL, worker, ctx);
153
printf ("error in pthread_create: %s\n", zmq_strerror (rc));
158
rc = zmq_msg_init (&msg);
160
printf ("error in zmq_msg_init: %s\n", zmq_strerror (errno));
164
printf ("message size: %d [B]\n", (int) message_size);
165
printf ("message count: %d\n", (int) message_count);
167
rc = zmq_recvmsg (s, &msg, 0);
169
printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno));
172
if (zmq_msg_size (&msg) != message_size) {
173
printf ("message of incorrect size received\n");
177
watch = zmq_stopwatch_start ();
179
for (i = 0; i != message_count - 1; i++) {
180
rc = zmq_recvmsg (s, &msg, 0);
182
printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno));
185
if (zmq_msg_size (&msg) != message_size) {
186
printf ("message of incorrect size received\n");
191
elapsed = zmq_stopwatch_stop (watch);
195
rc = zmq_msg_close (&msg);
197
printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno));
201
#if defined ZMQ_HAVE_WINDOWS
202
DWORD rc2 = WaitForSingleObject (local_thread, INFINITE);
203
if (rc2 == WAIT_FAILED) {
204
printf ("error in WaitForSingleObject\n");
207
BOOL rc3 = CloseHandle (local_thread);
209
printf ("error in CloseHandle\n");
213
rc = pthread_join (local_thread, NULL);
215
printf ("error in pthread_join: %s\n", zmq_strerror (rc));
222
printf ("error in zmq_close: %s\n", zmq_strerror (errno));
228
printf ("error in zmq_term: %s\n", zmq_strerror (errno));
232
throughput = (unsigned long)
233
((double) message_count / (double) elapsed * 1000000);
234
megabits = (double) (throughput * message_size * 8) / 1000000;
236
printf ("mean throughput: %d [msg/s]\n", (int) throughput);
237
printf ("mean throughput: %.3f [Mb/s]\n", (double) megabits);