|
160
|
1 /* Copyright libuv project 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 "uv.h"
|
|
|
23 #include "task.h"
|
|
|
24
|
|
|
25 #include <stdlib.h>
|
|
|
26 #include <stdio.h>
|
|
|
27
|
|
|
28 /* Run the benchmark for this many ms */
|
|
|
29 #define TIME 5000
|
|
|
30
|
|
|
31 typedef struct {
|
|
|
32 int pongs;
|
|
|
33 int state;
|
|
|
34 uv_udp_t udp;
|
|
|
35 struct sockaddr_in server_addr;
|
|
|
36 } pinger_t;
|
|
|
37
|
|
|
38 typedef struct buf_s {
|
|
|
39 uv_buf_t uv_buf_t;
|
|
|
40 struct buf_s* next;
|
|
|
41 } buf_t;
|
|
|
42
|
|
|
43 static char PING[] = "PING\n";
|
|
|
44
|
|
|
45 static uv_loop_t* loop;
|
|
|
46
|
|
|
47 static int completed_pingers;
|
|
|
48 static unsigned long completed_pings;
|
|
|
49 static int64_t start_time;
|
|
|
50
|
|
|
51
|
|
|
52 static void buf_alloc(uv_handle_t* tcp, size_t size, uv_buf_t* buf) {
|
|
|
53 static char slab[64 * 1024];
|
|
|
54 buf->base = slab;
|
|
|
55 buf->len = sizeof(slab);
|
|
|
56 }
|
|
|
57
|
|
|
58
|
|
|
59 static void buf_free(const uv_buf_t* buf) {
|
|
|
60 }
|
|
|
61
|
|
|
62
|
|
|
63 static void pinger_close_cb(uv_handle_t* handle) {
|
|
|
64 pinger_t* pinger;
|
|
|
65
|
|
|
66 pinger = (pinger_t*)handle->data;
|
|
|
67 #if DEBUG
|
|
|
68 fprintf(stderr, "ping_pongs: %d roundtrips/s\n",
|
|
|
69 pinger->pongs / (TIME / 1000));
|
|
|
70 #endif
|
|
|
71
|
|
|
72 completed_pings += pinger->pongs;
|
|
|
73 completed_pingers++;
|
|
|
74 free(pinger);
|
|
|
75 }
|
|
|
76
|
|
|
77 static void pinger_write_ping(pinger_t* pinger) {
|
|
|
78 uv_buf_t buf;
|
|
|
79 int r;
|
|
|
80
|
|
|
81 buf = uv_buf_init(PING, sizeof(PING) - 1);
|
|
|
82 r = uv_udp_try_send(&pinger->udp, &buf, 1,
|
|
|
83 (const struct sockaddr*) &pinger->server_addr);
|
|
|
84 if (r < 0)
|
|
|
85 FATAL("uv_udp_send failed");
|
|
|
86 }
|
|
|
87
|
|
|
88 static void pinger_read_cb(uv_udp_t* udp,
|
|
|
89 ssize_t nread,
|
|
|
90 const uv_buf_t* buf,
|
|
|
91 const struct sockaddr* addr,
|
|
|
92 unsigned flags) {
|
|
|
93 ssize_t i;
|
|
|
94 pinger_t* pinger;
|
|
|
95 pinger = (pinger_t*)udp->data;
|
|
|
96
|
|
|
97 /* No data here means something went wrong */
|
|
|
98 ASSERT_GT(nread, 0);
|
|
|
99
|
|
|
100 /* Now we count the pings */
|
|
|
101 for (i = 0; i < nread; i++) {
|
|
|
102 ASSERT_EQ(buf->base[i], PING[pinger->state]);
|
|
|
103 pinger->state = (pinger->state + 1) % (sizeof(PING) - 1);
|
|
|
104 if (pinger->state == 0) {
|
|
|
105 pinger->pongs++;
|
|
|
106 if (uv_now(loop) - start_time > TIME) {
|
|
|
107 uv_close((uv_handle_t*)udp, pinger_close_cb);
|
|
|
108 break;
|
|
|
109 }
|
|
|
110 pinger_write_ping(pinger);
|
|
|
111 }
|
|
|
112 }
|
|
|
113
|
|
|
114 if (buf && !(flags & UV_UDP_MMSG_CHUNK))
|
|
|
115 buf_free(buf);
|
|
|
116 }
|
|
|
117
|
|
|
118 static void udp_pinger_new(void) {
|
|
|
119 pinger_t* pinger = malloc(sizeof(*pinger));
|
|
|
120 int r;
|
|
|
121
|
|
|
122 ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &pinger->server_addr));
|
|
|
123 pinger->state = 0;
|
|
|
124 pinger->pongs = 0;
|
|
|
125
|
|
|
126 /* Try to do NUM_PINGS ping-pongs (connection-less). */
|
|
|
127 r = uv_udp_init(loop, &pinger->udp);
|
|
|
128 ASSERT_OK(r);
|
|
|
129 r = uv_udp_bind(&pinger->udp, (const struct sockaddr*) &pinger->server_addr, 0);
|
|
|
130 ASSERT_OK(r);
|
|
|
131
|
|
|
132 pinger->udp.data = pinger;
|
|
|
133
|
|
|
134 /* Start pinging */
|
|
|
135 if (0 != uv_udp_recv_start(&pinger->udp, buf_alloc, pinger_read_cb)) {
|
|
|
136 FATAL("uv_udp_read_start failed");
|
|
|
137 }
|
|
|
138 pinger_write_ping(pinger);
|
|
|
139 }
|
|
|
140
|
|
|
141 static int ping_udp(unsigned pingers) {
|
|
|
142 unsigned i;
|
|
|
143
|
|
|
144 loop = uv_default_loop();
|
|
|
145 start_time = uv_now(loop);
|
|
|
146
|
|
|
147 for (i = 0; i < pingers; ++i) {
|
|
|
148 udp_pinger_new();
|
|
|
149 }
|
|
|
150 uv_run(loop, UV_RUN_DEFAULT);
|
|
|
151 ASSERT_GE(completed_pingers, 1);
|
|
|
152
|
|
|
153 fprintf(stderr, "ping_pongs: %d pingers, ~ %lu roundtrips/s\n",
|
|
|
154 completed_pingers, completed_pings / (TIME/1000));
|
|
|
155
|
|
|
156 MAKE_VALGRIND_HAPPY(loop);
|
|
|
157 return 0;
|
|
|
158 }
|
|
|
159
|
|
|
160 #define X(PINGERS) \
|
|
|
161 BENCHMARK_IMPL(ping_udp##PINGERS) {\
|
|
|
162 return ping_udp(PINGERS); \
|
|
|
163 }
|
|
|
164
|
|
|
165 X(1)
|
|
|
166 X(10)
|
|
|
167 X(100)
|
|
|
168
|
|
|
169 #undef X
|