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 {
32 uv_write_t req;
33 uv_buf_t buf;
34 } write_req;
35
36
37 static write_req* write_reqs;
38 static uv_tcp_t tcp_client;
39 static uv_connect_t connect_req;
40 static uv_shutdown_t shutdown_req;
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
connect_cb(uv_connect_t * req,int status)53 static void connect_cb(uv_connect_t* req, int status) {
54 write_req* w;
55 int i;
56 int r;
57
58 ASSERT_PTR_EQ(req->handle, (uv_stream_t*)&tcp_client);
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_OK(r);
64 }
65
66 r = uv_shutdown(&shutdown_req, req->handle, shutdown_cb);
67 ASSERT_OK(r);
68
69 connect_cb_called++;
70 }
71
72
write_cb(uv_write_t * req,int status)73 static void write_cb(uv_write_t* req, int status) {
74 ASSERT_NOT_NULL(req);
75 ASSERT_OK(status);
76 write_cb_called++;
77 }
78
79
shutdown_cb(uv_shutdown_t * req,int status)80 static void shutdown_cb(uv_shutdown_t* req, int status) {
81 ASSERT_PTR_EQ(req->handle, (uv_stream_t*)&tcp_client);
82 ASSERT_OK(req->handle->write_queue_size);
83
84 uv_close((uv_handle_t*)req->handle, close_cb);
85 free(write_reqs);
86
87 shutdown_cb_called++;
88 }
89
90
close_cb(uv_handle_t * handle)91 static void close_cb(uv_handle_t* handle) {
92 ASSERT_PTR_EQ(handle, (uv_handle_t*)&tcp_client);
93 close_cb_called++;
94 }
95
96
BENCHMARK_IMPL(tcp_write_batch)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_NOT_NULL(write_reqs);
107
108 /* Prepare the data to write out. */
109 for (i = 0; i < NUM_WRITE_REQS; i++) {
110 write_reqs[i].buf = uv_buf_init(WRITE_REQ_DATA,
111 sizeof(WRITE_REQ_DATA) - 1);
112 }
113
114 loop = uv_default_loop();
115 ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
116
117 r = uv_tcp_init(loop, &tcp_client);
118 ASSERT_OK(r);
119
120 r = uv_tcp_connect(&connect_req,
121 &tcp_client,
122 (const struct sockaddr*) &addr,
123 connect_cb);
124 ASSERT_OK(r);
125
126 start = uv_hrtime();
127
128 r = uv_run(loop, UV_RUN_DEFAULT);
129 ASSERT_OK(r);
130
131 stop = uv_hrtime();
132
133 ASSERT_EQ(1, connect_cb_called);
134 ASSERT_EQ(write_cb_called, NUM_WRITE_REQS);
135 ASSERT_EQ(1, shutdown_cb_called);
136 ASSERT_EQ(1, close_cb_called);
137
138 printf("%ld write requests in %.2fs.\n",
139 (long)NUM_WRITE_REQS,
140 (stop - start) / 1e9);
141
142 MAKE_VALGRIND_HAPPY(loop);
143 return 0;
144 }
145