[svn-commits] russell: branch russell/heap r175773 - /team/russell/heap/tests/test_sched.c

SVN commits to the Digium repositories svn-commits at lists.digium.com
Sat Feb 14 15:37:42 CST 2009


Author: russell
Date: Sat Feb 14 15:37:42 2009
New Revision: 175773

URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=175773
Log:
merge the add() and del() tests

Modified:
    team/russell/heap/tests/test_sched.c

Modified: team/russell/heap/tests/test_sched.c
URL: http://svn.digium.com/svn-view/asterisk/team/russell/heap/tests/test_sched.c?view=diff&rev=175773&r1=175772&r2=175773
==============================================================================
--- team/russell/heap/tests/test_sched.c (original)
+++ team/russell/heap/tests/test_sched.c Sat Feb 14 15:37:42 2009
@@ -37,17 +37,18 @@
 	return 0;
 }
 
-static char *handle_cli_sched_test_add(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+static char *handle_cli_sched_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
 	struct sched_context *con;
 	struct timeval start;
-	unsigned int i;
+	unsigned int num, i;
+	int *sched_ids = NULL;
 
 	switch (cmd) {
 	case CLI_INIT:
-		e->command = "sched test add";
+		e->command = "sched test";
 		e->usage = ""
-			"Usage: sched test add <num>\n"
+			"Usage: sched test <num>\n"
 			"";
 		return NULL;
 	case CLI_GENERATE:
@@ -58,24 +59,43 @@
 		return CLI_SHOWUSAGE;
 	}
 
-	if (sscanf(a->argv[e->args], "%u", &i) != 1) {
+	if (sscanf(a->argv[e->args], "%u", &num) != 1) {
 		return CLI_SHOWUSAGE;
 	}
-
-	ast_cli(a->fd, "Testing ast_sched_add() performance - timing how long it takes "
-			"to add %u entries at random time intervals from 0 to 60 seconds\n", i);
 
 	if (!(con = sched_context_create())) {
 		ast_cli(a->fd, "Test failed - could not create scheduler context\n");
 		return CLI_FAILURE;
 	}
 
+	if (!(sched_ids = ast_malloc(sizeof(*sched_ids) * num))) {
+		ast_cli(a->fd, "Test failed - memory allocation failure\n");
+		goto return_cleanup;
+	}
+
+	ast_cli(a->fd, "Testing ast_sched_add() performance - timing how long it takes "
+			"to add %u entries at random time intervals from 0 to 60 seconds\n", num);
+
 	start = ast_tvnow();
 
-	while (i--) {
+	for (i = 0; i < num; i++) {
 		int when = abs(ast_random()) % 60000;
-		if (ast_sched_add(con, when, sched_cb, NULL) == -1) {
+		if ((sched_ids[i] = ast_sched_add(con, when, sched_cb, NULL)) == -1) {
 			ast_cli(a->fd, "Test failed - sched_add returned -1\n");
+			goto return_cleanup;
+		}
+	}
+
+	ast_cli(a->fd, "Test complete - %d ms\n", ast_tvdiff_ms(ast_tvnow(), start));
+
+	ast_cli(a->fd, "Testing ast_sched_del() performance - timing how long it takes "
+			"to delete %u entries with random time intervals from 0 to 60 seconds\n", num);
+
+	start = ast_tvnow();
+
+	for (i = 0; i < num; i++) {
+		if (ast_sched_del(con, sched_ids[i]) == -1) {
+			ast_cli(a->fd, "Test failed - sched_del returned -1\n");
 			goto return_cleanup;
 		}
 	}
@@ -84,57 +104,15 @@
 
 return_cleanup:
 	sched_context_destroy(con);
-
-	return CLI_SUCCESS;
-}
-
-static char *handle_cli_sched_test_del(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
-{
-	struct sched_context *con;
-	struct timeval start;
-	unsigned int i;
-
-	switch (cmd) {
-	case CLI_INIT:
-		e->command = "sched test del";
-		e->usage = ""
-			"Usage: sched test del <num>\n"
-			"";
-		return NULL;
-	case CLI_GENERATE:
-		return NULL;
+	if (sched_ids) {
+		ast_free(sched_ids);
 	}
-
-	if (a->argc != e->args + 1) {
-		return CLI_SHOWUSAGE;
-	}
-
-	if (sscanf(a->argv[e->args], "%u", &i) != 1) {
-		return CLI_SHOWUSAGE;
-	}
-
-	ast_cli(a->fd, "Testing ast_sched_del() performance - timing how long it takes "
-			"to delete %u entries with random time intervals from 0 to 60 seconds\n", i);
-
-	if (!(con = sched_context_create())) {
-		ast_cli(a->fd, "Test failed - could not create scheduler context\n");
-		return CLI_FAILURE;
-	}
-
-	start = ast_tvnow();
-
-	/* Stuff goes here. */
-
-	ast_cli(a->fd, "Test complete - %d ms\n", ast_tvdiff_ms(ast_tvnow(), start));
-
-	sched_context_destroy(con);
 
 	return CLI_SUCCESS;
 }
 
 static struct ast_cli_entry cli_sched[] = {
-	AST_CLI_DEFINE(handle_cli_sched_test_add, "Test ast_sched_add performance"),
-	AST_CLI_DEFINE(handle_cli_sched_test_del, "Test ast_sched_del performance"),
+	AST_CLI_DEFINE(handle_cli_sched_test, "Test ast_sched add/del performance"),
 };
 
 static int unload_module(void)




More information about the svn-commits mailing list