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