testLib.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /************************************************************************
  2. * AUTHOR: NiuJiuRu
  3. * FILENAME: testLib.c
  4. * CONTENT: "swapi library"库基准测试程序
  5. * NOTE: 由于"swapi library"库包含的内容较多,这里只测试重要核心的东西
  6. 包括:内存、堆、线程、线程池、消息队列、字符串函、日志数等
  7. * HISTORY:
  8. * 1, [2010-12-28] created by NiuJiuRu
  9. ***********************************************************************/
  10. #include "swapi.h"
  11. #include "swmem.h"
  12. #include "swthrd.h"
  13. #include "swthrdpool.h"
  14. #include "swmsgq.h"
  15. #include "swstring.h"
  16. #include "swrand.h"
  17. #include "swlog.h"
  18. // 可分块/可跟踪内存泄露的全局堆
  19. static void *s_hPool = NULL;
  20. // 超时
  21. static int s_timeout = 2000;
  22. // 线程函数1(循环)
  23. static int fun1(unsigned long wParam, unsigned long lParam)
  24. {
  25. printf("Hello, world!\n");
  26. return 1000; // 延时1秒
  27. }
  28. // 线程函数2(单次)
  29. static int fun2(unsigned long wParam, unsigned long lParam)
  30. {
  31. int a = 0;
  32. int cnt0 = 0, cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0, cnt5 = 0, cnt6 = 0, cnt7 = 0, cnt8 = 0, cnt9 = 0;
  33. int i = 0;
  34. printf("生成1000个10以内的随机数:\n");
  35. for(i = 0; i < 1000; i++)
  36. {
  37. a = xrand32(10);
  38. if(a==0) cnt0++;
  39. else if(a==1) cnt1++;
  40. else if(a==2) cnt2++;
  41. else if(a==3) cnt3++;
  42. else if(a==4) cnt4++;
  43. else if(a==5) cnt5++;
  44. else if(a==6) cnt6++;
  45. else if(a==7) cnt7++;
  46. else if(a==8) cnt8++;
  47. else if(a==9) cnt9++;
  48. printf("%d ", a);
  49. }
  50. printf("\n统计概率次数:\n");
  51. printf("0=%d; 1=%d; 2=%d; 3=%d; 4=%d; 5=%d; 6=%d; 7=%d; 8=%d; 9=%d\n", cnt0, cnt1, cnt2, cnt3, cnt4, cnt5, cnt6, cnt7, cnt8, cnt9);
  52. return -1; // 退出线程
  53. }
  54. // 线程函数3(消息队列)
  55. static void *s_hMsgQ = NULL;
  56. #define MSG_1 0x1980
  57. #define MSG_2 0x0813
  58. static int fun3(unsigned long wParam, unsigned long lParam)
  59. {
  60. void *hThrd = NULL;
  61. int msg = 0;
  62. sw_msgq_read(s_hMsgQ, &msg, 0, 0, 1000);
  63. if(msg == MSG_1)
  64. {
  65. printf("[带消息队列的线程]read messag: %d ok\n", msg);
  66. }
  67. else if(msg == MSG_2)
  68. {
  69. hThrd = sw_thrdpool_alloc(s_hPool, fun2, 0, 0, s_timeout);
  70. if(hThrd) sw_thrd_resume(hThrd);
  71. }
  72. return 1;
  73. }
  74. // 检查核心内存分配情况
  75. static void coreMemCheck(const char *filename, int line, const char *ptr, int size, void *lpParameter)
  76. {
  77. sw_log_info("filename:%s line:%d addr:%#p size:%dbytes", filename, line, ptr, size);
  78. }
  79. // 主函数
  80. int main(int argc,char *argv[])
  81. {
  82. void *hThrd1 = NULL, *hThrd2 = NULL, *hThrd3 = NULL;
  83. char coreBuf[64*1024] = { 0 }, cmdBuf[32] = { 0 };
  84. // 测试日志打印
  85. sw_log_info("\"swapi library\" standard test starting...");
  86. // 初始化全局堆
  87. if(!sw_heap_init(coreBuf, sizeof(coreBuf), 4)) goto EndP;
  88. // 创建线程池
  89. s_hPool = sw_thrdpool_create(8);
  90. if(!s_hPool) goto EndP;
  91. // 创建消息队列
  92. s_hMsgQ = sw_msgq_create(8);
  93. if(!s_hMsgQ) goto EndP;
  94. // 创建线程
  95. hThrd1 = sw_thrdpool_alloc(s_hPool, fun1, 0, 0, s_timeout);
  96. if(hThrd1) sw_thrd_resume(hThrd1);
  97. hThrd2 = sw_thrdpool_alloc(s_hPool, fun2, 0, 0, s_timeout);
  98. if(hThrd2) sw_thrd_resume(hThrd2);
  99. hThrd3 = sw_thrdpool_alloc(s_hPool, fun3, 0, 0, s_timeout);
  100. if(hThrd3) sw_thrd_resume(hThrd3);
  101. // 打印全局堆的内存分配情况
  102. sw_log_info("memory allocation situation(0x%08x %dbytes):", coreBuf, sw_heap_getTotalSize());
  103. sw_heap_check(coreMemCheck, NULL);
  104. // 控制命令
  105. CmdP:
  106. memset(cmdBuf, 0, sizeof(cmdBuf));
  107. if(fgets(cmdBuf, sizeof(cmdBuf), stdin) == NULL)
  108. {
  109. sw_thrd_delay(1); // 没有读到任何输入时, 延时1毫秒, 交出CPU控制权
  110. goto CmdP;
  111. }
  112. if(xstrncasecmp(cmdBuf, "exit", strlen("exit")) == 0 || xstrncasecmp(cmdBuf, "0", 1) == 0)
  113. {
  114. sw_thrdpool_printf(s_hPool); // 打印线程池当前的分配情况
  115. sw_thrdpool_destroy(s_hPool, s_timeout); // 销毁线程池
  116. goto EndP;
  117. }
  118. else if(xstrncasecmp(cmdBuf, "1", 1) == 0)
  119. {
  120. sw_thrd_pause(hThrd1);
  121. sw_msgq_post(s_hMsgQ, MSG_1, 0, 0);
  122. }
  123. else if(xstrncasecmp(cmdBuf, "2", 1) == 0)
  124. {
  125. sw_thrd_resume(hThrd1);
  126. sw_msgq_post(s_hMsgQ, MSG_2, 0, 0);
  127. }
  128. goto CmdP;
  129. // 结束退出
  130. EndP:
  131. sw_msgq_destroy(s_hMsgQ); // 销毁消息队列
  132. sw_log_info("Memory leak check:");
  133. sw_heap_check(coreMemCheck, NULL); // 内存泄露检查
  134. sw_heap_exit();
  135. sw_log_info("\"swapi library\" standard test end.");
  136. return 0;
  137. }