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