benchmark-ping-udp.c
Go to the documentation of this file.
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;
35  struct sockaddr_in server_addr;
36 } pinger_t;
37 
38 typedef struct buf_s {
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;
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 
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;
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 
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  /* Now we count the pings */
98  for (i = 0; i < nread; i++) {
99  ASSERT(buf->base[i] == PING[pinger->state]);
100  pinger->state = (pinger->state + 1) % (sizeof(PING) - 1);
101  if (pinger->state == 0) {
102  pinger->pongs++;
103  if (uv_now(loop) - start_time > TIME) {
105  break;
106  }
107  pinger_write_ping(pinger);
108  }
109  }
110 
111  buf_free(buf);
112 }
113 
114 static void udp_pinger_new(void) {
115  pinger_t* pinger = malloc(sizeof(*pinger));
116  int r;
117 
118  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &pinger->server_addr));
119  pinger->state = 0;
120  pinger->pongs = 0;
121 
122  /* Try to do NUM_PINGS ping-pongs (connection-less). */
123  r = uv_udp_init(loop, &pinger->udp);
124  ASSERT(r == 0);
125 
126  pinger->udp.data = pinger;
127 
128  /* Start pinging */
129  if (0 != uv_udp_recv_start(&pinger->udp, buf_alloc, pinger_read_cb)) {
130  FATAL("uv_udp_read_start failed");
131  }
132  pinger_write_ping(pinger);
133 }
134 
135 static int ping_udp(unsigned pingers) {
136  unsigned i;
137 
138  loop = uv_default_loop();
140 
141  for (i = 0; i < pingers; ++i) {
142  udp_pinger_new();
143  }
146 
147  fprintf(stderr, "ping_pongs: %d pingers, ~ %lu roundtrips/s\n",
149 
151  return 0;
152 }
153 
154 #define X(PINGERS) \
155 BENCHMARK_IMPL(ping_udp##PINGERS) {\
156  return ping_udp(PINGERS); \
157 }
158 
159 X(1)
160 X(10)
161 X(100)
162 
163 #undef X
slab
static char slab[1]
Definition: test-watcher-cross-stop.c:37
task.h
uv_now
UV_EXTERN uint64_t uv_now(const uv_loop_t *)
Definition: uv-common.c:537
pinger_t::pongs
int pongs
Definition: benchmark-ping-pongs.c:33
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
completed_pings
static unsigned long completed_pings
Definition: benchmark-ping-udp.c:48
ASSERT
#define ASSERT(expr)
Definition: task.h:102
tcp
static uv_tcp_t tcp
Definition: test-connection-fail.c:29
buf_s::next
struct buf_s * next
Definition: benchmark-ping-pongs.c:42
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
PING
static char PING[]
Definition: benchmark-ping-udp.c:43
start_time
static int64_t start_time
Definition: benchmark-ping-udp.c:49
buf_s::uv_buf_t
uv_buf_t uv_buf_t
Definition: benchmark-ping-pongs.c:41
TEST_PORT
#define TEST_PORT
Definition: task.h:53
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_ip4_addr
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
for
for(map_begin_internal(intern, &it);!map_done(&it);map_next(&it))
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/map.c:207
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
uv_udp_init
UV_EXTERN int uv_udp_init(uv_loop_t *, uv_udp_t *handle)
Definition: unix/udp.c:988
ssize_t
intptr_t ssize_t
Definition: win.h:27
pinger_t::udp
uv_udp_t udp
Definition: benchmark-ping-udp.c:34
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
PE_SECTION_TYPES::DEBUG
@ DEBUG
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uv_udp_s
Definition: uv.h:629
buf_t
struct buf_s buf_t
ping_udp
static int ping_udp(unsigned pingers)
Definition: benchmark-ping-udp.c:135
pinger_read_cb
static void pinger_read_cb(uv_udp_t *udp, ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr, unsigned flags)
Definition: benchmark-ping-udp.c:88
buf_free
static void buf_free(const uv_buf_t *buf)
Definition: benchmark-ping-udp.c:59
loop
static uv_loop_t * loop
Definition: benchmark-ping-udp.c:45
udp
static uv_udp_t udp
Definition: test-getsockname.c:38
uv.h
pinger_t::state
int state
Definition: benchmark-ping-pongs.c:34
uv_udp_recv_start
UV_EXTERN int uv_udp_recv_start(uv_udp_t *handle, uv_alloc_cb alloc_cb, uv_udp_recv_cb recv_cb)
Definition: uv-common.c:438
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
completed_pingers
static int completed_pingers
Definition: benchmark-ping-udp.c:47
FATAL
#define FATAL(msg)
Definition: task.h:88
uv_buf_t
Definition: unix.h:121
pinger_write_ping
static void pinger_write_ping(pinger_t *pinger)
Definition: benchmark-ping-udp.c:77
buf_alloc
static void buf_alloc(uv_handle_t *tcp, size_t size, uv_buf_t *buf)
Definition: benchmark-ping-udp.c:52
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
pinger_close_cb
static void pinger_close_cb(uv_handle_t *handle)
Definition: benchmark-ping-udp.c:63
fix_build_deps.r
r
Definition: fix_build_deps.py:491
pinger_t::server_addr
struct sockaddr_in server_addr
Definition: benchmark-ping-udp.c:35
pinger_t
Definition: benchmark-ping-pongs.c:32
uv_buf_init
UV_EXTERN uv_buf_t uv_buf_init(char *base, unsigned int len)
Definition: uv-common.c:157
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
uv_loop_s
Definition: uv.h:1767
TIME
#define TIME
Definition: benchmark-ping-udp.c:29
X
#define X(PINGERS)
Definition: benchmark-ping-udp.c:154
uv_udp_try_send
UV_EXTERN int uv_udp_try_send(uv_udp_t *handle, const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr *addr)
Definition: uv-common.c:424
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
udp_pinger_new
static void udp_pinger_new(void)
Definition: benchmark-ping-udp.c:114
buf_s
Definition: benchmark-ping-pongs.c:40
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
state
static struct rpc_state state
Definition: bad_server_response_test.cc:87


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:45