Merged revisions 328247 via svnmerge from
[asterisk/asterisk.git] / tests / test_logger.c
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
19 /*! \file
20  *
21  * \brief Test module for the logging subsystem
22  *
23  * \author\verbatim Kevin P. Fleming <kpfleming@digium.com> \endverbatim
24  *
25  * \ingroup tests
26  */
27
28 /*** MODULEINFO
29         <defaultenabled>no</defaultenabled>
30         <support_level>extended</support_level>
31  ***/
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
37 #include "asterisk/file.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/module.h"
41 #include "asterisk/lock.h"
42 #include "asterisk/app.h"
43 #include "asterisk/cli.h"
44
45 struct test {
46         const char *name;
47         unsigned int x_success;
48         unsigned int x_failure;
49         unsigned int u_success;
50         unsigned int u_failure;
51 };
52
53 static void output_tests(struct test *tests, size_t num_tests, int fd)
54 {
55         unsigned int x;
56
57         for (x = 0; x < num_tests; x++) {
58                 ast_cli(fd, "Test %d: %s\n", x + 1, tests[x].name);
59                 ast_cli(fd, "\tExpected Successes: %d\n", tests[x].x_success);
60                 ast_cli(fd, "\tExpected Failures: %d\n", tests[x].x_failure);
61                 ast_cli(fd, "\tUnexpected Successes: %d\n", tests[x].u_success);
62                 ast_cli(fd, "\tUnexpected Failures: %d\n", tests[x].u_failure);
63                 ast_cli(fd, "Test %d Result: %s\n", x + 1, (tests[x].u_success + tests[x].u_failure) ? "FAIL" : "PASS");
64         }
65 }
66
67 static char *handle_cli_dynamic_level_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
68 {
69         unsigned int level;
70         unsigned int x;
71         unsigned int test;
72         struct test tests[] = {
73                 { .name = "Simple register/message/unregister",
74                 },
75                 { .name = "Register multiple levels",
76                 },
77         };
78
79         switch (cmd) {
80         case CLI_INIT:
81                 e->command = "logger test dynamic";
82                 e->usage = ""
83                         "Usage: logger test dynamic\n"
84                         "";
85                 return NULL;
86         case CLI_GENERATE:
87                 return NULL;
88         }
89
90         for (test = 0; test < ARRAY_LEN(tests); test++) {
91                 ast_cli(a->fd, "Test %d: %s.\n", test + 1, tests[test].name);
92                 switch (test) {
93                 case 0:
94                         if ((level = ast_logger_register_level("test")) != -1) {
95                                 ast_cli(a->fd, "Test: got level %d\n", level);
96                                 ast_log_dynamic_level(level, "Logger Dynamic Test: Test 1\n");
97                                 ast_logger_unregister_level("test");
98                                 tests[test].x_success++;
99                         } else {
100                                 ast_cli(a->fd, "Test: Failed, could not register level 'test'.\n");
101                                 tests[test].u_failure++;
102                         }
103                         break;
104                 case 1:
105                 {
106                         char level_name[18][8];
107
108                         for (x = 0; x < ARRAY_LEN(level_name); x++) {
109                                 sprintf(level_name[x], "level%02d", x);
110                                 if ((level = ast_logger_register_level(level_name[x])) == -1) {
111                                         if (x < 16) {
112                                                 tests[test].u_failure++;
113                                         } else {
114                                                 tests[test].x_failure++;
115                                         }
116                                         level_name[x][0] = '\0';
117                                 } else {
118                                         ast_cli(a->fd, "Test: registered '%s', got level %d\n", level_name[x], level);
119                                         if (x < 16) {
120                                                 tests[test].x_success++;
121                                         } else {
122                                                 tests[test].u_success++;
123                                         }
124                                 }
125                         }
126
127                         for (x = 0; x < ARRAY_LEN(level_name); x++) {
128                                 if (!ast_strlen_zero(level_name[x])) {
129                                         ast_logger_unregister_level(level_name[x]);
130                                 }
131                         }
132                 }
133                 }
134         }
135
136         output_tests(tests, ARRAY_LEN(tests), a->fd);
137
138         return CLI_SUCCESS;
139 }
140
141 static char *handle_cli_performance_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
142 {
143         unsigned int level;
144         unsigned int test;
145         struct test tests[] = {
146                 { .name = "Log 10,000 messages",
147                 },
148         };
149
150         switch (cmd) {
151         case CLI_INIT:
152                 e->command = "logger test performance";
153                 e->usage = ""
154                         "Usage: logger test performance\n"
155                         "";
156                 return NULL;
157         case CLI_GENERATE:
158                 return NULL;
159         }
160
161         for (test = 0; test < ARRAY_LEN(tests); test++) {
162                 ast_cli(a->fd, "Test %d: %s.\n", test + 1, tests[test].name);
163                 switch (test) {
164                 case 0:
165                         if ((level = ast_logger_register_level("perftest")) != -1) {
166                                 unsigned int x;
167                                 struct timeval start, end;
168                                 int elapsed;
169
170                                 ast_cli(a->fd, "Test: got level %d\n", level);
171                                 start = ast_tvnow();
172                                 for (x = 0; x < 10000; x++) {
173                                         ast_log_dynamic_level(level, "Performance test log message\n");
174                                 }
175                                 end = ast_tvnow();
176                                 elapsed = ast_tvdiff_ms(end, start);
177                                 ast_cli(a->fd, "Test: 10,000 messages in %f seconds.\n", (float) elapsed / 1000);
178                                 ast_logger_unregister_level("perftest");
179                                 tests[test].x_success++;
180                         } else {
181                                 ast_cli(a->fd, "Test: Failed, could not register level 'perftest'.\n");
182                                 tests[test].u_failure++;
183                         }
184                         break;
185                 }
186         }
187
188         output_tests(tests, ARRAY_LEN(tests), a->fd);
189
190         return CLI_SUCCESS;
191 }
192
193 static struct ast_cli_entry cli_logger[] = {
194         AST_CLI_DEFINE(handle_cli_dynamic_level_test, "Test the dynamic logger level implementation"),
195         AST_CLI_DEFINE(handle_cli_performance_test, "Test the logger performance"),
196 };
197
198 static int unload_module(void)
199 {
200         ast_cli_unregister_multiple(cli_logger, ARRAY_LEN(cli_logger));
201         return 0;
202 }
203
204 static int load_module(void)
205 {
206         ast_cli_register_multiple(cli_logger, ARRAY_LEN(cli_logger));
207         return AST_MODULE_LOAD_SUCCESS;
208 }
209
210 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Logger Test Module");