test_logger.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2009, Digium, Inc.
  5. *
  6. * Kevin P. Fleming <kpfleming@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Test module for the logging subsystem
  21. *
  22. * \author\verbatim Kevin P. Fleming <kpfleming@digium.com> \endverbatim
  23. *
  24. * \ingroup tests
  25. */
  26. /*** MODULEINFO
  27. <depend>TEST_FRAMEWORK</depend>
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "asterisk/file.h"
  33. #include "asterisk/channel.h"
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/lock.h"
  37. #include "asterisk/app.h"
  38. #include "asterisk/cli.h"
  39. struct test {
  40. const char *name;
  41. unsigned int x_success;
  42. unsigned int x_failure;
  43. unsigned int u_success;
  44. unsigned int u_failure;
  45. };
  46. static void output_tests(struct test *tests, size_t num_tests, int fd)
  47. {
  48. unsigned int x;
  49. for (x = 0; x < num_tests; x++) {
  50. ast_cli(fd, "Test %u: %s\n", x + 1, tests[x].name);
  51. ast_cli(fd, "\tExpected Successes: %u\n", tests[x].x_success);
  52. ast_cli(fd, "\tExpected Failures: %u\n", tests[x].x_failure);
  53. ast_cli(fd, "\tUnexpected Successes: %u\n", tests[x].u_success);
  54. ast_cli(fd, "\tUnexpected Failures: %u\n", tests[x].u_failure);
  55. ast_cli(fd, "Test %u Result: %s\n", x + 1, (tests[x].u_success + tests[x].u_failure) ? "FAIL" : "PASS");
  56. }
  57. }
  58. static char *handle_cli_dynamic_level_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  59. {
  60. unsigned int level;
  61. unsigned int x;
  62. unsigned int test;
  63. struct test tests[] = {
  64. { .name = "Simple register/message/unregister",
  65. },
  66. { .name = "Register multiple levels",
  67. },
  68. };
  69. switch (cmd) {
  70. case CLI_INIT:
  71. e->command = "logger test dynamic";
  72. e->usage = ""
  73. "Usage: logger test dynamic\n"
  74. "";
  75. return NULL;
  76. case CLI_GENERATE:
  77. return NULL;
  78. }
  79. for (test = 0; test < ARRAY_LEN(tests); test++) {
  80. ast_cli(a->fd, "Test %u: %s.\n", test + 1, tests[test].name);
  81. switch (test) {
  82. case 0:
  83. if ((level = ast_logger_register_level("test")) != -1) {
  84. ast_cli(a->fd, "Test: got level %u\n", level);
  85. ast_log_dynamic_level(level, "Logger Dynamic Test: Test 1\n");
  86. ast_logger_unregister_level("test");
  87. tests[test].x_success++;
  88. } else {
  89. ast_cli(a->fd, "Test: Failed, could not register level 'test'.\n");
  90. tests[test].u_failure++;
  91. }
  92. break;
  93. case 1:
  94. {
  95. char level_name[18][8];
  96. for (x = 0; x < ARRAY_LEN(level_name); x++) {
  97. sprintf(level_name[x], "level%02u", x);
  98. if ((level = ast_logger_register_level(level_name[x])) == -1) {
  99. if (x < 16) {
  100. tests[test].u_failure++;
  101. } else {
  102. tests[test].x_failure++;
  103. }
  104. level_name[x][0] = '\0';
  105. } else {
  106. ast_cli(a->fd, "Test: registered '%s', got level %u\n", level_name[x], level);
  107. if (x < 16) {
  108. tests[test].x_success++;
  109. } else {
  110. tests[test].u_success++;
  111. }
  112. }
  113. }
  114. for (x = 0; x < ARRAY_LEN(level_name); x++) {
  115. if (!ast_strlen_zero(level_name[x])) {
  116. ast_logger_unregister_level(level_name[x]);
  117. }
  118. }
  119. }
  120. }
  121. }
  122. output_tests(tests, ARRAY_LEN(tests), a->fd);
  123. return CLI_SUCCESS;
  124. }
  125. static char *handle_cli_performance_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  126. {
  127. unsigned int level;
  128. unsigned int test;
  129. struct test tests[] = {
  130. { .name = "Log 10,000 messages",
  131. },
  132. };
  133. switch (cmd) {
  134. case CLI_INIT:
  135. e->command = "logger test performance";
  136. e->usage = ""
  137. "Usage: logger test performance\n"
  138. "";
  139. return NULL;
  140. case CLI_GENERATE:
  141. return NULL;
  142. }
  143. for (test = 0; test < ARRAY_LEN(tests); test++) {
  144. ast_cli(a->fd, "Test %u: %s.\n", test + 1, tests[test].name);
  145. switch (test) {
  146. case 0:
  147. if ((level = ast_logger_register_level("perftest")) != -1) {
  148. unsigned int x;
  149. struct timeval start, end;
  150. int elapsed;
  151. ast_cli(a->fd, "Test: got level %u\n", level);
  152. start = ast_tvnow();
  153. for (x = 0; x < 10000; x++) {
  154. ast_log_dynamic_level(level, "Performance test log message\n");
  155. }
  156. end = ast_tvnow();
  157. elapsed = ast_tvdiff_ms(end, start);
  158. ast_cli(a->fd, "Test: 10,000 messages in %f seconds.\n", (float) elapsed / 1000);
  159. ast_logger_unregister_level("perftest");
  160. tests[test].x_success++;
  161. } else {
  162. ast_cli(a->fd, "Test: Failed, could not register level 'perftest'.\n");
  163. tests[test].u_failure++;
  164. }
  165. break;
  166. }
  167. }
  168. output_tests(tests, ARRAY_LEN(tests), a->fd);
  169. return CLI_SUCCESS;
  170. }
  171. static char *handle_cli_queue_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  172. {
  173. int level;
  174. int current_queue_limit;
  175. unsigned int x;
  176. struct timeval start, end;
  177. int elapsed;
  178. char tmppath[] = "/tmp/asterisk_logger_queue.XXXXXX";
  179. int fd;
  180. switch (cmd) {
  181. case CLI_INIT:
  182. e->command = "logger test queue";
  183. e->usage = ""
  184. "Usage: logger test queue\n"
  185. "";
  186. return NULL;
  187. case CLI_GENERATE:
  188. return NULL;
  189. }
  190. fd = mkstemp(tmppath);
  191. if (fd < 0) {
  192. ast_cli(a->fd, "Test: Failed, could not create temporary log file '%s'.\n", tmppath);
  193. return CLI_SUCCESS;
  194. }
  195. level = ast_logger_register_level("queuetest");
  196. if (level < 0) {
  197. ast_cli(a->fd, "Test: Failed, could not register level 'queuetest'.\n");
  198. return CLI_SUCCESS;
  199. }
  200. ast_cli(a->fd, "Test: got level %d for 'queuetest'.\n", level);
  201. if (ast_logger_create_channel(tmppath, "queuetest") != AST_LOGGER_SUCCESS) {
  202. ast_cli(a->fd, "Test: Unable to create logger channel '%s'\n", tmppath);
  203. goto error;
  204. }
  205. current_queue_limit = ast_logger_get_queue_limit();
  206. ast_cli(a->fd, "Test: Current queue limit: %d. Setting to 100 for test.\n", current_queue_limit);
  207. ast_logger_set_queue_limit(100);
  208. ast_cli(a->fd, "Test: You should see SOME 'exceeded' and 'resumed' messages after the test "
  209. "is completed. How many is dependent on system resources.\n");
  210. start = ast_tvnow();
  211. for (x = 0; x < 10000; x++) {
  212. ast_log_dynamic_level(level, "Performance test log message %2d\n", x);
  213. }
  214. end = ast_tvnow();
  215. elapsed = ast_tvdiff_ms(end, start);
  216. ast_cli(a->fd, "Test: 10,000 messages in %f seconds.\n", (float) elapsed / 1000);
  217. ast_cli(a->fd, "Test: Completed. Resetting queue limit to %d.\n", current_queue_limit);
  218. ast_logger_set_queue_limit(current_queue_limit);
  219. error:
  220. ast_logger_remove_channel(tmppath);
  221. ast_logger_unregister_level("queuetest");
  222. close(fd);
  223. unlink(tmppath);
  224. return CLI_SUCCESS;
  225. }
  226. static struct ast_cli_entry cli_logger[] = {
  227. AST_CLI_DEFINE(handle_cli_dynamic_level_test, "Test the dynamic logger level implementation"),
  228. AST_CLI_DEFINE(handle_cli_performance_test, "Test the logger performance"),
  229. AST_CLI_DEFINE(handle_cli_queue_test, "Test the logger queue"),
  230. };
  231. static int unload_module(void)
  232. {
  233. ast_cli_unregister_multiple(cli_logger, ARRAY_LEN(cli_logger));
  234. return 0;
  235. }
  236. static int load_module(void)
  237. {
  238. ast_cli_register_multiple(cli_logger, ARRAY_LEN(cli_logger));
  239. return AST_MODULE_LOAD_SUCCESS;
  240. }
  241. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Logger Test Module");