benchmark-tcp-write-batch.c
Go to the documentation of this file.
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 "uv.h"
23 #include "task.h"
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 
28 #define WRITE_REQ_DATA "Hello, world."
29 #define NUM_WRITE_REQS (1000 * 1000)
30 
31 typedef struct {
34 } write_req;
35 
36 
41 
42 static int shutdown_cb_called = 0;
43 static int connect_cb_called = 0;
44 static int write_cb_called = 0;
45 static int close_cb_called = 0;
46 
47 static void connect_cb(uv_connect_t* req, int status);
48 static void write_cb(uv_write_t* req, int status);
49 static void shutdown_cb(uv_shutdown_t* req, int status);
50 static void close_cb(uv_handle_t* handle);
51 
52 
53 static void connect_cb(uv_connect_t* req, int status) {
54  write_req* w;
55  int i;
56  int r;
57 
59 
60  for (i = 0; i < NUM_WRITE_REQS; i++) {
61  w = &write_reqs[i];
62  r = uv_write(&w->req, req->handle, &w->buf, 1, write_cb);
63  ASSERT(r == 0);
64  }
65 
67  ASSERT(r == 0);
68 
70 }
71 
72 
73 static void write_cb(uv_write_t* req, int status) {
74  ASSERT(req != NULL);
75  ASSERT(status == 0);
77 }
78 
79 
80 static void shutdown_cb(uv_shutdown_t* req, int status) {
82  ASSERT(req->handle->write_queue_size == 0);
83 
85  free(write_reqs);
86 
88 }
89 
90 
91 static void close_cb(uv_handle_t* handle) {
94 }
95 
96 
97 BENCHMARK_IMPL(tcp_write_batch) {
98  struct sockaddr_in addr;
99  uv_loop_t* loop;
100  uint64_t start;
101  uint64_t stop;
102  int i;
103  int r;
104 
105  write_reqs = malloc(sizeof(*write_reqs) * NUM_WRITE_REQS);
106  ASSERT(write_reqs != NULL);
107 
108  /* Prepare the data to write out. */
109  for (i = 0; i < NUM_WRITE_REQS; i++) {
111  sizeof(WRITE_REQ_DATA) - 1);
112  }
113 
114  loop = uv_default_loop();
115  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
116 
118  ASSERT(r == 0);
119 
121  &tcp_client,
122  (const struct sockaddr*) &addr,
123  connect_cb);
124  ASSERT(r == 0);
125 
126  start = uv_hrtime();
127 
129  ASSERT(r == 0);
130 
131  stop = uv_hrtime();
132 
136  ASSERT(close_cb_called == 1);
137 
138  printf("%ld write requests in %.2fs.\n",
139  (long)NUM_WRITE_REQS,
140  (stop - start) / 1e9);
141 
143  return 0;
144 }
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
task.h
uv_connect_s
Definition: uv.h:580
uv_shutdown_s
Definition: uv.h:417
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
uv_connect_s::handle
uv_stream_t * handle
Definition: uv.h:583
ASSERT
#define ASSERT(expr)
Definition: task.h:102
WRITE_REQ_DATA
#define WRITE_REQ_DATA
Definition: benchmark-tcp-write-batch.c:28
status
absl::Status status
Definition: rls.cc:251
write_req
Definition: benchmark-tcp-write-batch.c:31
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
write_req::req
uv_write_t req
Definition: benchmark-tcp-write-batch.c:32
shutdown_req
static uv_shutdown_t shutdown_req
Definition: benchmark-tcp-write-batch.c:40
TEST_PORT
#define TEST_PORT
Definition: task.h:53
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_stream_s
Definition: uv.h:491
write_req::buf
uv_buf_t buf
Definition: benchmark-tcp-write-batch.c:33
uv_tcp_connect
UV_EXTERN int uv_tcp_connect(uv_connect_t *req, uv_tcp_t *handle, const struct sockaddr *addr, uv_connect_cb cb)
Definition: uv-common.c:315
uv_ip4_addr
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
NUM_WRITE_REQS
#define NUM_WRITE_REQS
Definition: benchmark-tcp-write-batch.c:29
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
start
static uint64_t start
Definition: benchmark-pound.c:74
write_reqs
static write_req * write_reqs
Definition: benchmark-tcp-write-batch.c:37
write_cb
static void write_cb(uv_write_t *req, int status)
Definition: benchmark-tcp-write-batch.c:73
uv_write
UV_EXTERN int uv_write(uv_write_t *req, uv_stream_t *handle, const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb)
Definition: unix/stream.c:1492
req
static uv_connect_t req
Definition: test-connection-fail.c:30
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
tcp_client
static uv_tcp_t tcp_client
Definition: benchmark-tcp-write-batch.c:38
uv_shutdown
UV_PRIVATE_REQ_TYPES UV_EXTERN int uv_shutdown(uv_shutdown_t *req, uv_stream_t *handle, uv_shutdown_cb cb)
Definition: unix/stream.c:1259
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
connect_cb
static void connect_cb(uv_connect_t *req, int status)
Definition: benchmark-tcp-write-batch.c:53
BENCHMARK_IMPL
BENCHMARK_IMPL(tcp_write_batch)
Definition: benchmark-tcp-write-batch.c:97
uv_tcp_init
UV_EXTERN int uv_tcp_init(uv_loop_t *, uv_tcp_t *handle)
Definition: unix/tcp.c:143
uv_tcp_s
Definition: uv.h:544
close_cb_called
static int close_cb_called
Definition: benchmark-tcp-write-batch.c:45
connect_cb_called
static int connect_cb_called
Definition: benchmark-tcp-write-batch.c:43
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
shutdown_cb
static void shutdown_cb(uv_shutdown_t *req, int status)
Definition: benchmark-tcp-write-batch.c:80
uv_buf_t
Definition: unix.h:121
connect_req
static uv_connect_t connect_req
Definition: benchmark-tcp-write-batch.c:39
uv_hrtime
UV_EXTERN uint64_t uv_hrtime(void)
Definition: unix/core.c:107
fix_build_deps.r
r
Definition: fix_build_deps.py:491
uv_buf_init
UV_EXTERN uv_buf_t uv_buf_init(char *base, unsigned int len)
Definition: uv-common.c:157
uv_write_s
Definition: uv.h:522
stop
static const char stop[]
Definition: benchmark-async-pummel.c:35
shutdown_cb_called
static int shutdown_cb_called
Definition: benchmark-tcp-write-batch.c:42
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
uv_loop_s
Definition: uv.h:1767
write_cb_called
static int write_cb_called
Definition: benchmark-tcp-write-batch.c:44
write_req
uv_write_t write_req
Definition: libuv/docs/code/tty-gravity/main.c:9
close_cb
static void close_cb(uv_handle_t *handle)
Definition: benchmark-tcp-write-batch.c:91
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


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:36