pool performance testing
[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
19 #include <library.h>
20 #include <daemon.h>
21
22 #define ALLOCS 1000
23 #define THREADS 20
24
25 static void* testing(void *thread)
26 {
27 int i;
28 auth_info_t *auth;
29 host_t *addr[ALLOCS];
30 identification_t *id[ALLOCS];
31
32
33 auth = auth_info_create();
34
35 /* prepare identities */
36 for (i = 0; i < ALLOCS; i++)
37 {
38 char buf[256];
39
40 snprintf(buf, sizeof(buf), "%d-%d@strongswan.org", (int)thread, i);
41 id[i] = identification_create_from_string(buf);
42 if (!id[i])
43 {
44 return (void*)FALSE;
45 }
46 }
47
48 /* allocate addresses */
49 for (i = 0; i < ALLOCS; i++)
50 {
51 addr[i] = charon->attributes->acquire_address(charon->attributes,
52 "test", id[i], auth, NULL);
53 if (!addr[i])
54 {
55 return (void*)FALSE;
56 }
57 }
58
59 /* release addresses */
60 for (i = 0; i < ALLOCS; i++)
61 {
62 charon->attributes->release_address(charon->attributes, "test", addr[i]);
63 }
64
65 /* cleanup */
66 for (i = 0; i < ALLOCS; i++)
67 {
68 addr[i]->destroy(addr[i]);
69 id[i]->destroy(id[i]);
70 }
71 auth->destroy(auth);
72 return (void*)TRUE;
73 }
74
75
76 /*******************************************************************************
77 * SQL pool performance test
78 ******************************************************************************/
79 bool test_pool()
80 {
81 int i;
82 void *res;
83 pthread_t thread[THREADS];
84
85 for (i = 0; i < THREADS; i++)
86 {
87 if (pthread_create(&thread[i], NULL, (void*)testing, (void*)i) < 0)
88 {
89 return FALSE;
90 }
91 }
92 for (i = 0; i < THREADS; i++)
93 {
94 pthread_join(thread[i], &res);
95 if (res == NULL)
96 {
97 return FALSE;
98 }
99 }
100 return TRUE;
101 }
102