aboutsummaryrefslogtreecommitdiff
path: root/tests/scalability.c
blob: 38137e90c81a1341e3236bc6fa989fc0d61e077b (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
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#include "convey.h"
#include "nng.h"

#include "protocol/reqrep0/rep.h"
#include "protocol/reqrep0/req.h"
#include "stubs.h"
#include "supplemental/util/platform.h"

#include <string.h>

static int nclients = 200;

static char *addr = "inproc:///atscale";
nng_socket   rep;
nng_thread * server;

void
serve(void *arg)
{
	nng_msg *msg;
	(void) arg; // unused

	for (;;) {
		msg = NULL;
		if ((nng_recvmsg(rep, &msg, 0) != 0) ||
		    (nng_sendmsg(rep, msg, 0) != 0)) {
			break;
		}
	}
	if (msg != NULL) {
		nng_msg_free(msg);
	}
	nng_close(rep);
}

void
stop(void)
{
	nng_closeall();
	nng_thread_destroy(server);
	nng_fini();
}

int
openclients(nng_socket *clients, int num)
{
	int          rv;
	int          i;
	nng_duration t;
	for (i = 0; i < num; i++) {
		t = 100; // 100ms
		nng_socket c;
		if (((rv = nng_req_open(&c)) != 0) ||
		    ((rv = nng_setopt_ms(c, NNG_OPT_RECVTIMEO, t)) != 0) ||
		    ((rv = nng_setopt_ms(c, NNG_OPT_SENDTIMEO, t)) != 0) ||
		    ((rv = nng_dial(c, addr, NULL, 0)) != 0)) {
			return (rv);
		}
		clients[i] = c;
	}
	return (0);
}

int
transact(nng_socket *clients, int num)
{
	nng_msg *msg;
	int      rv;
	int      i;

	for (i = 0; i < num; i++) {

		if (((rv = nng_msg_alloc(&msg, 0)) != 0) ||
		    ((rv = nng_sendmsg(clients[i], msg, 0)) != 0) ||
		    ((rv = nng_recvmsg(clients[i], &msg, 0)) != 0)) {
			// We may leak a message, but this is an
			// error case anyway.
			break;
		}
		nng_msg_free(msg);
		msg = NULL;
	}
	return (rv);
}

Main({
	nng_socket *clients;
	int *       results;

	atexit(stop);

	clients = calloc(nclients, sizeof(nng_socket));
	results = calloc(nclients, sizeof(int));

	if ((nng_rep_open(&rep) != 0) ||
	    (nng_setopt_int(rep, NNG_OPT_RECVBUF, 256) != 0) ||
	    (nng_setopt_int(rep, NNG_OPT_SENDBUF, 256) != 0) ||
	    (nng_listen(rep, addr, NULL, 0) != 0) ||
	    (nng_thread_create(&server, serve, NULL) != 0)) {
		fprintf(stderr, "Unable to set up server!\n");
		exit(1);
	}

	Test("Scalability", {
		Convey("We can handle many many clients", {
			int i;
			So(openclients(clients, nclients) == 0);
			So(transact(clients, nclients) == 0);
			for (i = 0; i < nclients; i++) {
				So(nng_close(clients[i]) == 0);
			}
		});
	});

	free(clients);
	free(results);
})