pool_perf.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 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. #if INCLUDE_POOL_PERF_TEST
  21. #include <pjlib.h>
  22. #include <pj/compat/malloc.h>
  23. #if !PJ_HAS_HIGH_RES_TIMER
  24. # error Need high resolution timer for this test.
  25. #endif
  26. #define THIS_FILE "test"
  27. #define LOOP 10
  28. #define COUNT 1024
  29. static unsigned sizes[COUNT];
  30. static char *p[COUNT];
  31. #define MIN_SIZE 4
  32. #define MAX_SIZE 512
  33. static unsigned total_size;
  34. static int pool_test_pool()
  35. {
  36. int i;
  37. pj_pool_t *pool = pj_pool_create(mem, NULL, total_size + 4*COUNT, 0, NULL);
  38. if (!pool)
  39. return -1;
  40. for (i=0; i<COUNT; ++i) {
  41. char *ptr;
  42. if ( (ptr=(char*)pj_pool_alloc(pool, sizes[i])) == NULL) {
  43. PJ_LOG(3,(THIS_FILE," error: pool failed to allocate %d bytes",
  44. sizes[i]));
  45. pj_pool_release(pool);
  46. return -1;
  47. }
  48. *ptr = '\0';
  49. }
  50. pj_pool_release(pool);
  51. return 0;
  52. }
  53. /* Symbian doesn't have malloc()/free(), so we use new/delete instead */
  54. //#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
  55. #if 0
  56. static int pool_test_malloc_free()
  57. {
  58. int i; /* must be signed */
  59. for (i=0; i<COUNT; ++i) {
  60. p[i] = new char[sizes[i]];
  61. if (!p[i]) {
  62. PJ_LOG(3,(THIS_FILE," error: malloc failed to allocate %d bytes",
  63. sizes[i]));
  64. --i;
  65. while (i >= 0) {
  66. delete [] p[i];
  67. --i;
  68. }
  69. return -1;
  70. }
  71. *p[i] = '\0';
  72. }
  73. for (i=0; i<COUNT; ++i) {
  74. delete [] p[i];
  75. }
  76. return 0;
  77. }
  78. #else /* PJ_SYMBIAN */
  79. static int pool_test_malloc_free()
  80. {
  81. int i; /* must be signed */
  82. for (i=0; i<COUNT; ++i) {
  83. p[i] = (char*)malloc(sizes[i]);
  84. if (!p[i]) {
  85. PJ_LOG(3,(THIS_FILE," error: malloc failed to allocate %d bytes",
  86. sizes[i]));
  87. --i;
  88. while (i >= 0)
  89. free(p[i]), --i;
  90. return -1;
  91. }
  92. *p[i] = '\0';
  93. }
  94. for (i=0; i<COUNT; ++i) {
  95. free(p[i]);
  96. }
  97. return 0;
  98. }
  99. #endif /* PJ_SYMBIAN */
  100. int pool_perf_test()
  101. {
  102. unsigned i;
  103. pj_uint32_t pool_time=0, malloc_time=0, pool_time2=0;
  104. pj_timestamp start, end;
  105. pj_uint32_t best, worst;
  106. /* Initialize size of chunks to allocate in for the test. */
  107. for (i=0; i<COUNT; ++i) {
  108. unsigned aligned_size;
  109. sizes[i] = MIN_SIZE + (pj_rand() % MAX_SIZE);
  110. aligned_size = sizes[i];
  111. if (aligned_size & (PJ_POOL_ALIGNMENT-1))
  112. aligned_size = ((aligned_size + PJ_POOL_ALIGNMENT - 1)) & ~(PJ_POOL_ALIGNMENT - 1);
  113. total_size += aligned_size;
  114. }
  115. /* Add some more for pool admin area */
  116. total_size += 512;
  117. PJ_LOG(3, (THIS_FILE, "Benchmarking pool.."));
  118. /* Warmup */
  119. pool_test_pool();
  120. pool_test_malloc_free();
  121. for (i=0; i<LOOP; ++i) {
  122. pj_get_timestamp(&start);
  123. if (pool_test_pool()) {
  124. return 1;
  125. }
  126. pj_get_timestamp(&end);
  127. pool_time += (end.u32.lo - start.u32.lo);
  128. pj_get_timestamp(&start);
  129. if (pool_test_malloc_free()) {
  130. return 2;
  131. }
  132. pj_get_timestamp(&end);
  133. malloc_time += (end.u32.lo - start.u32.lo);
  134. pj_get_timestamp(&start);
  135. if (pool_test_pool()) {
  136. return 4;
  137. }
  138. pj_get_timestamp(&end);
  139. pool_time2 += (end.u32.lo - start.u32.lo);
  140. }
  141. PJ_LOG(4,(THIS_FILE,"..LOOP count: %u",LOOP));
  142. PJ_LOG(4,(THIS_FILE,"..number of alloc/dealloc per loop: %u",COUNT));
  143. PJ_LOG(4,(THIS_FILE,"..pool allocation/deallocation time: %u",pool_time));
  144. PJ_LOG(4,(THIS_FILE,"..malloc/free time: %u",malloc_time));
  145. PJ_LOG(4,(THIS_FILE,"..pool again, second invocation: %u",pool_time2));
  146. if (pool_time2==0) pool_time2=1;
  147. if (pool_time < pool_time2)
  148. best = pool_time, worst = pool_time2;
  149. else
  150. best = pool_time2, worst = pool_time;
  151. /* avoid division by zero */
  152. if (best==0) best=1;
  153. if (worst==0) worst=1;
  154. PJ_LOG(3, (THIS_FILE, "..pool speedup over malloc best=%dx, worst=%dx",
  155. (int)(malloc_time/best),
  156. (int)(malloc_time/worst)));
  157. return 0;
  158. }
  159. #endif /* INCLUDE_POOL_PERF_TEST */