blob: 8bd5635f70733977bff9d48721f4be4c1d4722b7 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include "test.h"
21
22#if INCLUDE_POOL_PERF_TEST
23
24#include <pjlib.h>
25#include <pj/compat/malloc.h>
26
27#if !PJ_HAS_HIGH_RES_TIMER
28# error Need high resolution timer for this test.
29#endif
30
31#define THIS_FILE "test"
32
33#define LOOP 10
34#define COUNT 1024
35static unsigned sizes[COUNT];
Benny Prijono42c5b9e2006-05-10 19:24:40 +000036static char *p[COUNT];
Benny Prijono5dcb38d2005-11-21 01:55:47 +000037#define MIN_SIZE 4
38#define MAX_SIZE 512
39static unsigned total_size;
40
Benny Prijono42c5b9e2006-05-10 19:24:40 +000041
Benny Prijono5dcb38d2005-11-21 01:55:47 +000042static int pool_test_pool()
43{
44 int i;
45 pj_pool_t *pool = pj_pool_create(mem, NULL, total_size + 4*COUNT, 0, NULL);
46 if (!pool)
47 return -1;
48
49 for (i=0; i<COUNT; ++i) {
50 char *p;
Benny Prijono42c5b9e2006-05-10 19:24:40 +000051 if ( (p=(char*)pj_pool_alloc(pool, sizes[i])) == NULL) {
52 PJ_LOG(3,(THIS_FILE," error: pool failed to allocate %d bytes",
53 sizes[i]));
54 pj_pool_release(pool);
Benny Prijono5dcb38d2005-11-21 01:55:47 +000055 return -1;
Benny Prijono42c5b9e2006-05-10 19:24:40 +000056 }
Benny Prijono5dcb38d2005-11-21 01:55:47 +000057 *p = '\0';
58 }
59
60 pj_pool_release(pool);
61 return 0;
62}
63
Benny Prijonoaeeb1d12007-05-01 10:42:22 +000064/* Symbian doesn't have malloc()/free(), so we use new/delete instead */
Benny Prijonof3ce1392007-12-28 19:03:20 +000065//#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
66#if 0
Benny Prijonoaeeb1d12007-05-01 10:42:22 +000067static int pool_test_malloc_free()
68{
69 int i; /* must be signed */
70
71 for (i=0; i<COUNT; ++i) {
Benny Prijonof3ce1392007-12-28 19:03:20 +000072 p[i] = new char[sizes[i]];
73 if (!p[i]) {
74 PJ_LOG(3,(THIS_FILE," error: malloc failed to allocate %d bytes",
75 sizes[i]));
76 --i;
77 while (i >= 0) {
78 delete [] p[i];
79 --i;
80 }
81 return -1;
82 }
83 *p[i] = '\0';
Benny Prijonoaeeb1d12007-05-01 10:42:22 +000084 }
85
86 for (i=0; i<COUNT; ++i) {
Benny Prijonof3ce1392007-12-28 19:03:20 +000087 delete [] p[i];
Benny Prijonoaeeb1d12007-05-01 10:42:22 +000088 }
89
90 return 0;
91}
92
93#else /* PJ_SYMBIAN */
94
Benny Prijono5dcb38d2005-11-21 01:55:47 +000095static int pool_test_malloc_free()
96{
Benny Prijono42c5b9e2006-05-10 19:24:40 +000097 int i; /* must be signed */
Benny Prijono5dcb38d2005-11-21 01:55:47 +000098
99 for (i=0; i<COUNT; ++i) {
100 p[i] = (char*)malloc(sizes[i]);
101 if (!p[i]) {
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000102 PJ_LOG(3,(THIS_FILE," error: malloc failed to allocate %d bytes",
103 sizes[i]));
104 --i;
105 while (i >= 0)
106 free(p[i]), --i;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000107 return -1;
108 }
109 *p[i] = '\0';
110 }
111
112 for (i=0; i<COUNT; ++i) {
113 free(p[i]);
114 }
115
116 return 0;
117}
118
Benny Prijonoaeeb1d12007-05-01 10:42:22 +0000119#endif /* PJ_SYMBIAN */
120
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000121int pool_perf_test()
122{
123 unsigned i;
124 pj_uint32_t pool_time=0, malloc_time=0, pool_time2=0;
125 pj_timestamp start, end;
126 pj_uint32_t best, worst;
127
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000128 /* Initialize size of chunks to allocate in for the test. */
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000129 for (i=0; i<COUNT; ++i) {
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000130 sizes[i] = MIN_SIZE + (pj_rand() % MAX_SIZE);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000131 total_size += sizes[i];
132 }
133
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000134 /* Add some more for pool admin area */
135 total_size += 512;
136
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000137 PJ_LOG(3, (THIS_FILE, "Benchmarking pool.."));
138
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000139 /* Warmup */
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000140 pool_test_pool();
141 pool_test_malloc_free();
142
143 for (i=0; i<LOOP; ++i) {
144 pj_get_timestamp(&start);
145 if (pool_test_pool()) {
146 return 1;
147 }
148 pj_get_timestamp(&end);
149 pool_time += (end.u32.lo - start.u32.lo);
150
151 pj_get_timestamp(&start);
152 if (pool_test_malloc_free()) {
153 return 2;
154 }
155 pj_get_timestamp(&end);
156 malloc_time += (end.u32.lo - start.u32.lo);
157
158 pj_get_timestamp(&start);
159 if (pool_test_pool()) {
160 return 4;
161 }
162 pj_get_timestamp(&end);
163 pool_time2 += (end.u32.lo - start.u32.lo);
164 }
165
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000166 PJ_LOG(4,(THIS_FILE,"..LOOP count: %u",LOOP));
167 PJ_LOG(4,(THIS_FILE,"..number of alloc/dealloc per loop: %u",COUNT));
168 PJ_LOG(4,(THIS_FILE,"..pool allocation/deallocation time: %u",pool_time));
169 PJ_LOG(4,(THIS_FILE,"..malloc/free time: %u",malloc_time));
170 PJ_LOG(4,(THIS_FILE,"..pool again, second invocation: %u",pool_time2));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000171
172 if (pool_time2==0) pool_time2=1;
173 if (pool_time < pool_time2)
174 best = pool_time, worst = pool_time2;
175 else
176 best = pool_time2, worst = pool_time;
Benny Prijonoaeeb1d12007-05-01 10:42:22 +0000177
178 /* avoid division by zero */
179 if (best==0) best=1;
180 if (worst==0) worst=1;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000181
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000182 PJ_LOG(3, (THIS_FILE, "..pool speedup over malloc best=%dx, worst=%dx",
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000183 (int)(malloc_time/best),
184 (int)(malloc_time/worst)));
185 return 0;
186}
187
188
189#endif /* INCLUDE_POOL_PERF_TEST */
190