1
/* $Id: test.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
24
#include <pjlib-util.h>
27
#define THIS_FILE "test.c"
29
#define DO_TEST(test) do { \
30
PJ_LOG(3, (THIS_FILE, "Running %s...", #test)); \
32
PJ_LOG(3, (THIS_FILE, \
34
(rc ? "..ERROR" : "..success"), rc)); \
35
if (rc!=0) goto on_return; \
38
#define DO_TSX_TEST(test, param) \
40
PJ_LOG(3, (THIS_FILE, "Running %s(%s)...", #test, (param)->tp_type)); \
42
PJ_LOG(3, (THIS_FILE, \
44
(rc ? "..ERROR" : "..success"), rc)); \
45
if (rc!=0) goto on_return; \
49
pjsip_endpoint *endpt;
51
int param_log_decor = PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_TIME |
54
static pj_oshandle_t fd_report;
55
const char *system_name = "Unknown";
56
static char buf[1024];
58
void app_perror(const char *msg, pj_status_t rc)
64
pj_strerror(rc, errbuf, sizeof(errbuf));
65
PJ_LOG(3,(THIS_FILE, "%s: [pj_status_t=%d] %s", msg, rc, errbuf));
69
void flush_events(unsigned duration)
71
pj_time_val stop_time;
73
pj_gettimeofday(&stop_time);
74
stop_time.msec += duration;
75
pj_time_val_normalize(&stop_time);
77
/* Process all events for the specified duration. */
79
pj_time_val timeout = {0, 1}, now;
81
pjsip_endpt_handle_events(endpt, &timeout);
83
pj_gettimeofday(&now);
84
if (PJ_TIME_VAL_GTE(now, stop_time))
89
pj_status_t register_static_modules(pj_size_t *count, pjsip_module **modules)
91
PJ_UNUSED_ARG(modules);
97
static pj_status_t init_report(void)
100
pj_time_val timestamp;
101
pj_parsed_time date_time;
105
pj_ansi_sprintf(tmp, "pjsip-static-bench-%s-%s.htm", PJ_OS_NAME, PJ_CC_NAME);
107
status = pj_file_open(NULL, tmp, PJ_O_WRONLY, &fd_report);
108
if (status != PJ_SUCCESS)
112
len = pj_ansi_sprintf(buf, "<HTML>\n"
114
" <TITLE>PJSIP %s (%s) - Static Benchmark</TITLE>\n"
119
(PJ_DEBUG ? "Debug" : "Release"));
120
pj_file_write(fd_report, buf, &len);
124
len = pj_ansi_sprintf(buf, "<H1>PJSIP %s (%s) - Static Benchmark</H1>\n",
126
(PJ_DEBUG ? "Debug" : "Release"));
127
pj_file_write(fd_report, buf, &len);
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);
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"
141
pj_file_write(fd_report, buf, &len);
145
report_sval("version", PJ_VERSION, "", "PJLIB/PJSIP version");
148
/* Debug or release */
149
report_sval("build-type", (PJ_DEBUG ? "Debug" : "Release"), "", "Build type");
152
/* Write timestamp */
153
pj_gettimeofday(×tamp);
154
report_ival("timestamp", timestamp.sec, "", "System timestamp of the test");
157
/* Write time of day */
158
pj_time_decode(×tamp, &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");
166
report_sval("system", system_name, "", "System description");
170
report_sval("os-family", PJ_OS_NAME, "", "Operating system family");
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");
182
void report_sval(const char *name, const char* value, const char *valname,
187
len = pj_ansi_sprintf(buf, " <TR><TD><TT>%s</TT></TD>\n"
188
" <TD align=\"right\"><B>%s %s</B></TD>\n"
191
name, value, valname, desc);
192
pj_file_write(fd_report, buf, &len);
196
void report_ival(const char *name, int value, const char *valname,
201
len = pj_ansi_sprintf(buf, " <TR><TD><TT>%s</TT></TD>\n"
202
" <TD align=\"right\"><B>%d %s</B></TD>\n"
205
name, value, valname, desc);
206
pj_file_write(fd_report, buf, &len);
210
static void close_report(void)
215
len = pj_ansi_sprintf(buf, "</TABLE>\n</BODY>\n</HTML>\n");
216
pj_file_write(fd_report, buf, &len);
218
pj_file_close(fd_report);
226
pj_caching_pool caching_pool;
227
const char *filename;
228
unsigned tsx_test_cnt=0;
229
struct tsx_test_param tsx_test[10];
235
pjsip_tpfactory *tpfactory;
236
#endif /* PJ_HAS_TCP */
237
#endif /* INCLUDE_TSX_TEST */
240
pj_log_set_level(log_level);
241
pj_log_set_decor(param_log_decor);
243
if ((rc=pj_init()) != PJ_SUCCESS) {
244
app_perror("pj_init", rc);
248
if ((rc=pjlib_util_init()) != PJ_SUCCESS) {
249
app_perror("pj_init", rc);
253
status = init_report();
254
if (status != PJ_SUCCESS)
259
pj_caching_pool_init( &caching_pool, &pj_pool_factory_default_policy,
260
PJSIP_TEST_MEM_SIZE );
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);
269
PJ_LOG(3,(THIS_FILE,""));
271
/* Init logger module. */
273
msg_logger_set_enabled(1);
275
/* Start transaction layer module. */
276
rc = pjsip_tsx_layer_init_module(endpt);
277
if (rc != PJ_SUCCESS) {
278
app_perror(" Error initializing transaction module", rc);
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",
289
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;
301
DO_TEST(msg_err_test());
304
#if INCLUDE_MULTIPART_TEST
305
DO_TEST(multipart_test());
308
#if INCLUDE_TXDATA_TEST
309
DO_TEST(txdata_test());
312
#if INCLUDE_TSX_BENCH
313
DO_TEST(tsx_bench());
317
DO_TEST(transport_udp_test());
320
#if INCLUDE_LOOP_TEST
321
DO_TEST(transport_loop_test());
325
DO_TEST(transport_tcp_test());
328
#if INCLUDE_RESOLVE_TEST
329
DO_TEST(resolve_test());
334
status = pjsip_udp_transport_start(endpt, NULL, NULL, 1, &tp);
335
if (status == PJ_SUCCESS) {
336
tsx_test[tsx_test_cnt].port = tp->local_name.port;
337
tsx_test[tsx_test_cnt].tp_type = "udp";
338
tsx_test[tsx_test_cnt].type = PJSIP_TRANSPORT_UDP;
343
status = pjsip_tcp_transport_start(endpt, NULL, 1, &tpfactory);
344
if (status == PJ_SUCCESS) {
345
tsx_test[tsx_test_cnt].port = tpfactory->addr_name.port;
346
tsx_test[tsx_test_cnt].tp_type = "tcp";
347
tsx_test[tsx_test_cnt].type = PJSIP_TRANSPORT_TCP;
350
app_perror("Unable to create TCP", status);
357
for (i=0; i<tsx_test_cnt; ++i) {
358
DO_TSX_TEST(tsx_basic_test, &tsx_test[i]);
359
DO_TSX_TEST(tsx_uac_test, &tsx_test[i]);
360
DO_TSX_TEST(tsx_uas_test, &tsx_test[i]);
364
#if INCLUDE_INV_OA_TEST
365
DO_TEST(inv_offer_answer_test());
368
#if INCLUDE_REGC_TEST
369
DO_TEST(regc_test());
376
/* Dumping memory pool usage */
377
PJ_LOG(3,(THIS_FILE, "Peak memory size=%u MB",
378
caching_pool.peak_used_size / 1000000));
380
pjsip_endpt_destroy(endpt);
381
pj_caching_pool_destroy(&caching_pool);
383
PJ_LOG(3,(THIS_FILE, ""));
385
pj_thread_get_stack_info(pj_thread_this(), &filename, &line);
386
PJ_LOG(3,(THIS_FILE, "Stack max usage: %u, deepest: %s:%u",
387
pj_thread_get_stack_max_usage(pj_thread_this()),
390
PJ_LOG(3,(THIS_FILE, "Looks like everything is okay!.."));
392
PJ_LOG(3,(THIS_FILE, "Test completed with error(s)"));
394
report_ival("test-status", rc, "", "Overall test status/result (0==success)");