libuv-svace-build
243 строки · 6.6 Кб
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 {
36struct sockaddr_in addr;
37uv_udp_send_t send_req;
38uv_udp_t udp_handle;
39};
40
41struct receiver_state {
42struct sockaddr_in addr;
43uv_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,
63size_t suggested_size,
64uv_buf_t* buf) {
65static char slab[65536];
66ASSERT_LE(suggested_size, sizeof(slab));
67buf->base = slab;
68buf->len = sizeof(slab);
69}
70
71
72static void send_cb(uv_udp_send_t* req, int status) {
73struct sender_state* s;
74
75ASSERT_NOT_NULL(req);
76
77if (status != 0) {
78ASSERT_EQ(status, UV_ECANCELED);
79return;
80}
81
82if (exiting)
83return;
84
85s = container_of(req, struct sender_state, send_req);
86ASSERT_PTR_EQ(req->handle, &s->udp_handle);
87
88if (timed)
89goto send;
90
91if (packet_counter == 0) {
92uv_close((uv_handle_t*)&s->udp_handle, NULL);
93return;
94}
95
96packet_counter--;
97
98send:
99ASSERT_OK(uv_udp_send(&s->send_req,
100&s->udp_handle,
101bufs,
102ARRAY_SIZE(bufs),
103(const struct sockaddr*) &s->addr,
104send_cb));
105send_cb_called++;
106}
107
108
109static void recv_cb(uv_udp_t* handle,
110ssize_t nread,
111const uv_buf_t* buf,
112const struct sockaddr* addr,
113unsigned flags) {
114if (nread == 0)
115return;
116
117if (nread < 0) {
118ASSERT_EQ(nread, UV_ECANCELED);
119return;
120}
121
122ASSERT_EQ(addr->sa_family, AF_INET);
123ASSERT(!memcmp(buf->base, EXPECTED, nread));
124
125recv_cb_called++;
126}
127
128
129static void close_cb(uv_handle_t* handle) {
130ASSERT_NOT_NULL(handle);
131close_cb_called++;
132}
133
134
135static void timeout_cb(uv_timer_t* timer) {
136int i;
137
138exiting = 1;
139
140for (i = 0; i < n_senders_; i++)
141uv_close((uv_handle_t*)&senders[i].udp_handle, close_cb);
142
143for (i = 0; i < n_receivers_; i++)
144uv_close((uv_handle_t*)&receivers[i].udp_handle, close_cb);
145}
146
147
148static int pummel(unsigned int n_senders,
149unsigned int n_receivers,
150unsigned long timeout) {
151uv_timer_t timer_handle;
152uint64_t duration;
153uv_loop_t* loop;
154unsigned int i;
155
156ASSERT_LE(n_senders, ARRAY_SIZE(senders));
157ASSERT_LE(n_receivers, ARRAY_SIZE(receivers));
158
159loop = uv_default_loop();
160
161n_senders_ = n_senders;
162n_receivers_ = n_receivers;
163
164if (timeout) {
165ASSERT_OK(uv_timer_init(loop, &timer_handle));
166ASSERT_OK(uv_timer_start(&timer_handle, timeout_cb, timeout, 0));
167/* Timer should not keep loop alive. */
168uv_unref((uv_handle_t*)&timer_handle);
169timed = 1;
170}
171
172for (i = 0; i < n_receivers; i++) {
173struct receiver_state* s = receivers + i;
174struct sockaddr_in addr;
175ASSERT_OK(uv_ip4_addr("0.0.0.0", BASE_PORT + i, &addr));
176ASSERT_OK(uv_udp_init(loop, &s->udp_handle));
177ASSERT_OK(uv_udp_bind(&s->udp_handle, (const struct sockaddr*) &addr, 0));
178ASSERT_OK(uv_udp_recv_start(&s->udp_handle, alloc_cb, recv_cb));
179uv_unref((uv_handle_t*)&s->udp_handle);
180}
181
182bufs[0] = uv_buf_init(&EXPECTED[0], 10);
183bufs[1] = uv_buf_init(&EXPECTED[10], 10);
184bufs[2] = uv_buf_init(&EXPECTED[20], 10);
185bufs[3] = uv_buf_init(&EXPECTED[30], 10);
186bufs[4] = uv_buf_init(&EXPECTED[40], 5);
187
188for (i = 0; i < n_senders; i++) {
189struct sender_state* s = senders + i;
190ASSERT_OK(uv_ip4_addr("127.0.0.1",
191BASE_PORT + (i % n_receivers),
192&s->addr));
193ASSERT_OK(uv_udp_init(loop, &s->udp_handle));
194ASSERT_OK(uv_udp_send(&s->send_req,
195&s->udp_handle,
196bufs,
197ARRAY_SIZE(bufs),
198(const struct sockaddr*) &s->addr,
199send_cb));
200}
201
202duration = uv_hrtime();
203ASSERT_OK(uv_run(loop, UV_RUN_DEFAULT));
204duration = uv_hrtime() - duration;
205/* convert from nanoseconds to milliseconds */
206duration = duration / (uint64_t) 1e6;
207
208printf("udp_pummel_%dv%d: %.0f/s received, %.0f/s sent. "
209"%u received, %u sent in %.1f seconds.\n",
210n_receivers,
211n_senders,
212recv_cb_called / (duration / 1000.0),
213send_cb_called / (duration / 1000.0),
214recv_cb_called,
215send_cb_called,
216duration / 1000.0);
217
218MAKE_VALGRIND_HAPPY(loop);
219return 0;
220}
221
222
223#define X(a, b) \
224BENCHMARK_IMPL(udp_pummel_##a##v##b) { \
225return pummel(a, b, 0); \
226} \
227BENCHMARK_IMPL(udp_timed_pummel_##a##v##b) { \
228return 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