Skip to content

Commit

Permalink
Fix pipe-pound and clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Zinkovsky committed Aug 18, 2011
1 parent 99abfd3 commit dcf8770
Showing 1 changed file with 79 additions and 64 deletions.
143 changes: 79 additions & 64 deletions test/benchmark-pound.c
Expand Up @@ -30,27 +30,36 @@

#define DEBUG 0

struct conn_rec_s;

typedef void (*setup_fn)(int num, void* arg);
typedef void (*make_connect_fn)(struct conn_rec_s* conn);
typedef int (*connect_fn)(int num, make_connect_fn make_connect, void* arg);

/* Base class for tcp_conn_rec and pipe_conn_rec.
* The ordering of fields matters!
*/
typedef struct {
typedef struct conn_rec_s {
int i;
uv_connect_t conn_req;
uv_write_t write_req;
make_connect_fn make_connect;
uv_stream_t stream;
} conn_rec;

typedef struct {
int i;
uv_connect_t conn_req;
uv_write_t write_req;
make_connect_fn make_connect;
uv_tcp_t stream;
} tcp_conn_rec;

typedef struct {
int i;
uv_connect_t conn_req;
uv_write_t write_req;
make_connect_fn make_connect;
uv_pipe_t stream;
} pipe_conn_rec;

Expand All @@ -63,9 +72,6 @@ static uint64_t start; /* in ms */
static int closed_streams;
static int conns_failed;

typedef void *(*setup_fn)(int num, void* arg);
typedef int (*connect_fn)(int num, void* handles, void* arg);

static uv_buf_t alloc_cb(uv_stream_t* stream, size_t suggested_size);
static void connect_cb(uv_connect_t* conn_req, int status);
static void read_cb(uv_stream_t* stream, ssize_t nread, uv_buf_t buf);
Expand Down Expand Up @@ -106,7 +112,7 @@ static void connect_cb(uv_connect_t* req, int status) {
ASSERT(req != NULL);
ASSERT(status == 0);

conn = req->data;
conn = (conn_rec*)req->data;
ASSERT(conn != NULL);

#if DEBUG
Expand All @@ -125,7 +131,7 @@ static void connect_cb(uv_connect_t* req, int status) {


static void read_cb(uv_stream_t* stream, ssize_t nread, uv_buf_t buf) {
conn_rec* p = stream->data;
conn_rec* p = (conn_rec*)stream->data;
uv_err_t err = uv_last_error();

ASSERT(stream != NULL);
Expand All @@ -148,34 +154,9 @@ static void read_cb(uv_stream_t* stream, ssize_t nread, uv_buf_t buf) {
}
}

static void make_connect(tcp_conn_rec* p) {
struct sockaddr_in addr;
int r;

r = uv_tcp_init(&p->stream);
ASSERT(r == 0);

addr = uv_ip4_addr("127.0.0.1", TEST_PORT);

r = uv_tcp_connect(&p->conn_req, &p->stream, addr, connect_cb);
if (r) {
fprintf(stderr, "uv_tcp_connect error %s\n",
uv_err_name(uv_last_error()));
ASSERT(0);
}

#if DEBUG
printf("make connect %d\n", p->i);
#endif

p->conn_req.data = p;
p->write_req.data = p;
p->stream.data = p;
}


static void close_cb(uv_handle_t* handle) {
tcp_conn_rec* p = handle->data;
conn_rec* p = (conn_rec*)handle->data;

ASSERT(handle != NULL);
closed_streams++;
Expand All @@ -185,61 +166,96 @@ static void close_cb(uv_handle_t* handle) {
#endif

if (uv_now() - start < 10000) {
make_connect(p);
p->make_connect(p);
}
}


static void* tcp_do_setup(int num, void* arg) {
static void tcp_do_setup(int num, void* arg) {
int i;

for (i = 0; i < num; i++) {
tcp_conns[i].i = i;
}
}

return tcp_conns;

static void pipe_do_setup(int num, void* arg) {
int i;

for (i = 0; i < num; i++) {
pipe_conns[i].i = i;
}
}


static void* pipe_do_setup(int num, void* arg) {
pipe_conn_rec* pe;
pipe_conn_rec* p;
static void tcp_make_connect(conn_rec* p) {
struct sockaddr_in addr;
int r;

for (p = pipe_conns, pe = p + num; p < pe; p++) {
r = uv_pipe_init(&p->stream);
ASSERT(r == 0);
r = uv_tcp_init((uv_tcp_t*)&p->stream);
ASSERT(r == 0);

addr = uv_ip4_addr("127.0.0.1", TEST_PORT);

r = uv_tcp_connect(&((tcp_conn_rec*)p)->conn_req, (uv_tcp_t*)&p->stream, addr, connect_cb);
if (r) {
fprintf(stderr, "uv_tcp_connect error %s\n",
uv_err_name(uv_last_error()));
ASSERT(0);
}

return pipe_conns;
#if DEBUG
printf("make connect %d\n", p->i);
#endif

p->conn_req.data = p;
p->write_req.data = p;
p->stream.data = p;
}


static int tcp_do_connect(int num, void* conns, void* arg) {
tcp_conn_rec* pe;
tcp_conn_rec* p;
static void pipe_make_connect(conn_rec* p) {
int r;
int i;

r = uv_pipe_init((uv_pipe_t*)&p->stream);
ASSERT(r == 0);

r = uv_pipe_connect(&((pipe_conn_rec*)p)->conn_req, (uv_pipe_t*)&p->stream, TEST_PIPENAME, connect_cb);
if (r) {
fprintf(stderr, "uv_tcp_connect error %s\n",
uv_err_name(uv_last_error()));
ASSERT(0);
}

#if DEBUG
printf("make connect %d\n", p->i);
#endif

p->conn_req.data = p;
p->write_req.data = p;
p->stream.data = p;
}


static int tcp_do_connect(int num, make_connect_fn make_connect, void* arg) {
int i;

for (i = 0; i < num; i++) {
make_connect(&tcp_conns[i]);
tcp_make_connect((conn_rec*)&tcp_conns[i]);
tcp_conns[i].make_connect = make_connect;
}

return 0;
}


static int pipe_do_connect(int num, void* conns, void* arg) {
pipe_conn_rec* pe;
pipe_conn_rec* p;
int r;

for (p = pipe_conns, pe = p + num; p < pe; p++) {
r = uv_pipe_connect(&p->conn_req, &p->stream, TEST_PIPENAME, connect_cb);
ASSERT(r == 0);
static int pipe_do_connect(int num, make_connect_fn make_connect, void* arg) {
int i;

p->conn_req.data = p;
for (i = 0; i < num; i++) {
pipe_make_connect((conn_rec*)&pipe_conns[i]);
pipe_conns[i].make_connect = make_connect;
}

return 0;
Expand All @@ -250,9 +266,9 @@ static int pound_it(int concurrency,
const char* type,
setup_fn do_setup,
connect_fn do_connect,
make_connect_fn make_connect,
void* arg) {
double secs;
void* state;
int r;
uint64_t start_time; /* in ns */
uint64_t end_time;
Expand All @@ -265,10 +281,9 @@ static int pound_it(int concurrency,
/* Run benchmark for at least five seconds. */
start_time = uv_hrtime();

state = do_setup(concurrency, arg);
ASSERT(state != NULL);
do_setup(concurrency, arg);

r = do_connect(concurrency, state, arg);
r = do_connect(concurrency, make_connect, arg);
ASSERT(!r);

uv_run();
Expand All @@ -289,20 +304,20 @@ static int pound_it(int concurrency,


BENCHMARK_IMPL(tcp4_pound_100) {
return pound_it(100, "tcp", tcp_do_setup, tcp_do_connect, NULL);
return pound_it(100, "tcp", tcp_do_setup, tcp_do_connect, tcp_make_connect, NULL);
}


BENCHMARK_IMPL(tcp4_pound_1000) {
return pound_it(1000, "tcp", tcp_do_setup, tcp_do_connect, NULL);
return pound_it(1000, "tcp", tcp_do_setup, tcp_do_connect, tcp_make_connect, NULL);
}


BENCHMARK_IMPL(pipe_pound_100) {
return pound_it(100, "pipe", pipe_do_setup, pipe_do_connect, NULL);
return pound_it(100, "pipe", pipe_do_setup, pipe_do_connect, pipe_make_connect, NULL);
}


BENCHMARK_IMPL(pipe_pound_1000) {
return pound_it(1000, "pipe", pipe_do_setup, pipe_do_connect, NULL);
return pound_it(1000, "pipe", pipe_do_setup, pipe_do_connect, pipe_make_connect, NULL);
}

0 comments on commit dcf8770

Please sign in to comment.