added missing includes
[strongswan.git] / src / charon / plugins / unit_tester / tests / test_pool.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <sys/time.h>
17 #include <time.h>
18 #include <pthread.h>
19
20 #include <library.h>
21 #include <daemon.h>
22
23 #define ALLOCS 1000
24 #define THREADS 20
25
26 static void* testing(void *thread)
27 {
28 int i;
29 auth_info_t *auth;
30 host_t *addr[ALLOCS];
31 identification_t *id[ALLOCS];
32
33
34 auth = auth_info_create();
35
36 /* prepare identities */
37 for (i = 0; i < ALLOCS; i++)
38 {
39 char buf[256];
40
41 snprintf(buf, sizeof(buf), "%d-%d@strongswan.org", (int)thread, i);
42 id[i] = identification_create_from_string(buf);
43 if (!id[i])
44 {
45 return (void*)FALSE;
46 }
47 }
48
49 /* allocate addresses */
50 for (i = 0; i < ALLOCS; i++)
51 {
52 addr[i] = charon->attributes->acquire_address(charon->attributes,
53 "test", id[i], auth, NULL);
54 if (!addr[i])
55 {
56 return (void*)FALSE;
57 }
58 }
59
60 /* release addresses */
61 for (i = 0; i < ALLOCS; i++)
62 {
63 charon->attributes->release_address(charon->attributes, "test", addr[i]);
64 }
65
66 /* cleanup */
67 for (i = 0; i < ALLOCS; i++)
68 {
69 addr[i]->destroy(addr[i]);
70 id[i]->destroy(id[i]);
71 }
72 auth->destroy(auth);
73 return (void*)TRUE;
74 }
75
76
77 /*******************************************************************************
78 * SQL pool performance test
79 ******************************************************************************/
80 bool test_pool()
81 {
82 int i;
83 void *res;
84 pthread_t thread[THREADS];
85
86 for (i = 0; i < THREADS; i++)
87 {
88 if (pthread_create(&thread[i], NULL, (void*)testing, (void*)i) < 0)
89 {
90 return FALSE;
91 }
92 }
93 for (i = 0; i < THREADS; i++)
94 {
95 pthread_join(thread[i], &res);
96 if (res == NULL)
97 {
98 return FALSE;
99 }
100 }
101 return TRUE;
102 }
103