diff options
author | Uros Majstorovic <majstor@majstor.org> | 2017-05-03 21:10:08 +0200 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2017-05-03 21:10:08 +0200 |
commit | 25de5e761daab8b897a4f09ff8503e6f43c299f9 (patch) | |
tree | a9a1c185c7f89d67f9250e42b2aa53eefc9a6770 /code/test |
initial commit
Diffstat (limited to 'code/test')
-rw-r--r-- | code/test/Makefile | 18 | ||||
-rw-r--r-- | code/test/client.c | 0 | ||||
-rw-r--r-- | code/test/server.c | 117 | ||||
-rw-r--r-- | code/test/stress.c | 223 |
4 files changed, 358 insertions, 0 deletions
diff --git a/code/test/Makefile b/code/test/Makefile new file mode 100644 index 0000000..7992127 --- /dev/null +++ b/code/test/Makefile @@ -0,0 +1,18 @@ +CFLAGS=-I.. -O3 -Wno-int-to-void-pointer-cast +LDFLAGS=-lm -pthread +dep=../libecpcore.a ../crypto/libecpcr.a ../htable/libecpht.a ../posix/libecptr.a ../posix/libecptm.a + +%.o: %.c + $(CC) $(CFLAGS) -c $< + +all: server stress + +server: server.o $(dep) + $(CC) $< $(dep) $(LDFLAGS) -o $@ + +stress: stress.o $(dep) + $(CC) $< $(dep) $(LDFLAGS) -o $@ + +clean: + rm -f *.o + rm -f server stress diff --git a/code/test/client.c b/code/test/client.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/code/test/client.c diff --git a/code/test/server.c b/code/test/server.c new file mode 100644 index 0000000..a03267e --- /dev/null +++ b/code/test/server.c @@ -0,0 +1,117 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include <core.h> + +ECPContext ctx_s; +ECPSocket sock_s; +ECPDHKey key_perma_s; +ECPConnHandler handler_s; + +ECPContext ctx_c; +ECPSocket sock_c; +ECPDHKey key_perma_c; +ECPConnHandler handler_c; + +ECPNode node; +ECPConnection conn; + +#define PTYPE_MSG 16 + +ssize_t handle_open_c(ECPConnection *c, unsigned char t, unsigned char *p, ssize_t s) { + uint32_t seq = 0; + + if (s < 0) { + printf("OPEN ERR:%ld\n", s); + return 0; + } + + unsigned char payload[ECP_SIZE_PLD(1000)]; + unsigned char *buf = ecp_pld_get_buf(payload); + char *msg = "PERA JE CAR!"; + + strcpy((char *)buf, msg); + ssize_t _rv = ecp_send(c, PTYPE_MSG, payload, 1000); + return 0; +} + +ssize_t handle_msg_c(ECPConnection *c, unsigned char t, unsigned char *p, ssize_t s) { + printf("MSG C:%s size:%ld\n", p, s); + + return s; +} + +ssize_t handle_msg_s(ECPConnection *c, unsigned char t, unsigned char *p, ssize_t s) { + printf("MSG S:%s size:%ld\n", p, s); + + unsigned char payload[ECP_SIZE_PLD(1000)]; + unsigned char *buf = ecp_pld_get_buf(payload); + char *msg = "VAISTINU JE CAR!"; + + strcpy((char *)buf, msg); + ssize_t _rv = ecp_send(c, PTYPE_MSG, payload, 1000); + + return s; +} + +int conn_create(ECPConnection *c, unsigned char *p, size_t s) { + c->handler = &handler_s; + return ECP_OK; +} + +int main(int argc, char *argv[]) { + int rv; + + rv = ecp_init(&ctx_s); + printf("ecp_init RV:%d\n", rv); + + rv = ecp_dhkey_generate(&ctx_s, &key_perma_s); + printf("ecp_dhkey_generate RV:%d\n", rv); + + rv = ecp_sock_create(&sock_s, &ctx_s, &key_perma_s); + printf("ecp_sock_create RV:%d\n", rv); + + rv = ecp_conn_hander_init(&handler_s); + + handler_s.f[PTYPE_MSG] = handle_msg_s; + sock_s.conn_create = conn_create; + + rv = ecp_sock_open(&sock_s, "0.0.0.0:3000"); + printf("ecp_sock_open RV:%d\n", rv); + + rv = ecp_start_receiver(&sock_s); + printf("ecp_start_receiver RV:%d\n", rv); + + rv = ecp_init(&ctx_c); + printf("ecp_init RV:%d\n", rv); + + rv = ecp_dhkey_generate(&ctx_c, &key_perma_c); + printf("ecp_dhkey_generate RV:%d\n", rv); + + rv = ecp_sock_create(&sock_c, &ctx_c, &key_perma_c); + printf("ecp_sock_create RV:%d\n", rv); + + rv = ecp_sock_open(&sock_c, NULL); + printf("ecp_sock_open RV:%d\n", rv); + + rv = ecp_start_receiver(&sock_c); + printf("ecp_start_receiver RV:%d\n", rv); + + rv = ecp_node_init(&ctx_c, &node, "127.0.0.1:3000", &key_perma_s.public); + printf("ecp_node_init RV:%d\n", rv); + + rv = ecp_conn_create(&conn, &sock_c); + printf("ecp_conn_create RV:%d\n", rv); + + rv = ecp_conn_hander_init(&handler_c); + printf("ecp_conn_hander_init RV:%d\n", rv); + + handler_c.f[ECP_PTYPE_OPEN] = handle_open_c; + handler_c.f[PTYPE_MSG] = handle_msg_c; + + rv = ecp_conn_open(&conn, &node, &handler_c); + printf("ecp_conn_open RV:%d\n", rv); + + while (1) sleep(1); +}
\ No newline at end of file diff --git a/code/test/stress.c b/code/test/stress.c new file mode 100644 index 0000000..c278af1 --- /dev/null +++ b/code/test/stress.c @@ -0,0 +1,223 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include <unistd.h> +#include <signal.h> +#include <sys/time.h> +#include <pthread.h> + +#include <core.h> + +#ifdef __linux__ +#define SIGINFO SIGTSTP +#endif + +#define NUM_S 32 +#define NUM_C 256 +#define MSG_RATE 65 + +#define PTYPE_MSG 16 + +ECPContext ctx_s; +ECPDHKey key_perma_s; +ECPConnHandler handler_s; +ECPSocket *sock_s; + +ECPConnHandler handler_c; +ECPContext *ctx_c; +ECPSocket *sock_c; +ECPDHKey *key_perma_c; + +ECPNode *node; +ECPConnection *conn; + +pthread_t *s_thd; +pthread_t *r_thd; +pthread_mutex_t *t_mtx; +int *t_sent, *t_rcvd; + +uint64_t t_start = 0, t_end = 0; +int c_start = 0; + +int num_s = NUM_S, num_c = NUM_C; +int msg_rate = MSG_RATE; + +static void display(void) { + int i, s = 0, r = 0; + + for (i=0; i<num_c; i++) { + pthread_mutex_lock(&t_mtx[i]); + s += t_sent[i]; + r += t_rcvd[i]; + pthread_mutex_unlock(&t_mtx[i]); + } + + uint64_t t_time = t_end - t_start; + + printf("TOTAL SENT:%d RCVD:%d\n", s, r); + printf("L:%f%%\n", (s-r)/(float)s*100); + printf("T/S S:%f R:%f\n", s/((float)t_time/1000000), r/((float)t_time/1000000)); +} + +static void catchINFO(int sig) { + struct timeval tv; + + if (!c_start) { + gettimeofday(&tv, NULL); + t_start = tv.tv_sec*(uint64_t)1000000+tv.tv_usec; + c_start = 1; + printf("COUNTER START\n"); + } else { + gettimeofday(&tv, NULL); + t_end = tv.tv_sec*(uint64_t)1000000+tv.tv_usec; + display(); + } +} + +void *sender(ECPConnection *c) { + int idx = (int)(c->conn_data); + unsigned char payload[ECP_SIZE_PLD(1000)]; + + printf("OPEN:%d\n", idx); + while(1) { + uint32_t rnd; + c->sock->ctx->rng(&rnd, sizeof(uint32_t)); + usleep(rnd % (2000000/msg_rate)); + + ssize_t _rv = ecp_send(c, PTYPE_MSG, payload, 1000); + if (c_start && (_rv > 0)) { + pthread_mutex_lock(&t_mtx[idx]); + t_sent[idx]++; + pthread_mutex_unlock(&t_mtx[idx]); + } + } +} + +ssize_t handle_open_c(ECPConnection *c, unsigned char t, unsigned char *p, ssize_t s) { + int idx = (int)(c->conn_data); + int rv = pthread_create(&s_thd[idx], NULL, (void *(*)(void *))sender, (void *)c); + if (rv) { + char msg[256]; + sprintf(msg, "THD %d CREATE\n", idx); + perror(msg); + exit(1); + } + return 0; +} + +ssize_t handle_msg_c(ECPConnection *c, unsigned char t, unsigned char *p, ssize_t s) { + int idx = (int)(c->conn_data); + unsigned char payload[ECP_SIZE_PLD(1000)]; + + if (c_start) { + pthread_mutex_lock(&t_mtx[idx]); + t_rcvd[idx]++; + pthread_mutex_unlock(&t_mtx[idx]); + } + + // ssize_t _rv = ecp_send(c, PTYPE_MSG, payload, 1000); + return s; +} + +ssize_t handle_msg_s(ECPConnection *c, unsigned char t, unsigned char *p, ssize_t s) { + unsigned char payload[ECP_SIZE_PLD(1000)]; + ssize_t _rv = ecp_send(c, PTYPE_MSG, payload, 1000); + return s; +} + +int conn_create(ECPConnection *c, unsigned char *p, size_t s) { + c->handler = &handler_s; + return ECP_OK; +} + +int main(int argc, char *argv[]) { + char addr[256]; + int rv; + int i; + + ECPConnHandler handler_c; + + ECPContext *ctx_c; + ECPSocket *sock_c; + ECPDHKey *key_perma_c; + + ECPNode *node; + ECPConnection *conn; + + sock_s = malloc(num_s * sizeof(ECPSocket)); + ctx_c = malloc(num_c * sizeof(ECPContext)); + sock_c = malloc(num_c * sizeof(ECPSocket)); + key_perma_c = malloc(num_c * sizeof(ECPDHKey)); + node = malloc(num_c * sizeof(ECPNode)); + conn = malloc(num_c * sizeof(ECPConnection)); + + s_thd = malloc(num_c * sizeof(pthread_t)); + r_thd = malloc(num_c * sizeof(pthread_t)); + t_mtx = malloc(num_c * sizeof(pthread_mutex_t)); + t_sent = malloc(num_c * sizeof(int)); + t_rcvd = malloc(num_c * sizeof(int)); + memset(t_rcvd, 0, num_c * sizeof(int)); + memset(t_sent, 0, num_c * sizeof(int)); + + struct sigaction actINFO; + memset(&actINFO, 0, sizeof(actINFO)); + actINFO.sa_handler = &catchINFO; + sigaction(SIGINFO, &actINFO, NULL); + + rv = ecp_init(&ctx_s); + if (!rv) rv = ecp_dhkey_generate(&ctx_s, &key_perma_s); + if (!rv) rv = ecp_conn_hander_init(&handler_s); + handler_s.f[PTYPE_MSG] = handle_msg_s; + + for (i=0; i<num_s; i++) { + if (!rv) rv = ecp_sock_create(&sock_s[i], &ctx_s, &key_perma_s); + + sock_s[i].conn_create = conn_create; + strcpy(addr, "0.0.0.0:"); + sprintf(addr+strlen(addr), "%d", 3000+i); + if (!rv) rv = ecp_sock_open(&sock_s[i], addr); + + if (!rv) rv = pthread_create(&r_thd[i], NULL, (void *(*)(void *))ecp_receiver, (void *)&sock_s[i]); + + if (rv) { + char msg[256]; + sprintf(msg, "SERVER %d CREATE:%d\n", i, rv); + perror(msg); + exit(1); + } + } + + rv = ecp_conn_hander_init(&handler_c); + + handler_c.f[ECP_PTYPE_OPEN] = handle_open_c; + handler_c.f[PTYPE_MSG] = handle_msg_c; + + for (i=0; i<num_c; i++) { + pthread_mutex_init(&t_mtx[i], NULL); + + if (!rv) rv = ecp_init(&ctx_c[i]); + if (!rv) rv = ecp_dhkey_generate(&ctx_c[i], &key_perma_c[i]); + if (!rv) rv = ecp_sock_create(&sock_c[i], &ctx_c[i], &key_perma_c[i]); + if (!rv) rv = ecp_sock_open(&sock_c[i], NULL); + + if (!rv) rv = pthread_create(&r_thd[i], NULL, (void *(*)(void *))ecp_receiver, (void *)&sock_c[i]); + + strcpy(addr, "127.0.0.1:"); + sprintf(addr+strlen(addr), "%d", 3000+(i % num_s)); + if (!rv) rv = ecp_node_init(&ctx_c[i], &node[i], addr, &key_perma_s.public); + + if (!rv) rv = ecp_conn_create(&conn[i], &sock_c[i]); + conn[i].conn_data = (void *)i; + + if (!rv) rv = ecp_conn_open(&conn[i], &node[i], &handler_c); + + if (rv) { + char msg[256]; + sprintf(msg, "CLIENT %d CREATE:%d\n", i, rv); + perror(msg); + exit(1); + } + + } + while (1) sleep(1); +}
\ No newline at end of file |