Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/pool_perf.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "test.h"
20
21 #if INCLUDE_POOL_PERF_TEST
22
23 #include <pjlib.h>
24 #include <pj/compat/malloc.h>
25
26 #if !PJ_HAS_HIGH_RES_TIMER
27 # error Need high resolution timer for this test.
28 #endif
29
30 #define THIS_FILE "test"
31
32 #define LOOP 10
33 #define COUNT 1024
34 static unsigned sizes[COUNT];
35 static char *p[COUNT];
36 #define MIN_SIZE 4
37 #define MAX_SIZE 512
38 static unsigned total_size;
39
40
41 static int pool_test_pool()
42 {
43 int i;
44 pj_pool_t *pool = pj_pool_create(mem, NULL, total_size + 4*COUNT, 0, NULL);
45 if (!pool)
46 return -1;
47
48 for (i=0; i<COUNT; ++i) {
49 char *ptr;
50 if ( (ptr=(char*)pj_pool_alloc(pool, sizes[i])) == NULL) {
51 PJ_LOG(3,(THIS_FILE," error: pool failed to allocate %d bytes",
52 sizes[i]));
53 pj_pool_release(pool);
54 return -1;
55 }
56 *ptr = '\0';
57 }
58
59 pj_pool_release(pool);
60 return 0;
61 }
62
63 /* Symbian doesn't have malloc()/free(), so we use new/delete instead */
64 //#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
65 #if 0
66 static int pool_test_malloc_free()
67 {
68 int i; /* must be signed */
69
70 for (i=0; i<COUNT; ++i) {
71 p[i] = new char[sizes[i]];
72 if (!p[i]) {
73 PJ_LOG(3,(THIS_FILE," error: malloc failed to allocate %d bytes",
74 sizes[i]));
75 --i;
76 while (i >= 0) {
77 delete [] p[i];
78 --i;
79 }
80 return -1;
81 }
82 *p[i] = '\0';
83 }
84
85 for (i=0; i<COUNT; ++i) {
86 delete [] p[i];
87 }
88
89 return 0;
90 }
91
92 #else /* PJ_SYMBIAN */
93
94 static int pool_test_malloc_free()
95 {
96 int i; /* must be signed */
97
98 for (i=0; i<COUNT; ++i) {
99 p[i] = (char*)malloc(sizes[i]);
100 if (!p[i]) {
101 PJ_LOG(3,(THIS_FILE," error: malloc failed to allocate %d bytes",
102 sizes[i]));
103 --i;
104 while (i >= 0)
105 free(p[i]), --i;
106 return -1;
107 }
108 *p[i] = '\0';
109 }
110
111 for (i=0; i<COUNT; ++i) {
112 free(p[i]);
113 }
114
115 return 0;
116 }
117
118 #endif /* PJ_SYMBIAN */
119
120 int pool_perf_test()
121 {
122 unsigned i;
123 pj_uint32_t pool_time=0, malloc_time=0, pool_time2=0;
124 pj_timestamp start, end;
125 pj_uint32_t best, worst;
126
127 /* Initialize size of chunks to allocate in for the test. */
128 for (i=0; i<COUNT; ++i) {
129 unsigned aligned_size;
130 sizes[i] = MIN_SIZE + (pj_rand() % MAX_SIZE);
131 aligned_size = sizes[i];
132 if (aligned_size & (PJ_POOL_ALIGNMENT-1))
133 aligned_size = ((aligned_size + PJ_POOL_ALIGNMENT - 1)) & ~(PJ_POOL_ALIGNMENT - 1);
134 total_size += aligned_size;
135 }
136
137 /* Add some more for pool admin area */
138 total_size += 512;
139
140 PJ_LOG(3, (THIS_FILE, "Benchmarking pool.."));
141
142 /* Warmup */
143 pool_test_pool();
144 pool_test_malloc_free();
145
146 for (i=0; i<LOOP; ++i) {
147 pj_get_timestamp(&start);
148 if (pool_test_pool()) {
149 return 1;
150 }
151 pj_get_timestamp(&end);
152 pool_time += (end.u32.lo - start.u32.lo);
153
154 pj_get_timestamp(&start);
155 if (pool_test_malloc_free()) {
156 return 2;
157 }
158 pj_get_timestamp(&end);
159 malloc_time += (end.u32.lo - start.u32.lo);
160
161 pj_get_timestamp(&start);
162 if (pool_test_pool()) {
163 return 4;
164 }
165 pj_get_timestamp(&end);
166 pool_time2 += (end.u32.lo - start.u32.lo);
167 }
168
169 PJ_LOG(4,(THIS_FILE,"..LOOP count: %u",LOOP));
170 PJ_LOG(4,(THIS_FILE,"..number of alloc/dealloc per loop: %u",COUNT));
171 PJ_LOG(4,(THIS_FILE,"..pool allocation/deallocation time: %u",pool_time));
172 PJ_LOG(4,(THIS_FILE,"..malloc/free time: %u",malloc_time));
173 PJ_LOG(4,(THIS_FILE,"..pool again, second invocation: %u",pool_time2));
174
175 if (pool_time2==0) pool_time2=1;
176 if (pool_time < pool_time2)
177 best = pool_time, worst = pool_time2;
178 else
179 best = pool_time2, worst = pool_time;
180
181 /* avoid division by zero */
182 if (best==0) best=1;
183 if (worst==0) worst=1;
184
185 PJ_LOG(3, (THIS_FILE, "..pool speedup over malloc best=%dx, worst=%dx",
186 (int)(malloc_time/best),
187 (int)(malloc_time/worst)));
188 return 0;
189 }
190
191
192 #endif /* INCLUDE_POOL_PERF_TEST */
193