test_logger.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. <defaultenabled>no</defaultenabled>
  28. <support_level>extended</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 %d: %s\n", x + 1, tests[x].name);
  51. ast_cli(fd, "\tExpected Successes: %d\n", tests[x].x_success);
  52. ast_cli(fd, "\tExpected Failures: %d\n", tests[x].x_failure);
  53. ast_cli(fd, "\tUnexpected Successes: %d\n", tests[x].u_success);
  54. ast_cli(fd, "\tUnexpected Failures: %d\n", tests[x].u_failure);
  55. ast_cli(fd, "Test %d 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 %d: %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 %d\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%02d", 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 %d\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 %d: %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 %d\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 struct ast_cli_entry cli_logger[] = {
  172. AST_CLI_DEFINE(handle_cli_dynamic_level_test, "Test the dynamic logger level implementation"),
  173. AST_CLI_DEFINE(handle_cli_performance_test, "Test the logger performance"),
  174. };
  175. static int unload_module(void)
  176. {
  177. ast_cli_unregister_multiple(cli_logger, ARRAY_LEN(cli_logger));
  178. return 0;
  179. }
  180. static int load_module(void)
  181. {
  182. ast_cli_register_multiple(cli_logger, ARRAY_LEN(cli_logger));
  183. return AST_MODULE_LOAD_SUCCESS;
  184. }
  185. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Logger Test Module");