xref: /haiku/src/tests/system/network/test2.c (revision 21258e2674226d6aa732321b6f8494841895af5f)
1 #include <stdio.h>
2 #include <kernel/OS.h>
3 #include <string.h>
4 #include <sys/time.h>
5 #include <malloc.h>
6 
7 #include "sys/socket.h"
8 #include "netinet/in.h"
9 #include "arpa/inet.h"
10 #include "sys/select.h"
11 
12 #include "ufunc.h"
13 
14 #define THREADS	2
15 #define TIME	10
16 
17 
18 int32 test_thread(void *data)
19 {
20 	int tnum = *(int*)data;
21 	int sock = 0;
22 	uint32 num = 0;
23 	int rv;
24 	struct sockaddr_in sa;
25 	bigtime_t tn;
26 
27 	sa.sin_len = sizeof(sa);
28 	sa.sin_port = 0;
29 	sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
30 	sa.sin_family = AF_INET;
31 	memset(&sa.sin_zero, 0, sizeof(sa.sin_zero));
32 
33 	printf("Thread %d, starting test...\n", tnum + 1);
34 
35 	tn = real_time_clock();
36 
37 	while (real_time_clock() - tn <= TIME) {
38 		sock = socket(AF_INET, SOCK_DGRAM , 0);
39 		if (sock < 0)
40 			err(sock, "Socket couldn't be created");
41 		rv = bind(sock, (struct sockaddr *)&sa, sizeof(sa));
42 		if (rv < 0)
43 			err(rv, "Socket could not be bound to an ephemereal port");
44 		closesocket(sock);
45 		num++;
46 	}
47 
48 	printf( "Thread %d:\n"
49 		"       sockets created : %5ld\n"
50 		"       test time       : %5d seconds\n"
51 		"       average         : %5ld sockets/sec\n",
52 		tnum + 1, num, TIME, num / TIME);
53 }
54 
55 int main(int argc, char **argv)
56 {
57 	thread_id t[THREADS];
58 	int i;
59 	status_t retval;
60 
61 	test_banner("Socket creation and bind() test");
62 
63 	for (i=0;i<THREADS;i++) {
64 		t[i] = spawn_thread(test_thread, "socket test thread",
65 			B_NORMAL_PRIORITY, &i);
66 		if (t[i] >= 0)
67 			resume_thread(t[i]);
68 	}
69 
70 	for (i=0;i<THREADS;i++) {
71 		wait_for_thread(t[i], &retval);
72 	}
73 
74 	return (0);
75 }
76 
77 
78