| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 64 |
|
| 65 |
#if 0 |
| 66 |
static int pool_test_malloc_free() |
| 67 |
{ |
| 68 |
int i; |
| 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 |
| 93 |
|
| 94 |
static int pool_test_malloc_free() |
| 95 |
{ |
| 96 |
int i; |
| 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 |
| 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 |
|
| 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 |
|
| 138 |
total_size += 512; |
| 139 |
|
| 140 |
PJ_LOG(3, (THIS_FILE, "Benchmarking pool..")); |
| 141 |
|
| 142 |
|
| 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 |
|
| 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 |
| 193 |
|