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
31typedef struct {
32  uv_write_t req;
33  uv_buf_t buf;
34} write_req;
35
36
37static write_req* write_reqs;
38static uv_tcp_t tcp_client;
39static uv_connect_t connect_req;
40static uv_shutdown_t shutdown_req;
41
42static int shutdown_cb_called = 0;
43static int connect_cb_called = 0;
44static int write_cb_called = 0;
45static int close_cb_called = 0;
46
47static void connect_cb(uv_connect_t* req, int status);
48static void write_cb(uv_write_t* req, int status);
49static void shutdown_cb(uv_shutdown_t* req, int status);
50static void close_cb(uv_handle_t* handle);
51
52
53static void connect_cb(uv_connect_t* req, int status) {
54  write_req* w;
55  int i;
56  int r;
57
58  ASSERT(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(r == 0);
64  }
65
66  r = uv_shutdown(&shutdown_req, req->handle, shutdown_cb);
67  ASSERT(r == 0);
68
69  connect_cb_called++;
70}
71
72
73static void write_cb(uv_write_t* req, int status) {
74  ASSERT_NOT_NULL(req);
75  ASSERT(status == 0);
76  write_cb_called++;
77}
78
79
80static void shutdown_cb(uv_shutdown_t* req, int status) {
81  ASSERT(req->handle == (uv_stream_t*)&tcp_client);
82  ASSERT(req->handle->write_queue_size == 0);
83
84  uv_close((uv_handle_t*)req->handle, close_cb);
85  free(write_reqs);
86
87  shutdown_cb_called++;
88}
89
90
91static void close_cb(uv_handle_t* handle) {
92  ASSERT(handle == (uv_handle_t*)&tcp_client);
93  close_cb_called++;
94}
95
96
97BENCHMARK_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(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
116
117  r = uv_tcp_init(loop, &tcp_client);
118  ASSERT(r == 0);
119
120  r = uv_tcp_connect(&connect_req,
121                     &tcp_client,
122                     (const struct sockaddr*) &addr,
123                     connect_cb);
124  ASSERT(r == 0);
125
126  start = uv_hrtime();
127
128  r = uv_run(loop, UV_RUN_DEFAULT);
129  ASSERT(r == 0);
130
131  stop = uv_hrtime();
132
133  ASSERT(connect_cb_called == 1);
134  ASSERT(write_cb_called == NUM_WRITE_REQS);
135  ASSERT(shutdown_cb_called == 1);
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
142  MAKE_VALGRIND_HAPPY();
143  return 0;
144}
145