blob: df5523a74374d56da41257085b50abbe2719a7ab [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21
22#include "test.h"
23#include <pjlib.h>
Benny Prijonofa9e5b12006-10-08 12:39:34 +000024#include <pjlib-util.h>
Benny Prijono40f2f642006-01-30 18:40:05 +000025#include <pjsip.h>
Benny Prijono5dcb38d2005-11-21 01:55:47 +000026
Benny Prijono85598d92006-01-07 18:44:25 +000027#define THIS_FILE "test.c"
28
Benny Prijono5dcb38d2005-11-21 01:55:47 +000029#define DO_TEST(test) do { \
Benny Prijono85598d92006-01-07 18:44:25 +000030 PJ_LOG(3, (THIS_FILE, "Running %s...", #test)); \
Benny Prijono5dcb38d2005-11-21 01:55:47 +000031 rc = test; \
Benny Prijono85598d92006-01-07 18:44:25 +000032 PJ_LOG(3, (THIS_FILE, \
Benny Prijono5dcb38d2005-11-21 01:55:47 +000033 "%s(%d)", \
34 (rc ? "..ERROR" : "..success"), rc)); \
35 if (rc!=0) goto on_return; \
36 } while (0)
37
Benny Prijonoe93e2872006-06-28 16:46:49 +000038#define DO_TSX_TEST(test, param) \
39 do { \
40 PJ_LOG(3, (THIS_FILE, "Running %s(%s)...", #test, (param)->tp_type)); \
41 rc = test(param); \
42 PJ_LOG(3, (THIS_FILE, \
43 "%s(%d)", \
44 (rc ? "..ERROR" : "..success"), rc)); \
45 if (rc!=0) goto on_return; \
46 } while (0)
Benny Prijono5dcb38d2005-11-21 01:55:47 +000047
48
49pjsip_endpoint *endpt;
Benny Prijono733c67a2006-06-23 01:03:52 +000050int log_level = 3;
Benny Prijonoec575c52008-10-01 10:00:37 +000051int param_log_decor = PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_TIME |
52 PJ_LOG_HAS_MICRO_SEC;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000053
Benny Prijonoe93e2872006-06-28 16:46:49 +000054static pj_oshandle_t fd_report;
55const char *system_name = "Unknown";
56static char buf[1024];
57
Benny Prijono5dcb38d2005-11-21 01:55:47 +000058void app_perror(const char *msg, pj_status_t rc)
59{
60 char errbuf[256];
61
62 PJ_CHECK_STACK();
63
Benny Prijono733c67a2006-06-23 01:03:52 +000064 pj_strerror(rc, errbuf, sizeof(errbuf));
Benny Prijono85598d92006-01-07 18:44:25 +000065 PJ_LOG(3,(THIS_FILE, "%s: [pj_status_t=%d] %s", msg, rc, errbuf));
Benny Prijono5dcb38d2005-11-21 01:55:47 +000066
67}
68
Benny Prijono85598d92006-01-07 18:44:25 +000069void flush_events(unsigned duration)
70{
71 pj_time_val stop_time;
72
73 pj_gettimeofday(&stop_time);
74 stop_time.msec += duration;
75 pj_time_val_normalize(&stop_time);
76
77 /* Process all events for the specified duration. */
78 for (;;) {
79 pj_time_val timeout = {0, 1}, now;
80
81 pjsip_endpt_handle_events(endpt, &timeout);
82
83 pj_gettimeofday(&now);
84 if (PJ_TIME_VAL_GTE(now, stop_time))
85 break;
86 }
87}
88
Benny Prijono5dcb38d2005-11-21 01:55:47 +000089pj_status_t register_static_modules(pj_size_t *count, pjsip_module **modules)
90{
Nanang Izzuddin2e4f03f2009-04-27 19:18:38 +000091 PJ_UNUSED_ARG(modules);
92
Benny Prijono5dcb38d2005-11-21 01:55:47 +000093 *count = 0;
94 return PJ_SUCCESS;
95}
96
Benny Prijonoe93e2872006-06-28 16:46:49 +000097static pj_status_t init_report(void)
98{
99 char tmp[80];
100 pj_time_val timestamp;
101 pj_parsed_time date_time;
102 pj_ssize_t len;
103 pj_status_t status;
104
105 pj_ansi_sprintf(tmp, "pjsip-static-bench-%s-%s.htm", PJ_OS_NAME, PJ_CC_NAME);
106
107 status = pj_file_open(NULL, tmp, PJ_O_WRONLY, &fd_report);
108 if (status != PJ_SUCCESS)
109 return status;
110
111 /* Title */
112 len = pj_ansi_sprintf(buf, "<HTML>\n"
113 " <HEAD>\n"
114 " <TITLE>PJSIP %s (%s) - Static Benchmark</TITLE>\n"
115 " </HEAD>\n"
116 "<BODY>\n"
117 "\n",
118 PJ_VERSION,
119 (PJ_DEBUG ? "Debug" : "Release"));
120 pj_file_write(fd_report, buf, &len);
121
122
123 /* Title */
124 len = pj_ansi_sprintf(buf, "<H1>PJSIP %s (%s) - Static Benchmark</H1>\n",
125 PJ_VERSION,
126 (PJ_DEBUG ? "Debug" : "Release"));
127 pj_file_write(fd_report, buf, &len);
128
129 len = pj_ansi_sprintf(buf, "<P>Below is the benchmark result generated "
130 "by <b>test-pjsip</b> program. The program "
131 "is single-threaded only.</P>\n");
132 pj_file_write(fd_report, buf, &len);
133
134
135 /* Write table heading */
136 len = pj_ansi_sprintf(buf, "<TABLE border=\"1\" cellpadding=\"4\">\n"
137 " <TR><TD bgColor=\"aqua\" align=\"center\">Variable</TD>\n"
138 " <TD bgColor=\"aqua\" align=\"center\">Value</TD>\n"
139 " <TD bgColor=\"aqua\" align=\"center\">Description</TD>\n"
140 " </TR>\n");
141 pj_file_write(fd_report, buf, &len);
142
143
144 /* Write version */
145 report_sval("version", PJ_VERSION, "", "PJLIB/PJSIP version");
146
147
148 /* Debug or release */
149 report_sval("build-type", (PJ_DEBUG ? "Debug" : "Release"), "", "Build type");
150
151
152 /* Write timestamp */
153 pj_gettimeofday(&timestamp);
154 report_ival("timestamp", timestamp.sec, "", "System timestamp of the test");
155
156
157 /* Write time of day */
158 pj_time_decode(&timestamp, &date_time);
159 len = pj_ansi_sprintf(tmp, "%04d-%02d-%02d %02d:%02d:%02d",
160 date_time.year, date_time.mon+1, date_time.day,
161 date_time.hour, date_time.min, date_time.sec);
162 report_sval("date-time", tmp, "", "Date/time of the test");
163
164
165 /* Write System */
166 report_sval("system", system_name, "", "System description");
167
168
169 /* Write OS type */
170 report_sval("os-family", PJ_OS_NAME, "", "Operating system family");
171
172
173 /* Write CC name */
174 len = pj_ansi_sprintf(tmp, "%s-%d.%d.%d", PJ_CC_NAME,
175 PJ_CC_VER_1, PJ_CC_VER_2, PJ_CC_VER_2);
176 report_sval("cc-name", tmp, "", "Compiler name and version");
177
178
179 return PJ_SUCCESS;
180}
181
182void report_sval(const char *name, const char* value, const char *valname,
183 const char *desc)
184{
185 pj_ssize_t len;
186
187 len = pj_ansi_sprintf(buf, " <TR><TD><TT>%s</TT></TD>\n"
188 " <TD align=\"right\"><B>%s %s</B></TD>\n"
189 " <TD>%s</TD>\n"
190 " </TR>\n",
191 name, value, valname, desc);
192 pj_file_write(fd_report, buf, &len);
193}
194
195
196void report_ival(const char *name, int value, const char *valname,
197 const char *desc)
198{
199 pj_ssize_t len;
200
201 len = pj_ansi_sprintf(buf, " <TR><TD><TT>%s</TT></TD>\n"
202 " <TD align=\"right\"><B>%d %s</B></TD>\n"
203 " <TD>%s</TD>\n"
204 " </TR>\n",
205 name, value, valname, desc);
206 pj_file_write(fd_report, buf, &len);
207
208}
209
210static void close_report(void)
211{
212 pj_ssize_t len;
213
214 if (fd_report) {
215 len = pj_ansi_sprintf(buf, "</TABLE>\n</BODY>\n</HTML>\n");
216 pj_file_write(fd_report, buf, &len);
217
218 pj_file_close(fd_report);
219 }
220}
221
222
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000223int test_main(void)
224{
225 pj_status_t rc;
226 pj_caching_pool caching_pool;
227 const char *filename;
Benny Prijonobc331ca2006-09-19 13:32:05 +0000228 unsigned tsx_test_cnt=0;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000229 struct tsx_test_param tsx_test[10];
230 pj_status_t status;
Benny Prijonobc331ca2006-09-19 13:32:05 +0000231#if INCLUDE_TSX_TEST
232 unsigned i;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000233 pjsip_transport *tp;
Benny Prijono3569c0d2007-04-06 10:29:20 +0000234#if PJ_HAS_TCP
Benny Prijonoe93e2872006-06-28 16:46:49 +0000235 pjsip_tpfactory *tpfactory;
Benny Prijono3569c0d2007-04-06 10:29:20 +0000236#endif /* PJ_HAS_TCP */
237#endif /* INCLUDE_TSX_TEST */
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000238 int line;
239
Benny Prijono9c1d9f52006-01-07 23:01:56 +0000240 pj_log_set_level(log_level);
Benny Prijonoec575c52008-10-01 10:00:37 +0000241 pj_log_set_decor(param_log_decor);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000242
243 if ((rc=pj_init()) != PJ_SUCCESS) {
244 app_perror("pj_init", rc);
245 return rc;
246 }
247
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000248 if ((rc=pjlib_util_init()) != PJ_SUCCESS) {
249 app_perror("pj_init", rc);
250 return rc;
251 }
252
Benny Prijonoe93e2872006-06-28 16:46:49 +0000253 status = init_report();
254 if (status != PJ_SUCCESS)
255 return status;
256
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000257 pj_dump_config();
258
Benny Prijono95c63482006-07-06 14:28:45 +0000259 pj_caching_pool_init( &caching_pool, &pj_pool_factory_default_policy,
260 PJSIP_TEST_MEM_SIZE );
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000261
262 rc = pjsip_endpt_create(&caching_pool.factory, "endpt", &endpt);
263 if (rc != PJ_SUCCESS) {
264 app_perror("pjsip_endpt_create", rc);
265 pj_caching_pool_destroy(&caching_pool);
266 return rc;
267 }
268
Benny Prijono85598d92006-01-07 18:44:25 +0000269 PJ_LOG(3,(THIS_FILE,""));
270
271 /* Init logger module. */
272 init_msg_logger();
273 msg_logger_set_enabled(1);
274
275 /* Start transaction layer module. */
Benny Prijono733c67a2006-06-23 01:03:52 +0000276 rc = pjsip_tsx_layer_init_module(endpt);
Benny Prijono85598d92006-01-07 18:44:25 +0000277 if (rc != PJ_SUCCESS) {
278 app_perror(" Error initializing transaction module", rc);
279 goto on_return;
280 }
281
282 /* Create loop transport. */
283 rc = pjsip_loop_start(endpt, NULL);
284 if (rc != PJ_SUCCESS) {
285 app_perror(" error: unable to create datagram loop transport",
286 rc);
287 goto on_return;
288 }
Benny Prijonoe93e2872006-06-28 16:46:49 +0000289 tsx_test[tsx_test_cnt].port = 5060;
290 tsx_test[tsx_test_cnt].tp_type = "loop-dgram";
291 tsx_test[tsx_test_cnt].type = PJSIP_TRANSPORT_LOOP_DGRAM;
292 ++tsx_test_cnt;
293
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000294
Benny Prijono02b8fd82006-06-26 15:12:55 +0000295#if INCLUDE_URI_TEST
Benny Prijono733c67a2006-06-23 01:03:52 +0000296 DO_TEST(uri_test());
Benny Prijono02b8fd82006-06-26 15:12:55 +0000297#endif
298
299#if INCLUDE_MSG_TEST
Benny Prijono733c67a2006-06-23 01:03:52 +0000300 DO_TEST(msg_test());
301 DO_TEST(msg_err_test());
Benny Prijono02b8fd82006-06-26 15:12:55 +0000302#endif
303
304#if INCLUDE_TXDATA_TEST
Benny Prijono733c67a2006-06-23 01:03:52 +0000305 DO_TEST(txdata_test());
Benny Prijono02b8fd82006-06-26 15:12:55 +0000306#endif
307
Benny Prijonoe93e2872006-06-28 16:46:49 +0000308#if INCLUDE_TSX_BENCH
309 DO_TEST(tsx_bench());
310#endif
311
Benny Prijono02b8fd82006-06-26 15:12:55 +0000312#if INCLUDE_UDP_TEST
Benny Prijono733c67a2006-06-23 01:03:52 +0000313 DO_TEST(transport_udp_test());
Benny Prijono02b8fd82006-06-26 15:12:55 +0000314#endif
315
316#if INCLUDE_LOOP_TEST
Benny Prijono733c67a2006-06-23 01:03:52 +0000317 DO_TEST(transport_loop_test());
Benny Prijono02b8fd82006-06-26 15:12:55 +0000318#endif
319
Benny Prijonoe93e2872006-06-28 16:46:49 +0000320#if INCLUDE_TCP_TEST
321 DO_TEST(transport_tcp_test());
322#endif
323
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000324#if INCLUDE_RESOLVE_TEST
325 DO_TEST(resolve_test());
326#endif
327
Benny Prijonoe93e2872006-06-28 16:46:49 +0000328
Benny Prijono02b8fd82006-06-26 15:12:55 +0000329#if INCLUDE_TSX_TEST
Benny Prijonoe93e2872006-06-28 16:46:49 +0000330 status = pjsip_udp_transport_start(endpt, NULL, NULL, 1, &tp);
331 if (status == PJ_SUCCESS) {
332 tsx_test[tsx_test_cnt].port = tp->local_name.port;
333 tsx_test[tsx_test_cnt].tp_type = "udp";
334 tsx_test[tsx_test_cnt].type = PJSIP_TRANSPORT_UDP;
335 ++tsx_test_cnt;
336 }
337
Benny Prijono3569c0d2007-04-06 10:29:20 +0000338#if PJ_HAS_TCP
Benny Prijonoe93e2872006-06-28 16:46:49 +0000339 status = pjsip_tcp_transport_start(endpt, NULL, 1, &tpfactory);
340 if (status == PJ_SUCCESS) {
341 tsx_test[tsx_test_cnt].port = tpfactory->addr_name.port;
342 tsx_test[tsx_test_cnt].tp_type = "tcp";
343 tsx_test[tsx_test_cnt].type = PJSIP_TRANSPORT_TCP;
344 ++tsx_test_cnt;
345 } else {
346 app_perror("Unable to create TCP", status);
347 rc = -4;
348 goto on_return;
349 }
Benny Prijono3569c0d2007-04-06 10:29:20 +0000350#endif
Benny Prijonoe93e2872006-06-28 16:46:49 +0000351
352
353 for (i=0; i<tsx_test_cnt; ++i) {
354 DO_TSX_TEST(tsx_basic_test, &tsx_test[i]);
355 DO_TSX_TEST(tsx_uac_test, &tsx_test[i]);
356 DO_TSX_TEST(tsx_uas_test, &tsx_test[i]);
357 }
Benny Prijono02b8fd82006-06-26 15:12:55 +0000358#endif
359
Benny Prijono1f7767b2007-10-03 18:28:49 +0000360#if INCLUDE_INV_OA_TEST
361 DO_TEST(inv_offer_answer_test());
362#endif
363
Benny Prijonodd742da2008-05-17 12:45:00 +0000364#if INCLUDE_REGC_TEST
365 DO_TEST(regc_test());
366#endif
367
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000368
369on_return:
Benny Prijonoe93e2872006-06-28 16:46:49 +0000370 flush_events(500);
Benny Prijono95c63482006-07-06 14:28:45 +0000371
372 /* Dumping memory pool usage */
Benny Prijono1ef06df2006-07-09 10:06:44 +0000373 PJ_LOG(3,(THIS_FILE, "Peak memory size=%u MB",
374 caching_pool.peak_used_size / 1000000));
Benny Prijono95c63482006-07-06 14:28:45 +0000375
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000376 pjsip_endpt_destroy(endpt);
377 pj_caching_pool_destroy(&caching_pool);
378
Benny Prijono85598d92006-01-07 18:44:25 +0000379 PJ_LOG(3,(THIS_FILE, ""));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000380
381 pj_thread_get_stack_info(pj_thread_this(), &filename, &line);
Benny Prijono85598d92006-01-07 18:44:25 +0000382 PJ_LOG(3,(THIS_FILE, "Stack max usage: %u, deepest: %s:%u",
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000383 pj_thread_get_stack_max_usage(pj_thread_this()),
384 filename, line));
385 if (rc == 0)
Benny Prijono85598d92006-01-07 18:44:25 +0000386 PJ_LOG(3,(THIS_FILE, "Looks like everything is okay!.."));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000387 else
Benny Prijono85598d92006-01-07 18:44:25 +0000388 PJ_LOG(3,(THIS_FILE, "Test completed with error(s)"));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000389
Benny Prijonoe93e2872006-06-28 16:46:49 +0000390 report_ival("test-status", rc, "", "Overall test status/result (0==success)");
391 close_report();
Benny Prijono733c67a2006-06-23 01:03:52 +0000392 return rc;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000393}
394