test_sched.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2009, Digium, Inc.
  5. *
  6. * Russell Bryant <russell@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 ast_sched performance test module
  21. *
  22. * \author Russell Bryant <russell@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <depend>TEST_FRAMEWORK</depend>
  26. <support_level>extended</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. #include <inttypes.h>
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/module.h"
  32. #include "asterisk/utils.h"
  33. #include "asterisk/sched.h"
  34. #include "asterisk/test.h"
  35. #include "asterisk/cli.h"
  36. static int sched_cb(const void *data)
  37. {
  38. return 0;
  39. }
  40. AST_TEST_DEFINE(sched_test_order)
  41. {
  42. struct ast_sched_context *con;
  43. enum ast_test_result_state res = AST_TEST_FAIL;
  44. int id1, id2, id3, wait;
  45. switch (cmd) {
  46. case TEST_INIT:
  47. info->name = "sched_test_order";
  48. info->category = "/main/sched/";
  49. info->summary = "Test ordering of events in the scheduler API";
  50. info->description =
  51. "This test ensures that events are properly ordered by the "
  52. "time they are scheduled to execute in the scheduler API.";
  53. return AST_TEST_NOT_RUN;
  54. case TEST_EXECUTE:
  55. break;
  56. }
  57. if (!(con = ast_sched_context_create())) {
  58. ast_test_status_update(test,
  59. "Test failed - could not create scheduler context\n");
  60. return AST_TEST_FAIL;
  61. }
  62. /* Add 3 scheduler entries, and then remove them, ensuring that the result
  63. * of ast_sched_wait() looks appropriate at each step along the way. */
  64. if ((wait = ast_sched_wait(con)) != -1) {
  65. ast_test_status_update(test,
  66. "ast_sched_wait() should have returned -1, returned '%d'\n",
  67. wait);
  68. goto return_cleanup;
  69. }
  70. if ((id1 = ast_sched_add(con, 100000, sched_cb, NULL)) == -1) {
  71. ast_test_status_update(test, "Failed to add scheduler entry\n");
  72. goto return_cleanup;
  73. }
  74. if ((wait = ast_sched_wait(con)) > 100000) {
  75. ast_test_status_update(test,
  76. "ast_sched_wait() should have returned <= 100000, returned '%d'\n",
  77. wait);
  78. goto return_cleanup;
  79. }
  80. if ((id2 = ast_sched_add(con, 10000, sched_cb, NULL)) == -1) {
  81. ast_test_status_update(test, "Failed to add scheduler entry\n");
  82. goto return_cleanup;
  83. }
  84. if ((wait = ast_sched_wait(con)) > 10000) {
  85. ast_test_status_update(test,
  86. "ast_sched_wait() should have returned <= 10000, returned '%d'\n",
  87. wait);
  88. goto return_cleanup;
  89. }
  90. if ((id3 = ast_sched_add(con, 1000, sched_cb, NULL)) == -1) {
  91. ast_test_status_update(test, "Failed to add scheduler entry\n");
  92. goto return_cleanup;
  93. }
  94. if ((wait = ast_sched_wait(con)) > 1000) {
  95. ast_test_status_update(test,
  96. "ast_sched_wait() should have returned <= 1000, returned '%d'\n",
  97. wait);
  98. goto return_cleanup;
  99. }
  100. if (ast_sched_del(con, id3) == -1) {
  101. ast_test_status_update(test, "Failed to remove scheduler entry\n");
  102. goto return_cleanup;
  103. }
  104. if ((wait = ast_sched_wait(con)) <= 1000) {
  105. ast_test_status_update(test,
  106. "ast_sched_wait() should have returned > 1000, returned '%d'\n",
  107. wait);
  108. goto return_cleanup;
  109. }
  110. if (ast_sched_del(con, id2) == -1) {
  111. ast_test_status_update(test, "Failed to remove scheduler entry\n");
  112. goto return_cleanup;
  113. }
  114. if ((wait = ast_sched_wait(con)) <= 10000) {
  115. ast_test_status_update(test,
  116. "ast_sched_wait() should have returned > 10000, returned '%d'\n",
  117. wait);
  118. goto return_cleanup;
  119. }
  120. if (ast_sched_del(con, id1) == -1) {
  121. ast_test_status_update(test, "Failed to remove scheduler entry\n");
  122. goto return_cleanup;
  123. }
  124. if ((wait = ast_sched_wait(con)) != -1) {
  125. ast_test_status_update(test,
  126. "ast_sched_wait() should have returned -1, returned '%d'\n",
  127. wait);
  128. goto return_cleanup;
  129. }
  130. res = AST_TEST_PASS;
  131. return_cleanup:
  132. ast_sched_context_destroy(con);
  133. return res;
  134. }
  135. static char *handle_cli_sched_bench(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  136. {
  137. struct ast_sched_context *con;
  138. struct timeval start;
  139. unsigned int num, i;
  140. int *sched_ids = NULL;
  141. switch (cmd) {
  142. case CLI_INIT:
  143. e->command = "sched benchmark";
  144. e->usage = ""
  145. "Usage: sched benchmark <num>\n"
  146. "";
  147. return NULL;
  148. case CLI_GENERATE:
  149. return NULL;
  150. }
  151. if (a->argc != e->args + 1) {
  152. return CLI_SHOWUSAGE;
  153. }
  154. if (sscanf(a->argv[e->args], "%u", &num) != 1) {
  155. return CLI_SHOWUSAGE;
  156. }
  157. if (!(con = ast_sched_context_create())) {
  158. ast_cli(a->fd, "Test failed - could not create scheduler context\n");
  159. return CLI_FAILURE;
  160. }
  161. if (!(sched_ids = ast_malloc(sizeof(*sched_ids) * num))) {
  162. ast_cli(a->fd, "Test failed - memory allocation failure\n");
  163. goto return_cleanup;
  164. }
  165. ast_cli(a->fd, "Testing ast_sched_add() performance - timing how long it takes "
  166. "to add %u entries at random time intervals from 0 to 60 seconds\n", num);
  167. start = ast_tvnow();
  168. for (i = 0; i < num; i++) {
  169. int when = abs(ast_random()) % 60000;
  170. if ((sched_ids[i] = ast_sched_add(con, when, sched_cb, NULL)) == -1) {
  171. ast_cli(a->fd, "Test failed - sched_add returned -1\n");
  172. goto return_cleanup;
  173. }
  174. }
  175. ast_cli(a->fd, "Test complete - %" PRIi64 " us\n", ast_tvdiff_us(ast_tvnow(), start));
  176. ast_cli(a->fd, "Testing ast_sched_del() performance - timing how long it takes "
  177. "to delete %u entries with random time intervals from 0 to 60 seconds\n", num);
  178. start = ast_tvnow();
  179. for (i = 0; i < num; i++) {
  180. if (ast_sched_del(con, sched_ids[i]) == -1) {
  181. ast_cli(a->fd, "Test failed - sched_del returned -1\n");
  182. goto return_cleanup;
  183. }
  184. }
  185. ast_cli(a->fd, "Test complete - %" PRIi64 " us\n", ast_tvdiff_us(ast_tvnow(), start));
  186. return_cleanup:
  187. ast_sched_context_destroy(con);
  188. if (sched_ids) {
  189. ast_free(sched_ids);
  190. }
  191. return CLI_SUCCESS;
  192. }
  193. static struct ast_cli_entry cli_sched[] = {
  194. AST_CLI_DEFINE(handle_cli_sched_bench, "Benchmark ast_sched add/del performance"),
  195. };
  196. static int unload_module(void)
  197. {
  198. AST_TEST_UNREGISTER(sched_test_order);
  199. ast_cli_unregister_multiple(cli_sched, ARRAY_LEN(cli_sched));
  200. return 0;
  201. }
  202. static int load_module(void)
  203. {
  204. AST_TEST_REGISTER(sched_test_order);
  205. ast_cli_register_multiple(cli_sched, ARRAY_LEN(cli_sched));
  206. return AST_MODULE_LOAD_SUCCESS;
  207. }
  208. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ast_sched performance test module");