blob: 077829950985f49c36b3e13504f5f35e291c4ade [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
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
34static unsigned sizes[COUNT];
Benny Prijono42c5b9e2006-05-10 19:24:40 +000035static char *p[COUNT];
Benny Prijono5dcb38d2005-11-21 01:55:47 +000036#define MIN_SIZE 4
37#define MAX_SIZE 512
38static unsigned total_size;
39
Benny Prijono42c5b9e2006-05-10 19:24:40 +000040
Benny Prijono5dcb38d2005-11-21 01:55:47 +000041static 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 *p;
Benny Prijono42c5b9e2006-05-10 19:24:40 +000050 if ( (p=(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);
Benny Prijono5dcb38d2005-11-21 01:55:47 +000054 return -1;
Benny Prijono42c5b9e2006-05-10 19:24:40 +000055 }
Benny Prijono5dcb38d2005-11-21 01:55:47 +000056 *p = '\0';
57 }
58
59 pj_pool_release(pool);
60 return 0;
61}
62
63static int pool_test_malloc_free()
64{
Benny Prijono42c5b9e2006-05-10 19:24:40 +000065 int i; /* must be signed */
Benny Prijono5dcb38d2005-11-21 01:55:47 +000066
67 for (i=0; i<COUNT; ++i) {
68 p[i] = (char*)malloc(sizes[i]);
69 if (!p[i]) {
Benny Prijono42c5b9e2006-05-10 19:24:40 +000070 PJ_LOG(3,(THIS_FILE," error: malloc failed to allocate %d bytes",
71 sizes[i]));
72 --i;
73 while (i >= 0)
74 free(p[i]), --i;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000075 return -1;
76 }
77 *p[i] = '\0';
78 }
79
80 for (i=0; i<COUNT; ++i) {
81 free(p[i]);
82 }
83
84 return 0;
85}
86
87int pool_perf_test()
88{
89 unsigned i;
90 pj_uint32_t pool_time=0, malloc_time=0, pool_time2=0;
91 pj_timestamp start, end;
92 pj_uint32_t best, worst;
93
Benny Prijono42c5b9e2006-05-10 19:24:40 +000094 /* Initialize size of chunks to allocate in for the test. */
Benny Prijono5dcb38d2005-11-21 01:55:47 +000095 for (i=0; i<COUNT; ++i) {
Benny Prijono42c5b9e2006-05-10 19:24:40 +000096 sizes[i] = MIN_SIZE + (pj_rand() % MAX_SIZE);
Benny Prijono5dcb38d2005-11-21 01:55:47 +000097 total_size += sizes[i];
98 }
99
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000100 /* Add some more for pool admin area */
101 total_size += 512;
102
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000103 PJ_LOG(3, (THIS_FILE, "Benchmarking pool.."));
104
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000105 /* Warmup */
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000106 pool_test_pool();
107 pool_test_malloc_free();
108
109 for (i=0; i<LOOP; ++i) {
110 pj_get_timestamp(&start);
111 if (pool_test_pool()) {
112 return 1;
113 }
114 pj_get_timestamp(&end);
115 pool_time += (end.u32.lo - start.u32.lo);
116
117 pj_get_timestamp(&start);
118 if (pool_test_malloc_free()) {
119 return 2;
120 }
121 pj_get_timestamp(&end);
122 malloc_time += (end.u32.lo - start.u32.lo);
123
124 pj_get_timestamp(&start);
125 if (pool_test_pool()) {
126 return 4;
127 }
128 pj_get_timestamp(&end);
129 pool_time2 += (end.u32.lo - start.u32.lo);
130 }
131
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000132 PJ_LOG(4,(THIS_FILE,"..LOOP count: %u",LOOP));
133 PJ_LOG(4,(THIS_FILE,"..number of alloc/dealloc per loop: %u",COUNT));
134 PJ_LOG(4,(THIS_FILE,"..pool allocation/deallocation time: %u",pool_time));
135 PJ_LOG(4,(THIS_FILE,"..malloc/free time: %u",malloc_time));
136 PJ_LOG(4,(THIS_FILE,"..pool again, second invocation: %u",pool_time2));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000137
138 if (pool_time2==0) pool_time2=1;
139 if (pool_time < pool_time2)
140 best = pool_time, worst = pool_time2;
141 else
142 best = pool_time2, worst = pool_time;
143
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000144 PJ_LOG(3, (THIS_FILE, "..pool speedup over malloc best=%dx, worst=%dx",
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000145 (int)(malloc_time/best),
146 (int)(malloc_time/worst)));
147 return 0;
148}
149
150
151#endif /* INCLUDE_POOL_PERF_TEST */
152