10e552da7Schristos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
20e552da7Schristos *
30e552da7Schristos * Permission is hereby granted, free of charge, to any person obtaining a copy
40e552da7Schristos * of this software and associated documentation files (the "Software"), to
50e552da7Schristos * deal in the Software without restriction, including without limitation the
60e552da7Schristos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
70e552da7Schristos * sell copies of the Software, and to permit persons to whom the Software is
80e552da7Schristos * furnished to do so, subject to the following conditions:
90e552da7Schristos *
100e552da7Schristos * The above copyright notice and this permission notice shall be included in
110e552da7Schristos * all copies or substantial portions of the Software.
120e552da7Schristos *
130e552da7Schristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
140e552da7Schristos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
150e552da7Schristos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
160e552da7Schristos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
170e552da7Schristos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
180e552da7Schristos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
190e552da7Schristos * IN THE SOFTWARE.
200e552da7Schristos */
210e552da7Schristos
220e552da7Schristos #include "uv.h"
230e552da7Schristos #include "task.h"
240e552da7Schristos
250e552da7Schristos #include <stdio.h>
260e552da7Schristos #include <stdlib.h>
270e552da7Schristos
280e552da7Schristos #define WRITE_REQ_DATA "Hello, world."
290e552da7Schristos #define NUM_WRITE_REQS (1000 * 1000)
300e552da7Schristos
310e552da7Schristos typedef struct {
320e552da7Schristos uv_write_t req;
330e552da7Schristos uv_buf_t buf;
340e552da7Schristos } write_req;
350e552da7Schristos
360e552da7Schristos
370e552da7Schristos static write_req* write_reqs;
380e552da7Schristos static uv_tcp_t tcp_client;
390e552da7Schristos static uv_connect_t connect_req;
400e552da7Schristos static uv_shutdown_t shutdown_req;
410e552da7Schristos
420e552da7Schristos static int shutdown_cb_called = 0;
430e552da7Schristos static int connect_cb_called = 0;
440e552da7Schristos static int write_cb_called = 0;
450e552da7Schristos static int close_cb_called = 0;
460e552da7Schristos
470e552da7Schristos static void connect_cb(uv_connect_t* req, int status);
480e552da7Schristos static void write_cb(uv_write_t* req, int status);
490e552da7Schristos static void shutdown_cb(uv_shutdown_t* req, int status);
500e552da7Schristos static void close_cb(uv_handle_t* handle);
510e552da7Schristos
520e552da7Schristos
connect_cb(uv_connect_t * req,int status)530e552da7Schristos static void connect_cb(uv_connect_t* req, int status) {
540e552da7Schristos write_req* w;
550e552da7Schristos int i;
560e552da7Schristos int r;
570e552da7Schristos
580e552da7Schristos ASSERT(req->handle == (uv_stream_t*)&tcp_client);
590e552da7Schristos
600e552da7Schristos for (i = 0; i < NUM_WRITE_REQS; i++) {
610e552da7Schristos w = &write_reqs[i];
620e552da7Schristos r = uv_write(&w->req, req->handle, &w->buf, 1, write_cb);
630e552da7Schristos ASSERT(r == 0);
640e552da7Schristos }
650e552da7Schristos
660e552da7Schristos r = uv_shutdown(&shutdown_req, req->handle, shutdown_cb);
670e552da7Schristos ASSERT(r == 0);
680e552da7Schristos
690e552da7Schristos connect_cb_called++;
700e552da7Schristos }
710e552da7Schristos
720e552da7Schristos
write_cb(uv_write_t * req,int status)730e552da7Schristos static void write_cb(uv_write_t* req, int status) {
74*5f2f4271Schristos ASSERT_NOT_NULL(req);
750e552da7Schristos ASSERT(status == 0);
760e552da7Schristos write_cb_called++;
770e552da7Schristos }
780e552da7Schristos
790e552da7Schristos
shutdown_cb(uv_shutdown_t * req,int status)800e552da7Schristos static void shutdown_cb(uv_shutdown_t* req, int status) {
810e552da7Schristos ASSERT(req->handle == (uv_stream_t*)&tcp_client);
820e552da7Schristos ASSERT(req->handle->write_queue_size == 0);
830e552da7Schristos
840e552da7Schristos uv_close((uv_handle_t*)req->handle, close_cb);
850e552da7Schristos free(write_reqs);
860e552da7Schristos
870e552da7Schristos shutdown_cb_called++;
880e552da7Schristos }
890e552da7Schristos
900e552da7Schristos
close_cb(uv_handle_t * handle)910e552da7Schristos static void close_cb(uv_handle_t* handle) {
920e552da7Schristos ASSERT(handle == (uv_handle_t*)&tcp_client);
930e552da7Schristos close_cb_called++;
940e552da7Schristos }
950e552da7Schristos
960e552da7Schristos
BENCHMARK_IMPL(tcp_write_batch)970e552da7Schristos BENCHMARK_IMPL(tcp_write_batch) {
980e552da7Schristos struct sockaddr_in addr;
990e552da7Schristos uv_loop_t* loop;
1000e552da7Schristos uint64_t start;
1010e552da7Schristos uint64_t stop;
1020e552da7Schristos int i;
1030e552da7Schristos int r;
1040e552da7Schristos
1050e552da7Schristos write_reqs = malloc(sizeof(*write_reqs) * NUM_WRITE_REQS);
106*5f2f4271Schristos ASSERT_NOT_NULL(write_reqs);
1070e552da7Schristos
1080e552da7Schristos /* Prepare the data to write out. */
1090e552da7Schristos for (i = 0; i < NUM_WRITE_REQS; i++) {
1100e552da7Schristos write_reqs[i].buf = uv_buf_init(WRITE_REQ_DATA,
1110e552da7Schristos sizeof(WRITE_REQ_DATA) - 1);
1120e552da7Schristos }
1130e552da7Schristos
1140e552da7Schristos loop = uv_default_loop();
1150e552da7Schristos ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
1160e552da7Schristos
1170e552da7Schristos r = uv_tcp_init(loop, &tcp_client);
1180e552da7Schristos ASSERT(r == 0);
1190e552da7Schristos
1200e552da7Schristos r = uv_tcp_connect(&connect_req,
1210e552da7Schristos &tcp_client,
1220e552da7Schristos (const struct sockaddr*) &addr,
1230e552da7Schristos connect_cb);
1240e552da7Schristos ASSERT(r == 0);
1250e552da7Schristos
1260e552da7Schristos start = uv_hrtime();
1270e552da7Schristos
1280e552da7Schristos r = uv_run(loop, UV_RUN_DEFAULT);
1290e552da7Schristos ASSERT(r == 0);
1300e552da7Schristos
1310e552da7Schristos stop = uv_hrtime();
1320e552da7Schristos
1330e552da7Schristos ASSERT(connect_cb_called == 1);
1340e552da7Schristos ASSERT(write_cb_called == NUM_WRITE_REQS);
1350e552da7Schristos ASSERT(shutdown_cb_called == 1);
1360e552da7Schristos ASSERT(close_cb_called == 1);
1370e552da7Schristos
1380e552da7Schristos printf("%ld write requests in %.2fs.\n",
1390e552da7Schristos (long)NUM_WRITE_REQS,
1400e552da7Schristos (stop - start) / 1e9);
1410e552da7Schristos
1420e552da7Schristos MAKE_VALGRIND_HAPPY();
1430e552da7Schristos return 0;
1440e552da7Schristos }
145