inproc_lat.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "../include/zmq.h"
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "platform.hpp"
  29. #if defined ZMQ_HAVE_WINDOWS
  30. #include <windows.h>
  31. #include <process.h>
  32. #else
  33. #include <pthread.h>
  34. #endif
  35. static size_t message_size;
  36. static int roundtrip_count;
  37. #if defined ZMQ_HAVE_WINDOWS
  38. static unsigned int __stdcall worker (void *ctx_)
  39. #else
  40. static void *worker (void *ctx_)
  41. #endif
  42. {
  43. void *s;
  44. int rc;
  45. int i;
  46. zmq_msg_t msg;
  47. s = zmq_socket (ctx_, ZMQ_REP);
  48. if (!s) {
  49. printf ("error in zmq_socket: %s\n", zmq_strerror (errno));
  50. exit (1);
  51. }
  52. rc = zmq_connect (s, "inproc://lat_test");
  53. if (rc != 0) {
  54. printf ("error in zmq_connect: %s\n", zmq_strerror (errno));
  55. exit (1);
  56. }
  57. rc = zmq_msg_init (&msg);
  58. if (rc != 0) {
  59. printf ("error in zmq_msg_init: %s\n", zmq_strerror (errno));
  60. exit (1);
  61. }
  62. for (i = 0; i != roundtrip_count; i++) {
  63. rc = zmq_recvmsg (s, &msg, 0);
  64. if (rc < 0) {
  65. printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno));
  66. exit (1);
  67. }
  68. rc = zmq_sendmsg (s, &msg, 0);
  69. if (rc < 0) {
  70. printf ("error in zmq_sendmsg: %s\n", zmq_strerror (errno));
  71. exit (1);
  72. }
  73. }
  74. rc = zmq_msg_close (&msg);
  75. if (rc != 0) {
  76. printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno));
  77. exit (1);
  78. }
  79. rc = zmq_close (s);
  80. if (rc != 0) {
  81. printf ("error in zmq_close: %s\n", zmq_strerror (errno));
  82. exit (1);
  83. }
  84. #if defined ZMQ_HAVE_WINDOWS
  85. return 0;
  86. #else
  87. return NULL;
  88. #endif
  89. }
  90. int main (int argc, char *argv[])
  91. {
  92. #if defined ZMQ_HAVE_WINDOWS
  93. HANDLE local_thread;
  94. #else
  95. pthread_t local_thread;
  96. #endif
  97. void *ctx;
  98. void *s;
  99. int rc;
  100. int i;
  101. zmq_msg_t msg;
  102. void *watch;
  103. unsigned long elapsed;
  104. double latency;
  105. if (argc != 3) {
  106. printf ("usage: inproc_lat <message-size> <roundtrip-count>\n");
  107. return 1;
  108. }
  109. message_size = atoi (argv[1]);
  110. roundtrip_count = atoi (argv[2]);
  111. ctx = zmq_init (1);
  112. if (!ctx) {
  113. printf ("error in zmq_init: %s\n", zmq_strerror (errno));
  114. return -1;
  115. }
  116. s = zmq_socket (ctx, ZMQ_REQ);
  117. if (!s) {
  118. printf ("error in zmq_socket: %s\n", zmq_strerror (errno));
  119. return -1;
  120. }
  121. rc = zmq_bind (s, "inproc://lat_test");
  122. if (rc != 0) {
  123. printf ("error in zmq_bind: %s\n", zmq_strerror (errno));
  124. return -1;
  125. }
  126. #if defined ZMQ_HAVE_WINDOWS
  127. local_thread = (HANDLE) _beginthreadex (NULL, 0, worker, ctx, 0, NULL);
  128. if (local_thread == 0) {
  129. printf ("error in _beginthreadex\n");
  130. return -1;
  131. }
  132. #else
  133. rc = pthread_create (&local_thread, NULL, worker, ctx);
  134. if (rc != 0) {
  135. printf ("error in pthread_create: %s\n", zmq_strerror (rc));
  136. return -1;
  137. }
  138. #endif
  139. rc = zmq_msg_init_size (&msg, message_size);
  140. if (rc != 0) {
  141. printf ("error in zmq_msg_init_size: %s\n", zmq_strerror (errno));
  142. return -1;
  143. }
  144. memset (zmq_msg_data (&msg), 0, message_size);
  145. printf ("message size: %d [B]\n", (int) message_size);
  146. printf ("roundtrip count: %d\n", (int) roundtrip_count);
  147. watch = zmq_stopwatch_start ();
  148. for (i = 0; i != roundtrip_count; i++) {
  149. rc = zmq_sendmsg (s, &msg, 0);
  150. if (rc < 0) {
  151. printf ("error in zmq_sendmsg: %s\n", zmq_strerror (errno));
  152. return -1;
  153. }
  154. rc = zmq_recvmsg (s, &msg, 0);
  155. if (rc < 0) {
  156. printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno));
  157. return -1;
  158. }
  159. if (zmq_msg_size (&msg) != message_size) {
  160. printf ("message of incorrect size received\n");
  161. return -1;
  162. }
  163. }
  164. elapsed = zmq_stopwatch_stop (watch);
  165. rc = zmq_msg_close (&msg);
  166. if (rc != 0) {
  167. printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno));
  168. return -1;
  169. }
  170. latency = (double) elapsed / (roundtrip_count * 2);
  171. #if defined ZMQ_HAVE_WINDOWS
  172. DWORD rc2 = WaitForSingleObject (local_thread, INFINITE);
  173. if (rc2 == WAIT_FAILED) {
  174. printf ("error in WaitForSingleObject\n");
  175. return -1;
  176. }
  177. BOOL rc3 = CloseHandle (local_thread);
  178. if (rc3 == 0) {
  179. printf ("error in CloseHandle\n");
  180. return -1;
  181. }
  182. #else
  183. rc = pthread_join (local_thread, NULL);
  184. if (rc != 0) {
  185. printf ("error in pthread_join: %s\n", zmq_strerror (rc));
  186. return -1;
  187. }
  188. #endif
  189. printf ("average latency: %.3f [us]\n", (double) latency);
  190. rc = zmq_close (s);
  191. if (rc != 0) {
  192. printf ("error in zmq_close: %s\n", zmq_strerror (errno));
  193. return -1;
  194. }
  195. rc = zmq_ctx_term (ctx);
  196. if (rc != 0) {
  197. printf ("error in zmq_ctx_term: %s\n", zmq_strerror (errno));
  198. return -1;
  199. }
  200. return 0;
  201. }