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