blob: 6bc6dcfbafc21c4f5bca620214ce2926125c0e60 [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];
35#define MIN_SIZE 4
36#define MAX_SIZE 512
37static unsigned total_size;
38
39static int pool_test_pool()
40{
41 int i;
42 pj_pool_t *pool = pj_pool_create(mem, NULL, total_size + 4*COUNT, 0, NULL);
43 if (!pool)
44 return -1;
45
46 for (i=0; i<COUNT; ++i) {
47 char *p;
48 if ( (p=(char*)pj_pool_alloc(pool, sizes[i])) == NULL)
49 return -1;
50 *p = '\0';
51 }
52
53 pj_pool_release(pool);
54 return 0;
55}
56
57static int pool_test_malloc_free()
58{
59 char *p[COUNT];
60 int i;
61
62 for (i=0; i<COUNT; ++i) {
63 p[i] = (char*)malloc(sizes[i]);
64 if (!p[i]) {
65 // Don't care for memory leak in this test
66 return -1;
67 }
68 *p[i] = '\0';
69 }
70
71 for (i=0; i<COUNT; ++i) {
72 free(p[i]);
73 }
74
75 return 0;
76}
77
78int pool_perf_test()
79{
80 unsigned i;
81 pj_uint32_t pool_time=0, malloc_time=0, pool_time2=0;
82 pj_timestamp start, end;
83 pj_uint32_t best, worst;
84
85 // Initialize sizes.
86 for (i=0; i<COUNT; ++i) {
87 sizes[i] = MIN_SIZE + pj_rand() % MAX_SIZE;
88 total_size += sizes[i];
89 }
90
91 PJ_LOG(3, (THIS_FILE, "Benchmarking pool.."));
92
93 // Warmup
94 pool_test_pool();
95 pool_test_malloc_free();
96
97 for (i=0; i<LOOP; ++i) {
98 pj_get_timestamp(&start);
99 if (pool_test_pool()) {
100 return 1;
101 }
102 pj_get_timestamp(&end);
103 pool_time += (end.u32.lo - start.u32.lo);
104
105 pj_get_timestamp(&start);
106 if (pool_test_malloc_free()) {
107 return 2;
108 }
109 pj_get_timestamp(&end);
110 malloc_time += (end.u32.lo - start.u32.lo);
111
112 pj_get_timestamp(&start);
113 if (pool_test_pool()) {
114 return 4;
115 }
116 pj_get_timestamp(&end);
117 pool_time2 += (end.u32.lo - start.u32.lo);
118 }
119
120 PJ_LOG(4, (THIS_FILE, "..LOOP count: %u", LOOP));
121 PJ_LOG(4, (THIS_FILE, "..number of alloc/dealloc per loop: %u", COUNT));
122 PJ_LOG(4, (THIS_FILE, "..pool allocation/deallocation time: %u", pool_time));
123 PJ_LOG(4, (THIS_FILE, "..malloc/free time: %u", malloc_time));
124 PJ_LOG(4, (THIS_FILE, "..pool again, second invocation: %u", pool_time2));
125
126 if (pool_time2==0) pool_time2=1;
127 if (pool_time < pool_time2)
128 best = pool_time, worst = pool_time2;
129 else
130 best = pool_time2, worst = pool_time;
131
132 PJ_LOG(3, (THIS_FILE, "..malloc Speedup best=%dx, worst=%dx",
133 (int)(malloc_time/best),
134 (int)(malloc_time/worst)));
135 return 0;
136}
137
138
139#endif /* INCLUDE_POOL_PERF_TEST */
140