2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2009, Digium, Inc.
6 * Russell Bryant <russell@digium.com>
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.
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.
21 * \brief ast_sched performance test module
23 * \author Russell Bryant <russell@digium.com>
27 <defaultenabled>no</defaultenabled>
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #include "asterisk/module.h"
37 #include "asterisk/cli.h"
38 #include "asterisk/utils.h"
39 #include "asterisk/sched.h"
41 static int sched_cb(const void *data)
46 static char *handle_cli_sched_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
48 struct sched_context *con;
49 char *res = CLI_FAILURE;
50 int id1, id2, id3, wait;
54 e->command = "sched test";
57 " Test scheduler entry ordering.\n"
64 if (a->argc != e->args) {
68 ast_cli(a->fd, "Testing scheduler entry ordering ...\n");
70 if (!(con = sched_context_create())) {
71 ast_cli(a->fd, "Test failed - could not create scheduler context\n");
75 /* Add 3 scheduler entries, and then remove them, ensuring that the result
76 * of ast_sched_wait() looks appropriate at each step along the way. */
78 if ((wait = ast_sched_wait(con)) != -1) {
79 ast_cli(a->fd, "ast_sched_wait() should have returned -1, returned '%d'\n",
84 if ((id1 = ast_sched_add(con, 100000, sched_cb, NULL)) == -1) {
85 ast_cli(a->fd, "Failed to add scheduler entry\n");
89 if ((wait = ast_sched_wait(con)) > 100000) {
90 ast_cli(a->fd, "ast_sched_wait() should have returned <= 100000, returned '%d'\n",
95 if ((id2 = ast_sched_add(con, 10000, sched_cb, NULL)) == -1) {
96 ast_cli(a->fd, "Failed to add scheduler entry\n");
100 if ((wait = ast_sched_wait(con)) > 10000) {
101 ast_cli(a->fd, "ast_sched_wait() should have returned <= 10000, returned '%d'\n",
106 if ((id3 = ast_sched_add(con, 1000, sched_cb, NULL)) == -1) {
107 ast_cli(a->fd, "Failed to add scheduler entry\n");
111 if ((wait = ast_sched_wait(con)) > 1000) {
112 ast_cli(a->fd, "ast_sched_wait() should have returned <= 1000, returned '%d'\n",
117 if (ast_sched_del(con, id3) == -1) {
118 ast_cli(a->fd, "Failed to remove scheduler entry\n");
122 if ((wait = ast_sched_wait(con)) <= 1000) {
123 ast_cli(a->fd, "ast_sched_wait() should have returned > 1000, returned '%d'\n",
128 if (ast_sched_del(con, id2) == -1) {
129 ast_cli(a->fd, "Failed to remove scheduler entry\n");
133 if ((wait = ast_sched_wait(con)) <= 10000) {
134 ast_cli(a->fd, "ast_sched_wait() should have returned > 10000, returned '%d'\n",
139 if (ast_sched_del(con, id1) == -1) {
140 ast_cli(a->fd, "Failed to remove scheduler entry\n");
144 if ((wait = ast_sched_wait(con)) != -1) {
145 ast_cli(a->fd, "ast_sched_wait() should have returned -1, returned '%d'\n",
152 ast_cli(a->fd, "Test passed!\n");
155 sched_context_destroy(con);
160 static char *handle_cli_sched_bench(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
162 struct sched_context *con;
163 struct timeval start;
165 int *sched_ids = NULL;
169 e->command = "sched benchmark";
171 "Usage: sched benchmark <num>\n"
178 if (a->argc != e->args + 1) {
179 return CLI_SHOWUSAGE;
182 if (sscanf(a->argv[e->args], "%u", &num) != 1) {
183 return CLI_SHOWUSAGE;
186 if (!(con = sched_context_create())) {
187 ast_cli(a->fd, "Test failed - could not create scheduler context\n");
191 if (!(sched_ids = ast_malloc(sizeof(*sched_ids) * num))) {
192 ast_cli(a->fd, "Test failed - memory allocation failure\n");
196 ast_cli(a->fd, "Testing ast_sched_add() performance - timing how long it takes "
197 "to add %u entries at random time intervals from 0 to 60 seconds\n", num);
201 for (i = 0; i < num; i++) {
202 int when = abs(ast_random()) % 60000;
203 if ((sched_ids[i] = ast_sched_add(con, when, sched_cb, NULL)) == -1) {
204 ast_cli(a->fd, "Test failed - sched_add returned -1\n");
209 ast_cli(a->fd, "Test complete - %" PRIi64 " us\n", ast_tvdiff_us(ast_tvnow(), start));
211 ast_cli(a->fd, "Testing ast_sched_del() performance - timing how long it takes "
212 "to delete %u entries with random time intervals from 0 to 60 seconds\n", num);
216 for (i = 0; i < num; i++) {
217 if (ast_sched_del(con, sched_ids[i]) == -1) {
218 ast_cli(a->fd, "Test failed - sched_del returned -1\n");
223 ast_cli(a->fd, "Test complete - %" PRIi64 " us\n", ast_tvdiff_us(ast_tvnow(), start));
226 sched_context_destroy(con);
234 static struct ast_cli_entry cli_sched[] = {
235 AST_CLI_DEFINE(handle_cli_sched_bench, "Benchmark ast_sched add/del performance"),
236 AST_CLI_DEFINE(handle_cli_sched_test, "Test scheduler entry ordering"),
239 static int unload_module(void)
241 ast_cli_unregister_multiple(cli_sched, ARRAY_LEN(cli_sched));
245 static int load_module(void)
247 ast_cli_register_multiple(cli_sched, ARRAY_LEN(cli_sched));
248 return AST_MODULE_LOAD_SUCCESS;
251 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ast_sched performance test module");