I am trying to evaluate the performance of libuv because of the promise that it can manage 100,000 of TCP sockets and can generate 10,000 of new TCP sessions/second.
Created the following code snippet based on a gist to validate the performance of libuv. However, when it runs against a server, it's surprisingly slow according to wireshark pcap capture
- TCP handshakes were done one after another, I was expecting TCP SYNs to be sent in parallel
- The TCP data was sent after all the sessions were established. I had expected the TCP data to be sent as soon as the TCP handshake is complete.
This test was run on Ubuntu 14.04 (64bit core i7 cpu).
Not sure if there is problem with libuv or with this code snippet.
I know this has some memory leaks, but it doesn't matter since I am only doing evaluation.
#include <stdio.h>
#include <stdlib.h>
#include <uv.h>
//based on https://gist.githubusercontent.com/snatchev/5255976/
//raw/8392c42d719bb775053036e32b21affdf932c1b7/libuv-tcp-client.c
static void on_close(uv_handle_t* handle);
static void on_connect(uv_connect_t* req, int status);
static void on_write(uv_write_t* req, int status);
static uv_loop_t *loop;
static uv_buf_t alloc_cb(uv_handle_t* handle, size_t size) {
return uv_buf_init(malloc(size), size);
}
void on_close(uv_handle_t* handle)
{
printf("closed.");
}
void on_write(uv_write_t* req, int status)
{
if (status) {
uv_err_t err = uv_last_error(loop);
fprintf(stderr, "uv_write error: %s\n", uv_strerror(err));
return;
}
printf("wrote.\n");
free(req);
//uv_close((uv_handle_t*)req->handle, on_close);
}
void on_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf)
{
printf("on_read. %p\n",tcp);
if(nread >= 0) {
//printf("read: %s\n", tcp->data);
printf("read: %s\n", buf.base);
}
else {
//we got an EOF
uv_close((uv_handle_t*)tcp, on_close);
}
//cargo-culted
free(buf.base);
}
void write2(uv_stream_t* stream, char *data, int len2) {
uv_buf_t buffer[] = {
{.base = data, .len = len2}
};
uv_write_t *req = malloc(sizeof(uv_write_t));
uv_write(req, stream, buffer, 1, on_write);
}
void on_connect(uv_connect_t* connection, int status)
{
if (status < 0) {
printf("failed to connect\n"); return;
}
printf("connected. %p %d\n",connection, status);
uv_stream_t* stream = connection->handle;
free(connection);
write2(stream, "echo world!", 12);
uv_read_start(stream, alloc_cb, on_read);
}
void startConn(char *host, int port) {
uv_tcp_t *pSock = malloc(sizeof(uv_tcp_t));
uv_tcp_init(loop, pSock);
uv_tcp_keepalive(pSock, 1, 60);
struct sockaddr_in dest = uv_ip4_addr(host, port);
uv_connect_t *pConn = malloc(sizeof(uv_connect_t));
printf("allocated %p\n", pConn);
uv_tcp_connect(pConn, pSock, dest, on_connect);
}
int main(int argc, char **argv) {
loop = uv_default_loop();
int i;
for (i=0; i<10; i++)
startConn("0.0.0.0", 1234);
uv_run(loop, UV_RUN_DEFAULT);
}