/*
* Pump data to the server and measures the time
* Author: Catalin(ux) M. BOIE
*/
#define _GNU_SOURCE
#include <Conn.h>
/* Global variables */
static unsigned short debug = 11;
static unsigned int ipv4conns = 0, ipv6conns = 2;
static int port = 9000;
static unsigned int buf_size = 4096 * 1;
static unsigned int ends = 0;
static unsigned char *buf;
static unsigned long long alloc_errors = 0;
static unsigned long long commit_errors = 0;
static unsigned long long enqueue_errors = 0;
static unsigned long long conn_errors = 0;
static unsigned long long bytes = 0;
static unsigned long long latency = 0;
static void c_connected(struct Conn *C)
{
int amount;
amount = Conn_enqueue(C, buf, buf_size);
if (amount == -1) {
printf("Cannot enqueue whole buffer (%d/%d)!\n",
amount, buf_size);
enqueue_errors++;
} else {
bytes += amount;
}
Conn_close(C);
}
static void c_error(struct Conn *C)
{
printf("%s id=%llu [%s]\n",
__FUNCTION__, Conn_getid(C), Conn_strerror());
conn_errors++;
}
static void c_close(struct Conn *C)
{
latency += Conn_lifetime(C);
}
int main(void)
{
int ret;
struct timeval start, end;
unsigned int elap;
unsigned int i;
struct Conn *C;
buf = malloc(buf_size);
if (!buf) {
printf("Cannnot alloc %u bytes!\n", buf_size);
return 1;
}
memset(buf, 0, buf_size);
Conn_debug(NULL, debug);
printf("Starting...\n");
printf("Port=%d\n", port);
printf("Debug=%d\n", debug);
printf("buf_size=%d\n", buf_size);
printf("sys: %s\n", Conn_sys());
Conn_default_ibuf = 4096;
Conn_default_obuf = 4096;
ret = Conn_init(0);
if (ret == -1) {
printf("%s\n", Conn_strerror());
return 1;
}
Conn_connected_cb = c_connected;
Conn_error_cb = c_error;
Conn_close_cb = c_close;
gettimeofday(&start, NULL);
ends = 0;
/* ipv4 */
for (i = 0; i < ipv4conns; i++) {
C = Conn_alloc();
if (!C) {
printf("Error alloc Conn struct [%s]!\n",
Conn_strerror());
alloc_errors++;
} else {
Conn_set_socket_domain(C, PF_INET);
Conn_set_socket_type(C, SOCK_STREAM);
Conn_set_socket_addr(C, "127.0.0.1");
Conn_set_socket_port(C, port);
ret = Conn_commit(C);
if (ret != 0) {
printf("Error commiting [%s]!\n",
Conn_strerror());
commit_errors++;
}
}
}
/* ipv6 */
for (i = 0; i < ipv6conns; i++) {
C = Conn_alloc();
if (!C) {
printf("Error alloc Conn struct [%s]!\n",
Conn_strerror());
alloc_errors++;
} else {
Conn_set_socket_domain(C, PF_INET6);
Conn_set_socket_type(C, SOCK_STREAM);
Conn_set_socket_addr(C, "::1");
Conn_set_socket_port(C, port);
ret = Conn_commit(C);
if (ret != 0) {
printf("Error commiting [%s]!\n",
Conn_strerror());
commit_errors++;
}
}
}
while (1) {
ret = Conn_poll(-1);
if (ret == -1) {
printf("Error in poll [%s]!\n",
Conn_strerror());
break;
} else if (ret == 0)
break;
printf("%d event(s), Conn_no=%u.\n",
ret, Conn_no);
}
free(buf);
gettimeofday(&end, NULL);
printf("\n");
elap = 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;
printf("Finish in %d.%06d ipv4conns=%d ipv6conns=%d total=%d ends=%d"
" Conns per sec=%d\n"
"alloc_errors=%llu commit_errors=%llu"
" conn_errors=%llu enqueue_errors=%llu\n"
"bytes=%llu latency=%llums"
"\n",
elap / 1000000, elap % 1000000,
ipv4conns, ipv6conns, ipv4conns + ipv6conns,
ends, (ipv4conns + ipv6conns) / (elap / 1000000 + 1),
alloc_errors, commit_errors, conn_errors, enqueue_errors,
bytes, latency);
Conn_shutdown();
return 0;
}