summaryrefslogtreecommitdiff
path: root/code/test/stress.c
blob: 6bc5a64636cfe30502156a2d64ca0a698ffe373d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#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 CTYPE_TEST  0
#define MTYPE_MSG   8

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);
    ECPBuffer packet;
    ECPBuffer payload;
    unsigned char pkt_buf[ECP_MAX_PKT];
    unsigned char pld_buf[ECP_MAX_PLD];
    
    packet.buffer = pkt_buf;
    packet.size = ECP_MAX_PKT;
    payload.buffer = pld_buf;
    payload.size = ECP_MAX_PLD;
    
    printf("OPEN:%d\n", idx);
    while(1) {
        uint32_t rnd;
        c->sock->ctx->rng(&rnd, sizeof(uint32_t));
        usleep(rnd % (2000000/msg_rate));

        ecp_pld_set_type(pld_buf, MTYPE_MSG);
        ssize_t _rv = 0;
        // XXX refactor
        // _rv = ecp_pld_send(c, &packet, &payload, ECP_SIZE_PLD(1000, 0));
        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 *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
    int idx = (int)(conn->conn_data);
    int rv = 0;
    
    ecp_conn_handle_open(conn, sq, t, p, s, b);
    rv = pthread_create(&s_thd[idx], NULL, (void *(*)(void *))sender, (void *)conn);
    if (rv) {
        char msg[256];
        sprintf(msg, "THD %d CREATE\n", idx);
        perror(msg);
        exit(1);
    }
    return s;
}

ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
    int idx = (int)(conn->conn_data);
    ECPBuffer packet;
    ECPBuffer payload;
    unsigned char pkt_buf[ECP_MAX_PKT];
    unsigned char pld_buf[ECP_MAX_PLD];
    
    packet.buffer = pkt_buf;
    packet.size = ECP_MAX_PKT;
    payload.buffer = pld_buf;
    payload.size = ECP_MAX_PLD;

    if (c_start) {
        pthread_mutex_lock(&t_mtx[idx]);
        t_rcvd[idx]++;
        pthread_mutex_unlock(&t_mtx[idx]);
    }
    
    // ecp_pld_set_type(payload, MTYPE_MSG);
    // ssize_t _rv = ecp_pld_send(c, &packet, &payload, ECP_SIZE_PLD(1000, 0));
    return s;
}

ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
    ECPBuffer packet;
    ECPBuffer payload;
    unsigned char pkt_buf[ECP_MAX_PKT];
    unsigned char pld_buf[ECP_MAX_PLD];
    
    packet.buffer = pkt_buf;
    packet.size = ECP_MAX_PKT;
    payload.buffer = pld_buf;
    payload.size = ECP_MAX_PLD;

    ecp_pld_set_type(pld_buf, MTYPE_MSG);
    // XXX refactor
    // ssize_t _rv = ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(1000, 0));
    return s;
}

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_conn_handler_init(&handler_s);
    handler_s.msg[MTYPE_MSG] = handle_msg_s;
    ctx_s.handler[CTYPE_TEST] = &handler_s;

    if (!rv) rv = ecp_dhkey_generate(&ctx_s, &key_perma_s);

    for (i=0; i<num_s; i++) {
        if (!rv) rv = ecp_sock_create(&sock_s[i], &ctx_s, &key_perma_s);

        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_handler_init(&handler_c);

    handler_c.msg[ECP_MTYPE_OPEN] = handle_open_c;
    handler_c.msg[MTYPE_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]);
        ctx_c[i].handler[CTYPE_TEST] = &handler_c;
        
        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], &key_perma_s.public, addr);
        
        if (!rv) rv = ecp_conn_create(&conn[i], &sock_c[i], CTYPE_TEST);
        conn[i].conn_data = (void *)i;
        
        if (!rv) rv = ecp_conn_open(&conn[i], &node[i]);
        
        if (rv) {
            char msg[256];
            sprintf(msg, "CLIENT %d CREATE:%d\n", i, rv);
            perror(msg);
            exit(1);
        }
        
    }
    while (1) sleep(1);
}