benchmark-udp-pummel.c revision 1.1.1.1
1/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 * 3 * Permission is hereby granted, free of charge, to any person obtaining a copy 4 * of this software and associated documentation files (the "Software"), to 5 * deal in the Software without restriction, including without limitation the 6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 * sell copies of the Software, and to permit persons to whom the Software is 8 * furnished to do so, subject to the following conditions: 9 * 10 * The above copyright notice and this permission notice shall be included in 11 * all copies or substantial portions of the Software. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 * IN THE SOFTWARE. 20 */ 21 22#include "task.h" 23#include "uv.h" 24 25#include <stdio.h> 26#include <stdlib.h> 27#include <string.h> 28 29#define EXPECTED "RANG TANG DING DONG I AM THE JAPANESE SANDMAN" 30 31#define TEST_DURATION 5000 /* ms */ 32 33#define BASE_PORT 12345 34 35struct sender_state { 36 struct sockaddr_in addr; 37 uv_udp_send_t send_req; 38 uv_udp_t udp_handle; 39}; 40 41struct receiver_state { 42 struct sockaddr_in addr; 43 uv_udp_t udp_handle; 44}; 45 46/* not used in timed mode */ 47static unsigned int packet_counter = (unsigned int) 1e6; 48 49static int n_senders_; 50static int n_receivers_; 51static uv_buf_t bufs[5]; 52static struct sender_state senders[1024]; 53static struct receiver_state receivers[1024]; 54 55static unsigned int send_cb_called; 56static unsigned int recv_cb_called; 57static unsigned int close_cb_called; 58static int timed; 59static int exiting; 60 61 62static void alloc_cb(uv_handle_t* handle, 63 size_t suggested_size, 64 uv_buf_t* buf) { 65 static char slab[65536]; 66 ASSERT(suggested_size <= sizeof(slab)); 67 buf->base = slab; 68 buf->len = sizeof(slab); 69} 70 71 72static void send_cb(uv_udp_send_t* req, int status) { 73 struct sender_state* s; 74 75 ASSERT(req != NULL); 76 77 if (status != 0) { 78 ASSERT(status == UV_ECANCELED); 79 return; 80 } 81 82 if (exiting) 83 return; 84 85 s = container_of(req, struct sender_state, send_req); 86 ASSERT(req->handle == &s->udp_handle); 87 88 if (timed) 89 goto send; 90 91 if (packet_counter == 0) { 92 uv_close((uv_handle_t*)&s->udp_handle, NULL); 93 return; 94 } 95 96 packet_counter--; 97 98send: 99 ASSERT(0 == uv_udp_send(&s->send_req, 100 &s->udp_handle, 101 bufs, 102 ARRAY_SIZE(bufs), 103 (const struct sockaddr*) &s->addr, 104 send_cb)); 105 send_cb_called++; 106} 107 108 109static void recv_cb(uv_udp_t* handle, 110 ssize_t nread, 111 const uv_buf_t* buf, 112 const struct sockaddr* addr, 113 unsigned flags) { 114 if (nread == 0) 115 return; 116 117 if (nread < 0) { 118 ASSERT(nread == UV_ECANCELED); 119 return; 120 } 121 122 ASSERT(addr->sa_family == AF_INET); 123 ASSERT(!memcmp(buf->base, EXPECTED, nread)); 124 125 recv_cb_called++; 126} 127 128 129static void close_cb(uv_handle_t* handle) { 130 ASSERT(handle != NULL); 131 close_cb_called++; 132} 133 134 135static void timeout_cb(uv_timer_t* timer) { 136 int i; 137 138 exiting = 1; 139 140 for (i = 0; i < n_senders_; i++) 141 uv_close((uv_handle_t*)&senders[i].udp_handle, close_cb); 142 143 for (i = 0; i < n_receivers_; i++) 144 uv_close((uv_handle_t*)&receivers[i].udp_handle, close_cb); 145} 146 147 148static int pummel(unsigned int n_senders, 149 unsigned int n_receivers, 150 unsigned long timeout) { 151 uv_timer_t timer_handle; 152 uint64_t duration; 153 uv_loop_t* loop; 154 unsigned int i; 155 156 ASSERT(n_senders <= ARRAY_SIZE(senders)); 157 ASSERT(n_receivers <= ARRAY_SIZE(receivers)); 158 159 loop = uv_default_loop(); 160 161 n_senders_ = n_senders; 162 n_receivers_ = n_receivers; 163 164 if (timeout) { 165 ASSERT(0 == uv_timer_init(loop, &timer_handle)); 166 ASSERT(0 == uv_timer_start(&timer_handle, timeout_cb, timeout, 0)); 167 /* Timer should not keep loop alive. */ 168 uv_unref((uv_handle_t*)&timer_handle); 169 timed = 1; 170 } 171 172 for (i = 0; i < n_receivers; i++) { 173 struct receiver_state* s = receivers + i; 174 struct sockaddr_in addr; 175 ASSERT(0 == uv_ip4_addr("0.0.0.0", BASE_PORT + i, &addr)); 176 ASSERT(0 == uv_udp_init(loop, &s->udp_handle)); 177 ASSERT(0 == uv_udp_bind(&s->udp_handle, (const struct sockaddr*) &addr, 0)); 178 ASSERT(0 == uv_udp_recv_start(&s->udp_handle, alloc_cb, recv_cb)); 179 uv_unref((uv_handle_t*)&s->udp_handle); 180 } 181 182 bufs[0] = uv_buf_init(EXPECTED + 0, 10); 183 bufs[1] = uv_buf_init(EXPECTED + 10, 10); 184 bufs[2] = uv_buf_init(EXPECTED + 20, 10); 185 bufs[3] = uv_buf_init(EXPECTED + 30, 10); 186 bufs[4] = uv_buf_init(EXPECTED + 40, 5); 187 188 for (i = 0; i < n_senders; i++) { 189 struct sender_state* s = senders + i; 190 ASSERT(0 == uv_ip4_addr("127.0.0.1", 191 BASE_PORT + (i % n_receivers), 192 &s->addr)); 193 ASSERT(0 == uv_udp_init(loop, &s->udp_handle)); 194 ASSERT(0 == uv_udp_send(&s->send_req, 195 &s->udp_handle, 196 bufs, 197 ARRAY_SIZE(bufs), 198 (const struct sockaddr*) &s->addr, 199 send_cb)); 200 } 201 202 duration = uv_hrtime(); 203 ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT)); 204 duration = uv_hrtime() - duration; 205 /* convert from nanoseconds to milliseconds */ 206 duration = duration / (uint64_t) 1e6; 207 208 printf("udp_pummel_%dv%d: %.0f/s received, %.0f/s sent. " 209 "%u received, %u sent in %.1f seconds.\n", 210 n_receivers, 211 n_senders, 212 recv_cb_called / (duration / 1000.0), 213 send_cb_called / (duration / 1000.0), 214 recv_cb_called, 215 send_cb_called, 216 duration / 1000.0); 217 218 MAKE_VALGRIND_HAPPY(); 219 return 0; 220} 221 222 223#define X(a, b) \ 224 BENCHMARK_IMPL(udp_pummel_##a##v##b) { \ 225 return pummel(a, b, 0); \ 226 } \ 227 BENCHMARK_IMPL(udp_timed_pummel_##a##v##b) { \ 228 return pummel(a, b, TEST_DURATION); \ 229 } 230 231X(1, 1) 232X(1, 10) 233X(1, 100) 234X(1, 1000) 235X(10, 10) 236X(10, 100) 237X(10, 1000) 238X(100, 10) 239X(100, 100) 240X(100, 1000) 241X(1000, 1000) 242 243#undef X 244