blob: 8cf76eed8597e5b24f0437cfb052e6ff060a91ba [file] [log] [blame]
Benny Prijono514ca6b2006-07-03 01:30:01 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20
21/**
22 * \page page_pjsip_perf_c Samples: SIP Performance Benchmark
23 *
24 * <b>pjsip-perf</b> is a complete program to measure the
25 * performance of PJSIP or other SIP endpoints. It consists of two
26 * parts:
27 * - the server, to respond incoming requests, and
28 * - the client, who actively submits requests and measure the
29 * performance of the server.
30 *
31 * Both server and client part can run simultaneously, to measure the
32 * performance when both endpoints are co-located in a single program.
33 *
34 * The server accepts both INVITE and non-INVITE requests.
35 * The server exports several different types of URL, which would
36 * control how the request would be handled by the server:
37 * - URL with "0" as the user part will be handled statelessly.
38 * It should not be used with INVITE method.
39 * - URL with "1" as the user part will be handled statefully.
40 * If the request is an INVITE request, INVITE transaction will
41 * be created and 200/OK response will be sent, along with a valid
42 * SDP body. However, the SDP is just a static text body, and
43 * is not a proper SDP generated by PJMEDIA.
44 * - URL with "2" as the user part is only meaningful for INVITE
45 * requests, as it would be handled <b>call-statefully</b> by the
46 * server. For this URL, the server also would generate SDP dynamically
47 * and perform a proper SDP negotiation for the incoming call.
48 * Also for every call, server will limit the call duration to
49 * 10 seconds, on which the call will be terminated if the client
50 * doesn't hangup the call.
51 *
52 *
53 *
54 * This file is pjsip-apps/src/samples/pjsip-perf.c
55 *
56 * \includelineno pjsip-perf.c
57 */
58
59/* Include all headers. */
60#include <pjsip.h>
61#include <pjmedia.h>
62#include <pjmedia-codec.h>
63#include <pjsip_ua.h>
64#include <pjsip_simple.h>
65#include <pjlib-util.h>
66#include <pjlib.h>
67#include <stdio.h>
68
Benny Prijono1479b652006-07-03 14:18:17 +000069#if defined(PJ_WIN32) && PJ_WIN32!=0
70# include <windows.h>
71#endif
72
73#define THIS_FILE "pjsip-perf.c"
74#define DEFAULT_COUNT (PJSIP_MAX_TSX_COUNT/2>10000?10000:PJSIP_MAX_TSX_COUNT/2)
Benny Prijonoc3573762006-07-10 21:39:24 +000075#define JOB_WINDOW 1000
Benny Prijono1479b652006-07-03 14:18:17 +000076#define TERMINATE_TSX(x,c)
77
78
79#ifndef CACHING_POOL_SIZE
80# define CACHING_POOL_SIZE (256*1024*1024)
81#endif
Benny Prijono514ca6b2006-07-03 01:30:01 +000082
83
84/* Static message body for INVITE, when stateful processing is
85 * invoked (instead of call-stateful, where SDP is generated
86 * dynamically.
87 */
88static pj_str_t dummy_sdp_str =
89{
90 "v=0\r\n"
91 "o=- 3360842071 3360842071 IN IP4 192.168.0.68\r\n"
92 "s=pjmedia\r\n"
93 "c=IN IP4 192.168.0.68\r\n"
94 "t=0 0\r\n"
Benny Prijonofc290a62006-08-13 18:20:55 +000095 "m=audio 4000 RTP/AVP 0 8 3 103 102 101\r\n"
Benny Prijono514ca6b2006-07-03 01:30:01 +000096 "a=rtcp:4001 IN IP4 192.168.0.68\r\n"
97 "a=rtpmap:103 speex/16000\r\n"
98 "a=rtpmap:102 speex/8000\r\n"
99 "a=rtpmap:3 GSM/8000\r\n"
100 "a=rtpmap:0 PCMU/8000\r\n"
101 "a=rtpmap:8 PCMA/8000\r\n"
102 "a=sendrecv\r\n"
103 "a=rtpmap:101 telephone-event/8000\r\n"
104 "a=fmtp:101 0-15\r\n",
105 0
106};
107
108static pj_str_t mime_application = { "application", 11};
109static pj_str_t mime_sdp = {"sdp", 3};
110
111
112struct srv_state
113{
114 unsigned stateless_cnt;
115 unsigned stateful_cnt;
116 unsigned call_cnt;
117};
118
119
120struct app
121{
122 pj_caching_pool cp;
123 pj_pool_t *pool;
Benny Prijonoc03a3c52006-07-03 02:31:10 +0000124 pj_bool_t use_tcp;
Benny Prijono514ca6b2006-07-03 01:30:01 +0000125 pj_str_t local_addr;
126 int local_port;
127 pjsip_endpoint *sip_endpt;
128 pjmedia_endpt *med_endpt;
129 pj_str_t local_uri;
130 pj_str_t local_contact;
131 unsigned skinfo_cnt;
132 pjmedia_sock_info skinfo[8];
133
134 pj_bool_t thread_quit;
135 unsigned thread_count;
136 pj_thread_t *thread[16];
137
138 pj_bool_t real_sdp;
139 pjmedia_sdp_session *dummy_sdp;
Benny Prijonoc3573762006-07-10 21:39:24 +0000140
141 int log_level;
Benny Prijono514ca6b2006-07-03 01:30:01 +0000142
143 struct {
144 pjsip_method method;
145 pj_str_t dst_uri;
146 pj_bool_t stateless;
147 unsigned timeout;
148 unsigned job_count,
149 job_submitted,
150 job_finished,
151 job_window;
Benny Prijono1479b652006-07-03 14:18:17 +0000152 unsigned stat_max_window;
Benny Prijono514ca6b2006-07-03 01:30:01 +0000153 pj_time_val first_request;
Benny Prijonoc3573762006-07-10 21:39:24 +0000154 pj_time_val requests_sent;
Benny Prijono514ca6b2006-07-03 01:30:01 +0000155 pj_time_val last_completion;
156 unsigned total_responses;
Benny Prijono49f682a2006-07-11 12:25:45 +0000157 unsigned response_codes[800];
Benny Prijono514ca6b2006-07-03 01:30:01 +0000158 } client;
159
160 struct {
Benny Prijonof521eb02006-08-06 23:07:25 +0000161 pj_bool_t send_trying;
162 pj_bool_t send_ringing;
163 unsigned delay;
Benny Prijono514ca6b2006-07-03 01:30:01 +0000164 struct srv_state prev_state;
165 struct srv_state cur_state;
166 } server;
167
168
169} app;
170
171struct call
172{
173 pjsip_inv_session *inv;
Benny Prijonof521eb02006-08-06 23:07:25 +0000174 pj_timer_entry ans_timer;
Benny Prijono514ca6b2006-07-03 01:30:01 +0000175};
176
177
178static void app_perror(const char *sender, const char *title,
179 pj_status_t status)
180{
181 char errmsg[PJ_ERR_MSG_SIZE];
182
183 pj_strerror(status, errmsg, sizeof(errmsg));
184 PJ_LOG(1,(sender, "%s: %s [code=%d]", title, errmsg, status));
185}
186
187
188/**************************************************************************
189 * STATELESS SERVER
190 */
191static pj_bool_t mod_stateless_on_rx_request(pjsip_rx_data *rdata);
192
193/* Module to handle incoming requests statelessly.
194 */
195static pjsip_module mod_stateless_server =
196{
197 NULL, NULL, /* prev, next. */
198 { "mod-stateless-server", 20 }, /* Name. */
199 -1, /* Id */
200 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
201 NULL, /* load() */
202 NULL, /* start() */
203 NULL, /* stop() */
204 NULL, /* unload() */
205 &mod_stateless_on_rx_request, /* on_rx_request() */
206 NULL, /* on_rx_response() */
207 NULL, /* on_tx_request. */
208 NULL, /* on_tx_response() */
209 NULL, /* on_tsx_state() */
210};
211
212
213static pj_bool_t mod_stateless_on_rx_request(pjsip_rx_data *rdata)
214{
215 const pj_str_t stateless_user = { "0", 1 };
216 pjsip_uri *uri;
217 pjsip_sip_uri *sip_uri;
218
219 uri = pjsip_uri_get_uri(rdata->msg_info.msg->line.req.uri);
220
221 /* Only want to receive SIP scheme */
222 if (!PJSIP_URI_SCHEME_IS_SIP(uri))
223 return PJ_FALSE;
224
225 sip_uri = (pjsip_sip_uri*) uri;
226
227 /* Check for matching user part */
228 if (pj_strcmp(&sip_uri->user, &stateless_user)!=0)
229 return PJ_FALSE;
230
231 /*
232 * Yes, this is for us.
233 */
234
235 /* Ignore ACK request */
236 if (rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD)
237 return PJ_TRUE;
238
239 /*
240 * Respond statelessly with 200/OK.
241 */
242 pjsip_endpt_respond_stateless(app.sip_endpt, rdata, 200, NULL,
243 NULL, NULL);
244 app.server.cur_state.stateless_cnt++;
245 return PJ_TRUE;
246}
247
248
249/**************************************************************************
250 * STATEFUL SERVER
251 */
252static pj_bool_t mod_stateful_on_rx_request(pjsip_rx_data *rdata);
253
254/* Module to handle incoming requests statefully.
255 */
256static pjsip_module mod_stateful_server =
257{
258 NULL, NULL, /* prev, next. */
259 { "mod-stateful-server", 19 }, /* Name. */
260 -1, /* Id */
261 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
262 NULL, /* load() */
263 NULL, /* start() */
264 NULL, /* stop() */
265 NULL, /* unload() */
266 &mod_stateful_on_rx_request, /* on_rx_request() */
267 NULL, /* on_rx_response() */
268 NULL, /* on_tx_request. */
269 NULL, /* on_tx_response() */
270 NULL, /* on_tsx_state() */
271};
272
273
274static pj_bool_t mod_stateful_on_rx_request(pjsip_rx_data *rdata)
275{
276 const pj_str_t stateful_user = { "1", 1 };
277 pjsip_uri *uri;
278 pjsip_sip_uri *sip_uri;
279
280 uri = pjsip_uri_get_uri(rdata->msg_info.msg->line.req.uri);
281
282 /* Only want to receive SIP scheme */
283 if (!PJSIP_URI_SCHEME_IS_SIP(uri))
284 return PJ_FALSE;
285
286 sip_uri = (pjsip_sip_uri*) uri;
287
288 /* Check for matching user part */
289 if (pj_strcmp(&sip_uri->user, &stateful_user)!=0)
290 return PJ_FALSE;
291
292 /*
293 * Yes, this is for us.
294 * Respond statefully with 200/OK.
295 */
296 switch (rdata->msg_info.msg->line.req.method.id) {
297 case PJSIP_INVITE_METHOD:
298 {
299 pjsip_msg_body *body;
300
301 if (dummy_sdp_str.slen == 0)
302 dummy_sdp_str.slen = pj_ansi_strlen(dummy_sdp_str.ptr);
303
304 body = pjsip_msg_body_create(rdata->tp_info.pool,
305 &mime_application, &mime_sdp,
306 &dummy_sdp_str);
307 pjsip_endpt_respond(app.sip_endpt, &mod_stateful_server, rdata,
308 200, NULL, NULL, body, NULL);
309 }
310 break;
311 case PJSIP_ACK_METHOD:
312 return PJ_TRUE;
313 default:
314 pjsip_endpt_respond(app.sip_endpt, &mod_stateful_server, rdata,
315 200, NULL, NULL, NULL, NULL);
316 break;
317 }
318
319 app.server.cur_state.stateful_cnt++;
320 return PJ_TRUE;
321}
322
323
324/**************************************************************************
325 * CALL SERVER
326 */
327static pj_bool_t mod_call_on_rx_request(pjsip_rx_data *rdata);
328
329/* Module to handle incoming requests callly.
330 */
331static pjsip_module mod_call_server =
332{
333 NULL, NULL, /* prev, next. */
334 { "mod-call-server", 15 }, /* Name. */
335 -1, /* Id */
336 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
337 NULL, /* load() */
338 NULL, /* start() */
339 NULL, /* stop() */
340 NULL, /* unload() */
341 &mod_call_on_rx_request, /* on_rx_request() */
342 NULL, /* on_rx_response() */
343 NULL, /* on_tx_request. */
344 NULL, /* on_tx_response() */
345 NULL, /* on_tsx_state() */
346};
347
348
Benny Prijonof521eb02006-08-06 23:07:25 +0000349static pj_status_t send_response(pjsip_inv_session *inv,
350 pjsip_rx_data *rdata,
351 int code,
352 pj_bool_t *has_initial)
353{
354 pjsip_tx_data *tdata;
355 pj_status_t status;
356
357 if (*has_initial) {
358 status = pjsip_inv_answer(inv, code, NULL, NULL, &tdata);
359 } else {
360 status = pjsip_inv_initial_answer(inv, rdata, code,
361 NULL, NULL, &tdata);
362 }
363
364 if (status != PJ_SUCCESS) {
365 if (*has_initial) {
366 status = pjsip_inv_answer(inv, PJSIP_SC_NOT_ACCEPTABLE,
367 NULL, NULL, &tdata);
368 } else {
369 status = pjsip_inv_initial_answer(inv, rdata,
370 PJSIP_SC_NOT_ACCEPTABLE,
371 NULL, NULL, &tdata);
372 }
373
374 if (status == PJ_SUCCESS) {
375 *has_initial = PJ_TRUE;
376 pjsip_inv_send_msg(inv, tdata);
377 } else {
378 pjsip_inv_terminate(inv, 500, PJ_FALSE);
379 return -1;
380 }
381 } else {
382 *has_initial = PJ_TRUE;
383
384 status = pjsip_inv_send_msg(inv, tdata);
385 if (status != PJ_SUCCESS) {
386 pjsip_tx_data_dec_ref(tdata);
387 return status;
388 }
389 }
390
391 return status;
392}
393
394static void answer_timer_cb(pj_timer_heap_t *h, pj_timer_entry *entry)
395{
396 struct call *call = entry->user_data;
397 pj_bool_t has_initial = PJ_TRUE;
398
Benny Prijonoc1e263f2006-08-09 13:53:59 +0000399 PJ_UNUSED_ARG(h);
400
Benny Prijonof521eb02006-08-06 23:07:25 +0000401 entry->id = 0;
402 send_response(call->inv, NULL, 200, &has_initial);
403}
404
Benny Prijono514ca6b2006-07-03 01:30:01 +0000405static pj_bool_t mod_call_on_rx_request(pjsip_rx_data *rdata)
406{
407 const pj_str_t call_user = { "2", 1 };
408 pjsip_uri *uri;
409 pjsip_sip_uri *sip_uri;
410 struct call *call;
411 pjsip_dialog *dlg;
412 pjmedia_sdp_session *sdp;
413 pjsip_tx_data *tdata;
Benny Prijonof521eb02006-08-06 23:07:25 +0000414 pj_bool_t has_initial = PJ_FALSE;
Benny Prijono514ca6b2006-07-03 01:30:01 +0000415 pj_status_t status;
416
417 uri = pjsip_uri_get_uri(rdata->msg_info.msg->line.req.uri);
418
419 /* Only want to receive SIP scheme */
420 if (!PJSIP_URI_SCHEME_IS_SIP(uri))
421 return PJ_FALSE;
422
423 sip_uri = (pjsip_sip_uri*) uri;
424
Benny Prijonodcc0cbf2006-07-16 10:40:37 +0000425 /* Only want to handle INVITE requests. */
Benny Prijono514ca6b2006-07-03 01:30:01 +0000426 if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD) {
427 return PJ_FALSE;
428 }
429
430
Benny Prijonodcc0cbf2006-07-16 10:40:37 +0000431 /* Check for matching user part. Incoming requests will be handled
432 * call-statefully if:
433 * - user part is "2", or
434 * - user part is not "0" nor "1" and method is INVITE.
435 */
436 if (pj_strcmp(&sip_uri->user, &call_user) == 0 ||
437 sip_uri->user.slen != 1 ||
438 (*sip_uri->user.ptr != '0' && *sip_uri->user.ptr != '1'))
439 {
440 /* Match */
441
442 } else {
443 return PJ_FALSE;
444 }
445
446
Benny Prijono514ca6b2006-07-03 01:30:01 +0000447 /* Verify that we can handle the request. */
448 if (app.real_sdp) {
449 unsigned options = 0;
450 status = pjsip_inv_verify_request(rdata, &options, NULL, NULL,
451 app.sip_endpt, &tdata);
452 if (status != PJ_SUCCESS) {
453
454 /*
455 * No we can't handle the incoming INVITE request.
456 */
457
458 if (tdata) {
459 pjsip_response_addr res_addr;
460
461 pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
462 pjsip_endpt_send_response(app.sip_endpt, &res_addr, tdata,
463 NULL, NULL);
464
465 } else {
466
467 /* Respond with 500 (Internal Server Error) */
468 pjsip_endpt_respond_stateless(app.sip_endpt, rdata, 500, NULL,
469 NULL, NULL);
470 }
471
472 return PJ_TRUE;
473 }
474 }
475
476 /* Create UAS dialog */
477 status = pjsip_dlg_create_uas( pjsip_ua_instance(), rdata,
478 &app.local_contact, &dlg);
479 if (status != PJ_SUCCESS) {
480 const pj_str_t reason = pj_str("Unable to create dialog");
481 pjsip_endpt_respond_stateless( app.sip_endpt, rdata,
482 500, &reason,
483 NULL, NULL);
484 return PJ_TRUE;
485 }
486
487 /* Alloc call structure. */
488 call = pj_pool_zalloc(dlg->pool, sizeof(struct call));
489
490 /* Create SDP from PJMEDIA */
491 if (app.real_sdp) {
492 status = pjmedia_endpt_create_sdp(app.med_endpt, rdata->tp_info.pool,
493 app.skinfo_cnt, app.skinfo,
494 &sdp);
495 } else {
496 sdp = app.dummy_sdp;
497 }
498
499 /* Create UAS invite session */
500 status = pjsip_inv_create_uas( dlg, rdata, sdp, 0, &call->inv);
501 if (status != PJ_SUCCESS) {
502 pjsip_dlg_create_response(dlg, rdata, 500, NULL, &tdata);
503 pjsip_dlg_send_response(dlg, pjsip_rdata_get_tsx(rdata), tdata);
504 return PJ_TRUE;
505 }
506
Benny Prijonof521eb02006-08-06 23:07:25 +0000507 /* Send 100/Trying if needed */
508 if (app.server.send_trying) {
509 status = send_response(call->inv, rdata, 100, &has_initial);
510 if (status != PJ_SUCCESS)
511 return PJ_TRUE;
Benny Prijono514ca6b2006-07-03 01:30:01 +0000512 }
513
Benny Prijonof521eb02006-08-06 23:07:25 +0000514 /* Send 180/Ringing if needed */
515 if (app.server.send_ringing) {
516 status = send_response(call->inv, rdata, 180, &has_initial);
517 if (status != PJ_SUCCESS)
518 return PJ_TRUE;
519 }
Benny Prijono514ca6b2006-07-03 01:30:01 +0000520
Benny Prijonof521eb02006-08-06 23:07:25 +0000521 /* Simulate call processing delay */
522 if (app.server.delay) {
523 pj_time_val delay;
Benny Prijono514ca6b2006-07-03 01:30:01 +0000524
Benny Prijonof521eb02006-08-06 23:07:25 +0000525 call->ans_timer.id = 1;
526 call->ans_timer.user_data = call;
527 call->ans_timer.cb = &answer_timer_cb;
528
529 delay.sec = 0;
530 delay.msec = app.server.delay;
531 pj_time_val_normalize(&delay);
532
533 pjsip_endpt_schedule_timer(app.sip_endpt, &call->ans_timer, &delay);
534
535 } else {
536 /* Send the 200 response immediately . */
Benny Prijonoc1e263f2006-08-09 13:53:59 +0000537 status = send_response(call->inv, rdata, 200, &has_initial);
Benny Prijonof521eb02006-08-06 23:07:25 +0000538 PJ_ASSERT_ON_FAIL(status == PJ_SUCCESS, return PJ_TRUE);
539 }
Benny Prijono514ca6b2006-07-03 01:30:01 +0000540
541 /* Done */
542 app.server.cur_state.call_cnt++;
543
544 return PJ_TRUE;
545}
546
547
548
Benny Prijonodcc0cbf2006-07-16 10:40:37 +0000549/**************************************************************************
550 * Default handler when incoming request is not handled by any other
551 * modules.
552 */
553static pj_bool_t mod_responder_on_rx_request(pjsip_rx_data *rdata);
554
555/* Module to handle incoming requests statelessly.
556 */
557static pjsip_module mod_responder =
558{
559 NULL, NULL, /* prev, next. */
560 { "mod-responder", 13 }, /* Name. */
561 -1, /* Id */
562 PJSIP_MOD_PRIORITY_APPLICATION+1, /* Priority */
563 NULL, /* load() */
564 NULL, /* start() */
565 NULL, /* stop() */
566 NULL, /* unload() */
567 &mod_responder_on_rx_request, /* on_rx_request() */
568 NULL, /* on_rx_response() */
569 NULL, /* on_tx_request. */
570 NULL, /* on_tx_response() */
571 NULL, /* on_tsx_state() */
572};
573
574
575static pj_bool_t mod_responder_on_rx_request(pjsip_rx_data *rdata)
576{
577 const pj_str_t reason = pj_str("Not expecting request at this URI");
578
579 /*
Benny Prijono7db431e2006-07-23 14:38:49 +0000580 * Respond any requests (except ACK!) with 500.
Benny Prijonodcc0cbf2006-07-16 10:40:37 +0000581 */
Benny Prijono7db431e2006-07-23 14:38:49 +0000582 if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) {
583 pjsip_endpt_respond_stateless(app.sip_endpt, rdata, 500, &reason,
584 NULL, NULL);
585 }
586
Benny Prijonodcc0cbf2006-07-16 10:40:37 +0000587 return PJ_TRUE;
588}
589
590
Benny Prijono514ca6b2006-07-03 01:30:01 +0000591
592/*****************************************************************************
593 * Below is a simple module to log all incoming and outgoing SIP messages
594 */
595
596
597/* Notification on incoming messages */
598static pj_bool_t logger_on_rx_msg(pjsip_rx_data *rdata)
599{
Benny Prijonoc3573762006-07-10 21:39:24 +0000600 PJ_LOG(3,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
Benny Prijono514ca6b2006-07-03 01:30:01 +0000601 "%.*s\n"
602 "--end msg--",
603 rdata->msg_info.len,
604 pjsip_rx_data_get_info(rdata),
Benny Prijonoc3573762006-07-10 21:39:24 +0000605 rdata->tp_info.transport->type_name,
Benny Prijono514ca6b2006-07-03 01:30:01 +0000606 rdata->pkt_info.src_name,
607 rdata->pkt_info.src_port,
608 (int)rdata->msg_info.len,
609 rdata->msg_info.msg_buf));
610
611 /* Always return false, otherwise messages will not get processed! */
612 return PJ_FALSE;
613}
614
615/* Notification on outgoing messages */
616static pj_status_t logger_on_tx_msg(pjsip_tx_data *tdata)
617{
618
619 /* Important note:
620 * tp_info field is only valid after outgoing messages has passed
621 * transport layer. So don't try to access tp_info when the module
622 * has lower priority than transport layer.
623 */
624
Benny Prijonoc3573762006-07-10 21:39:24 +0000625 PJ_LOG(3,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n"
Benny Prijono514ca6b2006-07-03 01:30:01 +0000626 "%.*s\n"
627 "--end msg--",
628 (tdata->buf.cur - tdata->buf.start),
629 pjsip_tx_data_get_info(tdata),
Benny Prijonoc3573762006-07-10 21:39:24 +0000630 tdata->tp_info.transport->type_name,
Benny Prijono514ca6b2006-07-03 01:30:01 +0000631 tdata->tp_info.dst_name,
632 tdata->tp_info.dst_port,
633 (int)(tdata->buf.cur - tdata->buf.start),
634 tdata->buf.start));
635
636 /* Always return success, otherwise message will not get sent! */
637 return PJ_SUCCESS;
638}
639
640/* The module instance. */
641static pjsip_module msg_logger =
642{
643 NULL, NULL, /* prev, next. */
644 { "mod-siprtp-log", 14 }, /* Name. */
645 -1, /* Id */
646 PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
647 NULL, /* load() */
648 NULL, /* start() */
649 NULL, /* stop() */
650 NULL, /* unload() */
651 &logger_on_rx_msg, /* on_rx_request() */
652 &logger_on_rx_msg, /* on_rx_response() */
653 &logger_on_tx_msg, /* on_tx_request. */
654 &logger_on_tx_msg, /* on_tx_response() */
655 NULL, /* on_tsx_state() */
656
657};
658
659
660
661/**************************************************************************
662 * Test Client.
663 */
664
665static pj_bool_t mod_test_on_rx_response(pjsip_rx_data *rdata);
666
667static void call_on_media_update( pjsip_inv_session *inv,
668 pj_status_t status);
669static void call_on_state_changed( pjsip_inv_session *inv,
670 pjsip_event *e);
671static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e);
672
673
674/* Module to handle incoming requests callly.
675 */
676static pjsip_module mod_test =
677{
678 NULL, NULL, /* prev, next. */
679 { "mod-test", 8 }, /* Name. */
680 -1, /* Id */
681 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
682 NULL, /* load() */
683 NULL, /* start() */
684 NULL, /* stop() */
685 NULL, /* unload() */
686 NULL, /* on_rx_request() */
687 &mod_test_on_rx_response, /* on_rx_response() */
688 NULL, /* on_tx_request. */
689 NULL, /* on_tx_response() */
690 NULL, /* on_tsx_state() */
691};
692
693
694static void report_completion(int status_code)
695{
696 app.client.job_finished++;
Benny Prijono49f682a2006-07-11 12:25:45 +0000697 if (status_code >= 200 && status_code < 800)
698 app.client.response_codes[status_code]++;
Benny Prijono514ca6b2006-07-03 01:30:01 +0000699 app.client.total_responses++;
700 pj_gettimeofday(&app.client.last_completion);
701}
702
703
704/* Handler when response is received. */
705static pj_bool_t mod_test_on_rx_response(pjsip_rx_data *rdata)
706{
707 if (pjsip_rdata_get_tsx(rdata) == NULL) {
708 report_completion(rdata->msg_info.msg->line.status.code);
709 }
710
711 return PJ_TRUE;
712}
713
714
715/*
716 * Create app
717 */
718static pj_status_t create_app(void)
719{
720 pj_status_t status;
721
722 status = pj_init();
723 if (status != PJ_SUCCESS) {
724 app_perror(THIS_FILE, "Error initializing pjlib", status);
725 return status;
726 }
727
728 /* init PJLIB-UTIL: */
729 status = pjlib_util_init();
730 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
731
732 /* Must create a pool factory before we can allocate any memory. */
Benny Prijono1479b652006-07-03 14:18:17 +0000733 pj_caching_pool_init(&app.cp, &pj_pool_factory_default_policy,
734 CACHING_POOL_SIZE);
Benny Prijono514ca6b2006-07-03 01:30:01 +0000735
736 /* Create application pool for misc. */
737 app.pool = pj_pool_create(&app.cp.factory, "app", 1000, 1000, NULL);
738
739 /* Create the endpoint: */
740 status = pjsip_endpt_create(&app.cp.factory, pj_gethostname()->ptr,
741 &app.sip_endpt);
742 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
743
744
745 return status;
746}
747
748
749/*
750 * Init SIP stack
751 */
752static pj_status_t init_sip()
753{
754 pj_status_t status;
755
Benny Prijonoc03a3c52006-07-03 02:31:10 +0000756 /* Add UDP/TCP transport. */
Benny Prijono514ca6b2006-07-03 01:30:01 +0000757 {
758 pj_sockaddr_in addr;
759 pjsip_host_port addrname;
Benny Prijonoc03a3c52006-07-03 02:31:10 +0000760 const char *transport_type;
Benny Prijono514ca6b2006-07-03 01:30:01 +0000761
Benny Prijonoac623b32006-07-03 15:19:31 +0000762 pj_bzero(&addr, sizeof(addr));
Benny Prijono514ca6b2006-07-03 01:30:01 +0000763 addr.sin_family = PJ_AF_INET;
764 addr.sin_addr.s_addr = 0;
765 addr.sin_port = pj_htons((pj_uint16_t)app.local_port);
766
767 if (app.local_addr.slen) {
768 addrname.host = app.local_addr;
769 addrname.port = 5060;
Benny Prijonoc03a3c52006-07-03 02:31:10 +0000770 }
Benny Prijono514ca6b2006-07-03 01:30:01 +0000771 if (app.local_port != 0)
772 addrname.port = app.local_port;
773
Benny Prijonoc03a3c52006-07-03 02:31:10 +0000774 if (app.use_tcp) {
775 pj_sockaddr_in local_addr;
776 pjsip_tpfactory *tpfactory;
777
778 transport_type = "tcp";
Benny Prijono1479b652006-07-03 14:18:17 +0000779 pj_sockaddr_in_init(&local_addr, 0, (pj_uint16_t)app.local_port);
Benny Prijonoc03a3c52006-07-03 02:31:10 +0000780 status = pjsip_tcp_transport_start(app.sip_endpt, &local_addr,
781 app.thread_count, &tpfactory);
782 if (status == PJ_SUCCESS) {
783 app.local_addr = tpfactory->addr_name.host;
784 app.local_port = tpfactory->addr_name.port;
785 }
786 } else {
787 pjsip_transport *tp;
788
789 transport_type = "udp";
790 status = pjsip_udp_transport_start(app.sip_endpt, &addr,
791 (app.local_addr.slen ? &addrname:NULL),
792 app.thread_count, &tp);
793 if (status == PJ_SUCCESS) {
794 app.local_addr = tp->local_name.host;
795 app.local_port = tp->local_name.port;
796 }
797
798 }
Benny Prijono514ca6b2006-07-03 01:30:01 +0000799 if (status != PJ_SUCCESS) {
Benny Prijonoc03a3c52006-07-03 02:31:10 +0000800 app_perror(THIS_FILE, "Unable to start transport", status);
Benny Prijono514ca6b2006-07-03 01:30:01 +0000801 return status;
802 }
803
Benny Prijono514ca6b2006-07-03 01:30:01 +0000804 app.local_uri.ptr = pj_pool_alloc(app.pool, 128);
805 app.local_uri.slen = pj_ansi_sprintf(app.local_uri.ptr,
Benny Prijonoc03a3c52006-07-03 02:31:10 +0000806 "<sip:pjsip-perf@%.*s:%d;transport=%s>",
807 (int)app.local_addr.slen,
808 app.local_addr.ptr,
809 app.local_port,
810 transport_type);
Benny Prijono514ca6b2006-07-03 01:30:01 +0000811
812 app.local_contact = app.local_uri;
813 }
814
815 /*
816 * Init transaction layer.
817 * This will create/initialize transaction hash tables etc.
818 */
819 status = pjsip_tsx_layer_init_module(app.sip_endpt);
820 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
821
822 /* Initialize UA layer. */
823 status = pjsip_ua_init_module( app.sip_endpt, NULL );
824 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
825
826 /* Init invite session module. */
827 {
828 pjsip_inv_callback inv_cb;
829
830 /* Init the callback for INVITE session: */
Benny Prijonoac623b32006-07-03 15:19:31 +0000831 pj_bzero(&inv_cb, sizeof(inv_cb));
Benny Prijono514ca6b2006-07-03 01:30:01 +0000832 inv_cb.on_state_changed = &call_on_state_changed;
833 inv_cb.on_new_session = &call_on_forked;
834 inv_cb.on_media_update = &call_on_media_update;
835
836 /* Initialize invite session module: */
837 status = pjsip_inv_usage_init(app.sip_endpt, &inv_cb);
838 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
839 }
840
841 /* Register our module to receive incoming requests. */
842 status = pjsip_endpt_register_module( app.sip_endpt, &mod_test);
843 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
844
845
846 /* Register stateless server module */
847 status = pjsip_endpt_register_module( app.sip_endpt, &mod_stateless_server);
848 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
849
Benny Prijonodcc0cbf2006-07-16 10:40:37 +0000850 /* Register default responder module */
851 status = pjsip_endpt_register_module( app.sip_endpt, &mod_responder);
852 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono514ca6b2006-07-03 01:30:01 +0000853
854 /* Register stateless server module */
855 status = pjsip_endpt_register_module( app.sip_endpt, &mod_stateful_server);
856 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
857
858
859 /* Register call server module */
860 status = pjsip_endpt_register_module( app.sip_endpt, &mod_call_server);
861 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
862
863
864 /* Done */
865 return PJ_SUCCESS;
866}
867
868
869/*
870 * Destroy SIP
871 */
872static void destroy_app()
873{
874 unsigned i;
875
876 app.thread_quit = 1;
877 for (i=0; i<app.thread_count; ++i) {
878 if (app.thread[i]) {
879 pj_thread_join(app.thread[i]);
880 pj_thread_destroy(app.thread[i]);
881 app.thread[i] = NULL;
882 }
883 }
884
885 if (app.sip_endpt) {
886 pjsip_endpt_destroy(app.sip_endpt);
887 app.sip_endpt = NULL;
888 }
889
890 if (app.pool) {
891 pj_pool_release(app.pool);
892 app.pool = NULL;
Benny Prijono1ef06df2006-07-09 10:06:44 +0000893 PJ_LOG(3,(THIS_FILE, "Peak memory size: %uMB",
894 app.cp.peak_used_size / 1000000));
Benny Prijono514ca6b2006-07-03 01:30:01 +0000895 pj_caching_pool_destroy(&app.cp);
896 }
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000897
898 /* Shutdown PJLIB */
899 pj_shutdown();
Benny Prijono514ca6b2006-07-03 01:30:01 +0000900}
901
902
903/*
904 * Init media stack.
905 */
906static pj_status_t init_media()
907{
908 unsigned i;
909 pj_uint16_t rtp_port;
910 pj_status_t status;
911
912
913 /* Initialize media endpoint so that at least error subsystem is properly
914 * initialized.
915 */
916 status = pjmedia_endpt_create(&app.cp.factory,
917 pjsip_endpt_get_ioqueue(app.sip_endpt), 0,
918 &app.med_endpt);
919 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
920
921
922 /* Must register all codecs to be supported */
Benny Prijono06d1d0e2007-01-27 18:09:28 +0000923#if defined(PJMEDIA_HAS_G711_CODEC) && PJMEDIA_HAS_G711_CODEC!=0
Benny Prijono514ca6b2006-07-03 01:30:01 +0000924 pjmedia_codec_g711_init(app.med_endpt);
Benny Prijono06d1d0e2007-01-27 18:09:28 +0000925#endif
926#if defined(PJMEDIA_HAS_GSM_CODEC) && PJMEDIA_HAS_GSM_CODEC!=0
Benny Prijono514ca6b2006-07-03 01:30:01 +0000927 pjmedia_codec_gsm_init(app.med_endpt);
Benny Prijono06d1d0e2007-01-27 18:09:28 +0000928#endif
929#if defined(PJMEDIA_HAS_SPEEX_CODEC) && PJMEDIA_HAS_SPEEX_CODEC!=0
Benny Prijono514ca6b2006-07-03 01:30:01 +0000930 pjmedia_codec_speex_init(app.med_endpt, PJMEDIA_SPEEX_NO_UWB, 3, 3);
Benny Prijono06d1d0e2007-01-27 18:09:28 +0000931#endif
Benny Prijono514ca6b2006-07-03 01:30:01 +0000932
933 /* Init dummy socket addresses */
934 app.skinfo_cnt = 0;
935 for (i=0, rtp_port=4000; i<PJ_ARRAY_SIZE(app.skinfo); ++i, rtp_port+=2) {
936 pjmedia_sock_info *skinfo;
937
938 skinfo = &app.skinfo[i];
939
940 pj_sockaddr_in_init(&skinfo->rtp_addr_name, &app.local_addr,
941 (pj_uint16_t)rtp_port);
942 pj_sockaddr_in_init(&skinfo->rtp_addr_name, &app.local_addr,
943 (pj_uint16_t)(rtp_port+1));
944 app.skinfo_cnt++;
945 }
946
947 /* Generate dummy SDP */
948 dummy_sdp_str.slen = pj_ansi_strlen(dummy_sdp_str.ptr);
949 status = pjmedia_sdp_parse(app.pool, dummy_sdp_str.ptr, dummy_sdp_str.slen,
950 &app.dummy_sdp);
951 if (status != PJ_SUCCESS) {
952 app_perror(THIS_FILE, "Error parsing dummy SDP", status);
953 return status;
954 }
955
956
957 /* Done */
958 return PJ_SUCCESS;
959}
960
961
962/* This is notification from the call about media negotiation
963 * status. This is called for client calls only.
964 */
965static void call_on_media_update( pjsip_inv_session *inv,
966 pj_status_t status)
967{
968 if (status != PJ_SUCCESS) {
969 pjsip_tx_data *tdata;
970 pj_status_t status;
971
972 status = pjsip_inv_end_session(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE,
973 NULL, &tdata);
974 if (status == PJ_SUCCESS && tdata)
975 status = pjsip_inv_send_msg(inv, tdata);
976 }
977}
978
979
980/* This is notification from the call when the call state has changed.
981 * This is called for client calls only.
982 */
983static void call_on_state_changed( pjsip_inv_session *inv,
984 pjsip_event *e)
985{
986 PJ_UNUSED_ARG(e);
987
988 /* Bail out if the session has been counted before */
989 if (inv->mod_data[mod_test.id] != NULL)
990 return;
991
992 /* Bail out if this is not an outgoing call */
993 if (inv->role != PJSIP_UAC_ROLE)
994 return;
995
996 if (inv->state == PJSIP_INV_STATE_CONFIRMED) {
997 pjsip_tx_data *tdata;
998 pj_status_t status;
999
1000 //report_completion(200);
1001 //inv->mod_data[mod_test.id] = (void*)1;
1002
1003 status = pjsip_inv_end_session(inv, PJSIP_SC_OK, NULL, &tdata);
1004 if (status == PJ_SUCCESS && tdata)
1005 status = pjsip_inv_send_msg(inv, tdata);
1006
1007 } else if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
1008 report_completion(inv->cause);
1009 inv->mod_data[mod_test.id] = (void*)1;
1010 }
1011}
1012
1013
1014/* Not implemented for now */
1015static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e)
1016{
1017 /* Do nothing */
1018 PJ_UNUSED_ARG(inv);
1019 PJ_UNUSED_ARG(e);
1020}
1021
1022
1023/*
1024 * Make outgoing call.
1025 */
1026static pj_status_t make_call(const pj_str_t *dst_uri)
1027{
1028 struct call *call;
1029 pjsip_dialog *dlg;
1030 pjmedia_sdp_session *sdp;
1031 pjsip_tx_data *tdata;
1032 pj_status_t status;
1033
1034
1035 /* Create UAC dialog */
1036 status = pjsip_dlg_create_uac( pjsip_ua_instance(),
1037 &app.local_uri, /* local URI */
1038 &app.local_contact, /* local Contact */
1039 dst_uri, /* remote URI */
1040 dst_uri, /* remote target */
1041 &dlg); /* dialog */
1042 if (status != PJ_SUCCESS) {
1043 return status;
1044 }
1045
1046 /* Create call */
1047 call = pj_pool_zalloc(dlg->pool, sizeof(struct call));
1048
1049 /* Create SDP */
1050 if (app.real_sdp) {
1051 status = pjmedia_endpt_create_sdp(app.med_endpt, dlg->pool, 1,
1052 app.skinfo, &sdp);
1053 if (status != PJ_SUCCESS) {
1054 pjsip_dlg_terminate(dlg);
1055 return status;
1056 }
1057 } else
1058 sdp = app.dummy_sdp;
1059
1060 /* Create the INVITE session. */
1061 status = pjsip_inv_create_uac( dlg, sdp, 0, &call->inv);
1062 if (status != PJ_SUCCESS) {
1063 pjsip_dlg_terminate(dlg);
1064 return status;
1065 }
1066
1067
1068 /* Create initial INVITE request.
1069 * This INVITE request will contain a perfectly good request and
1070 * an SDP body as well.
1071 */
1072 status = pjsip_inv_invite(call->inv, &tdata);
1073 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
1074
1075
1076 /* Send initial INVITE request.
1077 * From now on, the invite session's state will be reported to us
1078 * via the invite session callbacks.
1079 */
1080 status = pjsip_inv_send_msg(call->inv, tdata);
1081 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
1082
1083
1084 return PJ_SUCCESS;
1085}
1086
1087
1088/*
1089 * Verify that valid SIP url is given.
1090 */
1091static pj_status_t verify_sip_url(const char *c_url)
1092{
1093 pjsip_uri *p;
1094 pj_pool_t *pool;
1095 char *url;
1096 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
1097
1098 if (!len) return -1;
1099
1100 pool = pj_pool_create(&app.cp.factory, "check%p", 1024, 0, NULL);
1101 if (!pool) return PJ_ENOMEM;
1102
1103 url = pj_pool_alloc(pool, len+1);
1104 pj_ansi_strcpy(url, c_url);
1105 url[len] = '\0';
1106
1107 p = pjsip_parse_uri(pool, url, len, 0);
1108 if (!p || pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0)
1109 p = NULL;
1110
1111 pj_pool_release(pool);
1112 return p ? 0 : -1;
1113}
1114
1115
1116static void usage(void)
1117{
1118 printf(
1119 "Usage:\n"
1120 " pjsip-perf [OPTIONS] -- to start as server\n"
1121 " pjsip-perf [OPTIONS] URL -- to call server (possibly itself)\n"
1122 "\n"
1123 "where:\n"
Benny Prijonoc3573762006-07-10 21:39:24 +00001124 " URL The SIP URL to be contacted.\n"
Benny Prijono514ca6b2006-07-03 01:30:01 +00001125 "\n"
Benny Prijonoc3573762006-07-10 21:39:24 +00001126 "Client options:\n"
Benny Prijono775a1b22006-07-11 09:53:27 +00001127 " --method=METHOD, -m Set test method (set to INVITE for call benchmark)\n"
1128 " [default: OPTIONS]\n"
Benny Prijonoc3573762006-07-10 21:39:24 +00001129 " --count=N, -n Set total number of requests to initiate\n"
1130 " [default=%d]\n"
1131 " --stateless, -s Set to operate in stateless mode\n"
1132 " [default: stateful]\n"
1133 " --timeout=SEC, -t Set client timeout [default=60 sec]\n"
1134 " --window=COUNT, -w Set maximum outstanding job [default: %d]\n"
1135 "\n"
1136 "SDP options (client and server):\n"
Benny Prijono514ca6b2006-07-03 01:30:01 +00001137 " --real-sdp Generate real SDP from pjmedia, and also perform\n"
Benny Prijonoc3573762006-07-10 21:39:24 +00001138 " proper SDP negotiation [default: dummy]\n"
1139 "\n"
1140 "Client and Server options:\n"
1141 " --local-port=PORT, -p Set local port [default: 5060]\n"
Benny Prijono775a1b22006-07-11 09:53:27 +00001142 " --use-tcp, -T Use TCP instead of UDP. Note that when started as\n"
1143 " client, you must add ;transport=tcp parameter to URL\n"
1144 " [default: no]\n"
Benny Prijonoc3573762006-07-10 21:39:24 +00001145 " --thread-count=N Set number of worker threads [default=1]\n"
Benny Prijonof521eb02006-08-06 23:07:25 +00001146 " --trying Send 100/Trying response (server, default no)\n"
1147 " --ringing Send 180/Ringing response (server, default no)\n"
1148 " --delay=MS, -d Delay answering call by MS (server, default no)\n"
Benny Prijonoc3573762006-07-10 21:39:24 +00001149 "\n"
1150 "Misc options:\n"
Benny Prijono514ca6b2006-07-03 01:30:01 +00001151 " --help, -h Display this screen\n"
Benny Prijono775a1b22006-07-11 09:53:27 +00001152 " --verbose, -v Verbose logging (put more than once for even more)\n"
Benny Prijono514ca6b2006-07-03 01:30:01 +00001153 "\n"
1154 "When started as server, pjsip-perf can be contacted on the following URIs:\n"
Benny Prijonoc3573762006-07-10 21:39:24 +00001155 " - sip:0@server-addr To handle requests statelessly.\n"
1156 " - sip:1@server-addr To handle requests statefully.\n"
1157 " - sip:2@server-addr To handle INVITE call.\n",
Benny Prijono1479b652006-07-03 14:18:17 +00001158 DEFAULT_COUNT, JOB_WINDOW);
Benny Prijono514ca6b2006-07-03 01:30:01 +00001159}
1160
1161
1162static int my_atoi(const char *s)
1163{
1164 pj_str_t ss = pj_str((char*)s);
1165 return pj_strtoul(&ss);
1166}
1167
1168
1169static pj_status_t init_options(int argc, char *argv[])
1170{
Benny Prijonof521eb02006-08-06 23:07:25 +00001171 enum { OPT_THREAD_COUNT = 1, OPT_REAL_SDP, OPT_TRYING, OPT_RINGING };
Benny Prijono514ca6b2006-07-03 01:30:01 +00001172 struct pj_getopt_option long_options[] = {
1173 { "local-port", 1, 0, 'p' },
1174 { "count", 1, 0, 'c' },
1175 { "thread-count", 1, 0, OPT_THREAD_COUNT },
1176 { "method", 1, 0, 'm' },
1177 { "help", 0, 0, 'h' },
1178 { "stateless", 0, 0, 's' },
1179 { "timeout", 1, 0, 't' },
1180 { "real-sdp", 0, 0, OPT_REAL_SDP },
1181 { "verbose", 0, 0, 'v' },
Benny Prijonoc03a3c52006-07-03 02:31:10 +00001182 { "use-tcp", 0, 0, 'T' },
Benny Prijono1479b652006-07-03 14:18:17 +00001183 { "window", 1, 0, 'w' },
Benny Prijonof521eb02006-08-06 23:07:25 +00001184 { "delay", 1, 0, 'd' },
1185 { "trying", 0, 0, OPT_TRYING},
1186 { "ringing", 0, 0, OPT_RINGING},
Benny Prijono514ca6b2006-07-03 01:30:01 +00001187 { NULL, 0, 0, 0 },
1188 };
1189 int c;
1190 int option_index;
1191
1192 /* Init default application configs */
1193 app.local_port = 5060;
1194 app.thread_count = 1;
1195 app.client.job_count = DEFAULT_COUNT;
1196 app.client.method = pjsip_options_method;
1197 app.client.job_window = c = JOB_WINDOW;
1198 app.client.timeout = 60;
Benny Prijonoc3573762006-07-10 21:39:24 +00001199 app.log_level = 3;
Benny Prijono514ca6b2006-07-03 01:30:01 +00001200
1201
1202 /* Parse options */
1203 pj_optind = 0;
Benny Prijonof521eb02006-08-06 23:07:25 +00001204 while((c=pj_getopt_long(argc,argv, "p:c:m:t:w:d:hsv",
Benny Prijono514ca6b2006-07-03 01:30:01 +00001205 long_options, &option_index))!=-1)
1206 {
1207 switch (c) {
1208 case 'p':
1209 app.local_port = my_atoi(pj_optarg);
1210 if (app.local_port < 0 || app.local_port > 65535) {
1211 PJ_LOG(3,(THIS_FILE, "Invalid --local-port %s", pj_optarg));
1212 return -1;
1213 }
1214 break;
1215
1216 case 'c':
1217 app.client.job_count = my_atoi(pj_optarg);
1218 if (app.client.job_count < 0) {
1219 PJ_LOG(3,(THIS_FILE, "Invalid --local-port %s", pj_optarg));
1220 return -1;
1221 }
1222 if (app.client.job_count > PJSIP_MAX_TSX_COUNT)
1223 PJ_LOG(3,(THIS_FILE,
1224 "Warning: --count value (%d) exceeds maximum "
1225 "transaction count (%d)", app.client.job_count,
1226 PJSIP_MAX_TSX_COUNT));
1227 break;
1228
1229 case OPT_THREAD_COUNT:
1230 app.thread_count = my_atoi(pj_optarg);
1231 if (app.thread_count < 1 || app.thread_count > 16) {
1232 PJ_LOG(3,(THIS_FILE, "Invalid --thread-count %s", pj_optarg));
1233 return -1;
1234 }
1235 break;
1236
1237 case 'm':
1238 {
1239 pj_str_t temp = pj_str((char*)pj_optarg);
1240 pjsip_method_init_np(&app.client.method, &temp);
1241 }
1242 break;
1243
1244 case 'h':
1245 usage();
1246 return -1;
1247
1248 case 's':
1249 app.client.stateless = PJ_TRUE;
1250 break;
1251
1252 case OPT_REAL_SDP:
1253 app.real_sdp = 1;
1254 break;
1255
1256 case 'v':
Benny Prijonoc3573762006-07-10 21:39:24 +00001257 app.log_level++;
Benny Prijono514ca6b2006-07-03 01:30:01 +00001258 break;
1259
1260 case 't':
1261 app.client.timeout = my_atoi(pj_optarg);
1262 if (app.client.timeout < 0 || app.client.timeout > 600) {
1263 PJ_LOG(3,(THIS_FILE, "Invalid --timeout %s", pj_optarg));
1264 return -1;
1265 }
1266 break;
1267
Benny Prijono1479b652006-07-03 14:18:17 +00001268 case 'w':
1269 app.client.job_window = my_atoi(pj_optarg);
1270 if (app.client.job_window <= 0) {
1271 PJ_LOG(3,(THIS_FILE, "Invalid --window %s", pj_optarg));
1272 return -1;
1273 }
1274 break;
1275
Benny Prijonoc03a3c52006-07-03 02:31:10 +00001276 case 'T':
1277 app.use_tcp = PJ_TRUE;
1278 break;
1279
Benny Prijonof521eb02006-08-06 23:07:25 +00001280 case 'd':
1281 app.server.delay = my_atoi(pj_optarg);
1282 if (app.server.delay > 3600) {
1283 PJ_LOG(3,(THIS_FILE, "I think --delay %s is too long",
1284 pj_optarg));
1285 return -1;
1286 }
1287 break;
1288
1289 case OPT_TRYING:
1290 app.server.send_trying = 1;
1291 break;
1292
1293 case OPT_RINGING:
1294 app.server.send_ringing = 1;
1295 break;
1296
Benny Prijono514ca6b2006-07-03 01:30:01 +00001297 default:
1298 PJ_LOG(1,(THIS_FILE,
1299 "Invalid argument. Use --help to see help"));
1300 return -1;
1301 }
1302 }
1303
1304 if (pj_optind != argc) {
1305
1306 if (verify_sip_url(argv[pj_optind]) != PJ_SUCCESS) {
1307 PJ_LOG(1,(THIS_FILE, "Invalid SIP URI %s", argv[pj_optind]));
1308 return -1;
1309 }
1310 app.client.dst_uri = pj_str(argv[pj_optind]);
1311
1312 pj_optind++;
1313
1314 }
1315
1316 if (pj_optind != argc) {
1317 PJ_LOG(1,(THIS_FILE, "Error: unknown options %s", argv[pj_optind]));
1318 return -1;
1319 }
1320
1321 return 0;
1322}
1323
1324
1325/* Send one stateless request */
1326static pj_status_t submit_stateless_job(void)
1327{
1328 pjsip_tx_data *tdata;
1329 pj_status_t status;
1330
1331 status = pjsip_endpt_create_request(app.sip_endpt, &app.client.method,
1332 &app.client.dst_uri, &app.local_uri,
1333 &app.client.dst_uri, &app.local_contact,
1334 NULL, -1, NULL, &tdata);
1335 if (status != PJ_SUCCESS) {
1336 app_perror(THIS_FILE, "Error creating request", status);
1337 report_completion(701);
1338 return status;
1339 }
1340
1341 status = pjsip_endpt_send_request_stateless(app.sip_endpt, tdata, NULL,
1342 NULL);
1343 if (status != PJ_SUCCESS) {
1344 pjsip_tx_data_dec_ref(tdata);
1345 app_perror(THIS_FILE, "Error sending stateless request", status);
1346 report_completion(701);
1347 return status;
1348 }
1349
1350 return PJ_SUCCESS;
1351}
1352
1353
1354/* This callback is called when client transaction state has changed */
1355static void tsx_completion_cb(void *token, pjsip_event *event)
1356{
1357 pjsip_transaction *tsx;
1358
1359 PJ_UNUSED_ARG(token);
1360
1361 if (event->type != PJSIP_EVENT_TSX_STATE)
1362 return;
1363
1364 tsx = event->body.tsx_state.tsx;
1365
1366 if (tsx->mod_data[mod_test.id] != NULL) {
1367 /* This transaction has been calculated before */
1368 return;
1369 }
1370
1371 if (tsx->state==PJSIP_TSX_STATE_TERMINATED) {
1372 report_completion(tsx->status_code);
1373 tsx->mod_data[mod_test.id] = (void*)1;
1374 }
1375 else if (tsx->method.id == PJSIP_INVITE_METHOD &&
1376 tsx->state == PJSIP_TSX_STATE_CONFIRMED) {
1377
1378 report_completion(tsx->status_code);
1379 tsx->mod_data[mod_test.id] = (void*)1;
1380
1381 } else if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
1382
1383 report_completion(tsx->status_code);
1384 tsx->mod_data[mod_test.id] = (void*)1;
1385
Benny Prijono1479b652006-07-03 14:18:17 +00001386 TERMINATE_TSX(tsx, tsx->status_code);
Benny Prijono514ca6b2006-07-03 01:30:01 +00001387 }
1388}
1389
1390
1391/* Send one stateful request */
1392static pj_status_t submit_job(void)
1393{
1394 pjsip_tx_data *tdata;
1395 pj_status_t status;
1396
1397 status = pjsip_endpt_create_request(app.sip_endpt, &app.client.method,
1398 &app.client.dst_uri, &app.local_uri,
1399 &app.client.dst_uri, &app.local_contact,
1400 NULL, -1, NULL, &tdata);
1401 if (status != PJ_SUCCESS) {
1402 app_perror(THIS_FILE, "Error creating request", status);
1403 report_completion(701);
1404 return status;
1405 }
1406
1407 status = pjsip_endpt_send_request(app.sip_endpt, tdata, -1, NULL,
1408 &tsx_completion_cb);
1409 if (status != PJ_SUCCESS) {
1410 app_perror(THIS_FILE, "Error sending stateful request", status);
1411 //should have been reported by tsx_completion_cb().
1412 //report_completion(701);
Benny Prijono27f01dd2006-10-16 21:07:19 +00001413 //No longer necessary (r777)
1414 //pjsip_tx_data_dec_ref(tdata);
Benny Prijono514ca6b2006-07-03 01:30:01 +00001415 }
1416 return status;
1417}
1418
1419
1420/* Client worker thread */
1421static int client_thread(void *arg)
1422{
Benny Prijono775a1b22006-07-11 09:53:27 +00001423 pj_time_val end_time, last_report, now;
Benny Prijono7db431e2006-07-23 14:38:49 +00001424 unsigned thread_index = (unsigned)(long)arg;
Benny Prijono49f682a2006-07-11 12:25:45 +00001425 unsigned cycle = 0, last_cycle = 0;
Benny Prijono514ca6b2006-07-03 01:30:01 +00001426
1427 pj_thread_sleep(100);
1428
1429 pj_gettimeofday(&end_time);
1430 end_time.sec += app.client.timeout;
1431
Benny Prijono775a1b22006-07-11 09:53:27 +00001432 pj_gettimeofday(&last_report);
1433
Benny Prijono514ca6b2006-07-03 01:30:01 +00001434 if (app.client.first_request.sec == 0) {
1435 pj_gettimeofday(&app.client.first_request);
1436 }
1437
1438 /* Submit all jobs */
Benny Prijono775a1b22006-07-11 09:53:27 +00001439 while (app.client.job_submitted < app.client.job_count && !app.thread_quit){
Benny Prijonoc3573762006-07-10 21:39:24 +00001440 pj_time_val timeout = { 0, 1 };
Benny Prijono514ca6b2006-07-03 01:30:01 +00001441 unsigned i;
1442 int outstanding;
1443 pj_status_t status;
1444
Benny Prijono1479b652006-07-03 14:18:17 +00001445 /* Calculate current outstanding job */
1446 outstanding = app.client.job_submitted - app.client.job_finished;
1447
1448 /* Update stats on max outstanding jobs */
1449 if (outstanding > (int)app.client.stat_max_window)
1450 app.client.stat_max_window = outstanding;
1451
Benny Prijono514ca6b2006-07-03 01:30:01 +00001452 /* Wait if there are more pending jobs than allowed in the
Benny Prijono49f682a2006-07-11 12:25:45 +00001453 * window. But spawn a new job anyway if no events are happening
1454 * after we wait for some time.
Benny Prijono514ca6b2006-07-03 01:30:01 +00001455 */
Benny Prijono49f682a2006-07-11 12:25:45 +00001456 for (i=0; outstanding > (int)app.client.job_window && i<1000; ++i) {
1457 pj_time_val wait = { 0, 500 };
1458 unsigned count = 0;
1459
1460 pjsip_endpt_handle_events2(app.sip_endpt, &wait, &count);
Benny Prijono514ca6b2006-07-03 01:30:01 +00001461 outstanding = app.client.job_submitted - app.client.job_finished;
Benny Prijono49f682a2006-07-11 12:25:45 +00001462
1463 if (count == 0)
1464 break;
1465
1466 ++cycle;
Benny Prijono514ca6b2006-07-03 01:30:01 +00001467 }
1468
Benny Prijono1479b652006-07-03 14:18:17 +00001469
1470 /* Submit one job */
Benny Prijono514ca6b2006-07-03 01:30:01 +00001471 if (app.client.method.id == PJSIP_INVITE_METHOD) {
1472 status = make_call(&app.client.dst_uri);
1473 } else if (app.client.stateless) {
1474 status = submit_stateless_job();
1475 } else {
1476 status = submit_job();
1477 }
1478
1479 ++app.client.job_submitted;
Benny Prijono49f682a2006-07-11 12:25:45 +00001480 ++cycle;
Benny Prijono514ca6b2006-07-03 01:30:01 +00001481
Benny Prijono1479b652006-07-03 14:18:17 +00001482 /* Handle event */
1483 pjsip_endpt_handle_events2(app.sip_endpt, &timeout, NULL);
1484
Benny Prijono775a1b22006-07-11 09:53:27 +00001485 /* Check for time out, also print report */
Benny Prijono49f682a2006-07-11 12:25:45 +00001486 if (cycle - last_cycle >= 500) {
Benny Prijono1479b652006-07-03 14:18:17 +00001487 pj_gettimeofday(&now);
Benny Prijono775a1b22006-07-11 09:53:27 +00001488 if (PJ_TIME_VAL_GTE(now, end_time)) {
Benny Prijono514ca6b2006-07-03 01:30:01 +00001489 break;
Benny Prijono775a1b22006-07-11 09:53:27 +00001490 }
Benny Prijono49f682a2006-07-11 12:25:45 +00001491 last_cycle = cycle;
Benny Prijono775a1b22006-07-11 09:53:27 +00001492
1493
1494 if (thread_index == 0 && now.sec-last_report.sec >= 2) {
1495 printf("\r%d jobs started, %d completed... ",
1496 app.client.job_submitted, app.client.job_finished);
1497 fflush(stdout);
1498 last_report = now;
1499 }
Benny Prijono514ca6b2006-07-03 01:30:01 +00001500 }
1501 }
1502
Benny Prijonoc3573762006-07-10 21:39:24 +00001503 if (app.client.requests_sent.sec == 0) {
1504 pj_gettimeofday(&app.client.requests_sent);
1505 }
1506
1507
Benny Prijono775a1b22006-07-11 09:53:27 +00001508 if (thread_index == 0) {
1509 printf("\r%d jobs started, %d completed%s\n",
1510 app.client.job_submitted, app.client.job_finished,
1511 (app.client.job_submitted!=app.client.job_finished ?
1512 ", waiting..." : ".") );
1513 fflush(stdout);
1514 }
1515
Benny Prijono514ca6b2006-07-03 01:30:01 +00001516 /* Wait until all jobs completes, or timed out */
Benny Prijonoc3573762006-07-10 21:39:24 +00001517 pj_gettimeofday(&now);
Benny Prijono775a1b22006-07-11 09:53:27 +00001518 while (PJ_TIME_VAL_LT(now, end_time) &&
Benny Prijonoc3573762006-07-10 21:39:24 +00001519 app.client.job_finished < app.client.job_count &&
1520 !app.thread_quit)
1521 {
1522 pj_time_val timeout = { 0, 1 };
Benny Prijono514ca6b2006-07-03 01:30:01 +00001523 unsigned i;
1524
Benny Prijonoc3573762006-07-10 21:39:24 +00001525 for (i=0; i<1000; ++i) {
Benny Prijono775a1b22006-07-11 09:53:27 +00001526 unsigned count;
1527 count = 0;
1528 pjsip_endpt_handle_events2(app.sip_endpt, &timeout, &count);
1529 if (count == 0)
1530 break;
1531 }
1532
1533 pj_gettimeofday(&now);
1534 }
1535
Benny Prijono49f682a2006-07-11 12:25:45 +00001536 /* Wait couple of seconds to let jobs completes (e.g. ACKs to be sent) */
Benny Prijono775a1b22006-07-11 09:53:27 +00001537 pj_gettimeofday(&now);
1538 end_time = now;
1539 end_time.sec += 2;
Benny Prijono49f682a2006-07-11 12:25:45 +00001540 while (PJ_TIME_VAL_LT(now, end_time))
Benny Prijono775a1b22006-07-11 09:53:27 +00001541 {
1542 pj_time_val timeout = { 0, 1 };
1543 unsigned i;
1544
1545 for (i=0; i<1000; ++i) {
1546 unsigned count;
1547 count = 0;
1548 pjsip_endpt_handle_events2(app.sip_endpt, &timeout, &count);
1549 if (count == 0)
1550 break;
Benny Prijono514ca6b2006-07-03 01:30:01 +00001551 }
1552
1553 pj_gettimeofday(&now);
Benny Prijonoc3573762006-07-10 21:39:24 +00001554 }
Benny Prijono514ca6b2006-07-03 01:30:01 +00001555
1556 return 0;
1557}
1558
1559
1560static const char *good_number(char *buf, pj_int32_t val)
1561{
1562 if (val < 1000) {
1563 pj_ansi_sprintf(buf, "%d", val);
1564 } else if (val < 1000000) {
1565 pj_ansi_sprintf(buf, "%d.%dK",
1566 val / 1000,
1567 (val % 1000) / 100);
1568 } else {
1569 pj_ansi_sprintf(buf, "%d.%02dM",
1570 val / 1000000,
1571 (val % 1000000) / 10000);
1572 }
1573
1574 return buf;
1575}
1576
1577
1578static int server_thread(void *arg)
1579{
Benny Prijonoc3573762006-07-10 21:39:24 +00001580 pj_time_val timeout = { 0, 1 };
Benny Prijono7db431e2006-07-23 14:38:49 +00001581 unsigned thread_index = (unsigned)(long)arg;
Benny Prijono514ca6b2006-07-03 01:30:01 +00001582 pj_time_val last_report, next_report;
1583
1584 pj_gettimeofday(&last_report);
1585 next_report = last_report;
1586 next_report.sec++;
1587
1588 while (!app.thread_quit) {
1589 pj_time_val now;
1590 unsigned i;
1591
1592 for (i=0; i<100; ++i) {
1593 unsigned count = 0;
1594 pjsip_endpt_handle_events2(app.sip_endpt, &timeout, &count);
1595 if (count == 0)
1596 break;
1597 }
1598
1599 if (thread_index == 0) {
1600 pj_gettimeofday(&now);
1601
1602 if (PJ_TIME_VAL_GTE(now, next_report)) {
1603 pj_time_val tmp;
1604 unsigned msec;
1605 unsigned stateless, stateful, call;
1606 char str_stateless[32], str_stateful[32], str_call[32];
1607
1608 tmp = now;
1609 PJ_TIME_VAL_SUB(tmp, last_report);
1610 msec = PJ_TIME_VAL_MSEC(tmp);
1611
1612 last_report = now;
1613 next_report = last_report;
1614 next_report.sec++;
1615
1616 stateless = app.server.cur_state.stateless_cnt - app.server.prev_state.stateless_cnt;
1617 stateful = app.server.cur_state.stateful_cnt - app.server.prev_state.stateful_cnt;
1618 call = app.server.cur_state.call_cnt - app.server.prev_state.call_cnt;
1619
1620 good_number(str_stateless, app.server.cur_state.stateless_cnt);
1621 good_number(str_stateful, app.server.cur_state.stateful_cnt);
1622 good_number(str_call, app.server.cur_state.call_cnt);
1623
Benny Prijono1479b652006-07-03 14:18:17 +00001624 printf("Total(rate): stateless:%s (%d/s), statefull:%s (%d/s), call:%s (%d/s) \r",
Benny Prijono514ca6b2006-07-03 01:30:01 +00001625 str_stateless, stateless*1000/msec,
1626 str_stateful, stateful*1000/msec,
1627 str_call, call*1000/msec);
1628 fflush(stdout);
1629
1630 app.server.prev_state = app.server.cur_state;
1631 }
1632 }
1633 }
1634
1635 return 0;
1636}
1637
Benny Prijono1479b652006-07-03 14:18:17 +00001638static void write_report(const char *msg)
1639{
1640 puts(msg);
1641
1642#if defined(PJ_WIN32) && PJ_WIN32!=0
1643 OutputDebugString(msg);
1644 OutputDebugString("\n");
1645#endif
1646}
1647
1648
Benny Prijono514ca6b2006-07-03 01:30:01 +00001649int main(int argc, char *argv[])
1650{
Benny Prijono1479b652006-07-03 14:18:17 +00001651 static char report[1024];
Benny Prijono514ca6b2006-07-03 01:30:01 +00001652
Benny Prijonodcc0cbf2006-07-16 10:40:37 +00001653 printf("PJSIP Performance Measurement Tool v%s\n"
1654 "(c)2006 pjsip.org\n\n",
1655 PJ_VERSION);
Benny Prijono775a1b22006-07-11 09:53:27 +00001656
Benny Prijono514ca6b2006-07-03 01:30:01 +00001657 if (create_app() != 0)
1658 return 1;
1659
1660 if (init_options(argc, argv) != 0)
1661 return 1;
1662
1663 if (init_sip() != 0)
1664 return 1;
1665
1666 if (init_media() != 0)
1667 return 1;
1668
Benny Prijonoc3573762006-07-10 21:39:24 +00001669 pj_log_set_level(app.log_level);
1670
Benny Prijonodcc0cbf2006-07-16 10:40:37 +00001671 if (app.log_level > 4) {
Benny Prijono514ca6b2006-07-03 01:30:01 +00001672 pjsip_endpt_register_module(app.sip_endpt, &msg_logger);
1673 }
1674
1675
1676 /* Misc infos */
1677 if (app.client.dst_uri.slen != 0) {
Benny Prijonoc3573762006-07-10 21:39:24 +00001678 if (app.client.method.id == PJSIP_INVITE_METHOD) {
1679 if (app.client.stateless) {
1680 PJ_LOG(3,(THIS_FILE,
1681 "Info: --stateless option makes no sense for INVITE,"
1682 " ignored."));
1683 }
Benny Prijono514ca6b2006-07-03 01:30:01 +00001684 }
Benny Prijonoc3573762006-07-10 21:39:24 +00001685
Benny Prijono514ca6b2006-07-03 01:30:01 +00001686 }
1687
Benny Prijono514ca6b2006-07-03 01:30:01 +00001688
1689
1690 if (app.client.dst_uri.slen) {
1691 /* Client mode */
1692 pj_status_t status;
1693 char test_type[64];
Benny Prijonoc3573762006-07-10 21:39:24 +00001694 unsigned msec_req, msec_res;
Benny Prijono514ca6b2006-07-03 01:30:01 +00001695 unsigned i;
1696
1697 /* Get the job name */
1698 if (app.client.method.id == PJSIP_INVITE_METHOD) {
1699 pj_ansi_strcpy(test_type, "INVITE calls");
1700 } else if (app.client.stateless) {
1701 pj_ansi_sprintf(test_type, "stateless %.*s requests",
1702 (int)app.client.method.name.slen,
1703 app.client.method.name.ptr);
1704 } else {
1705 pj_ansi_sprintf(test_type, "stateful %.*s requests",
1706 (int)app.client.method.name.slen,
1707 app.client.method.name.ptr);
1708 }
1709
1710
Benny Prijono775a1b22006-07-11 09:53:27 +00001711 printf("Sending %d %s to '%.*s' with %d maximum outstanding jobs, please wait..\n",
Benny Prijonoc03a3c52006-07-03 02:31:10 +00001712 app.client.job_count, test_type,
Benny Prijonoc3573762006-07-10 21:39:24 +00001713 (int)app.client.dst_uri.slen, app.client.dst_uri.ptr,
Benny Prijono775a1b22006-07-11 09:53:27 +00001714 app.client.job_window);
Benny Prijono514ca6b2006-07-03 01:30:01 +00001715
1716 for (i=0; i<app.thread_count; ++i) {
Benny Prijono7db431e2006-07-23 14:38:49 +00001717 status = pj_thread_create(app.pool, NULL, &client_thread,
1718 (void*)(long)i, 0, 0, &app.thread[i]);
Benny Prijono514ca6b2006-07-03 01:30:01 +00001719 if (status != PJ_SUCCESS) {
1720 app_perror(THIS_FILE, "Unable to create thread", status);
1721 return 1;
1722 }
1723 }
1724
1725 for (i=0; i<app.thread_count; ++i) {
1726 pj_thread_join(app.thread[i]);
1727 app.thread[i] = NULL;
1728 }
1729
1730 if (app.client.last_completion.sec) {
1731 pj_time_val duration;
1732 duration = app.client.last_completion;
1733 PJ_TIME_VAL_SUB(duration, app.client.first_request);
Benny Prijonoc3573762006-07-10 21:39:24 +00001734 msec_res = PJ_TIME_VAL_MSEC(duration);
Benny Prijono514ca6b2006-07-03 01:30:01 +00001735 } else {
Benny Prijonoc3573762006-07-10 21:39:24 +00001736 msec_res = app.client.timeout * 1000;
Benny Prijono514ca6b2006-07-03 01:30:01 +00001737 }
1738
Benny Prijonoc3573762006-07-10 21:39:24 +00001739 if (msec_res == 0) msec_res = 1;
1740
1741 if (app.client.requests_sent.sec) {
1742 pj_time_val duration;
1743 duration = app.client.requests_sent;
1744 PJ_TIME_VAL_SUB(duration, app.client.first_request);
1745 msec_req = PJ_TIME_VAL_MSEC(duration);
1746 } else {
1747 msec_req = app.client.timeout * 1000;
1748 }
1749
1750 if (msec_req == 0) msec_req = 1;
1751
Benny Prijono775a1b22006-07-11 09:53:27 +00001752 if (app.client.job_submitted < app.client.job_count)
1753 puts("\ntimed-out!\n");
1754 else
1755 puts("\ndone.\n");
Benny Prijono514ca6b2006-07-03 01:30:01 +00001756
Benny Prijono1479b652006-07-03 14:18:17 +00001757 pj_ansi_snprintf(
1758 report, sizeof(report),
Benny Prijono775a1b22006-07-11 09:53:27 +00001759 "Total %d %s sent in %d ms at rate of %d/sec\n"
Benny Prijono49f682a2006-07-11 12:25:45 +00001760 "Total %d responses receieved in %d ms at rate of %d/sec:",
Benny Prijonoc3573762006-07-10 21:39:24 +00001761 app.client.job_submitted, test_type, msec_req,
1762 app.client.job_submitted * 1000 / msec_req,
1763 app.client.total_responses, msec_res,
Benny Prijono49f682a2006-07-11 12:25:45 +00001764 app.client.total_responses*1000/msec_res);
1765 write_report(report);
1766
1767 /* Print detailed response code received */
1768 pj_ansi_sprintf(report, "\nDetailed responses received:");
1769 write_report(report);
1770
1771 for (i=0; i<PJ_ARRAY_SIZE(app.client.response_codes); ++i) {
1772 const pj_str_t *reason;
1773
1774 if (app.client.response_codes[i] == 0)
1775 continue;
1776
1777 reason = pjsip_get_status_text(i);
1778 pj_ansi_snprintf( report, sizeof(report),
1779 " - %d responses: %7d (%.*s)",
1780 i, app.client.response_codes[i],
1781 (int)reason->slen, reason->ptr);
1782 write_report(report);
1783 }
1784
1785 /* Total responses and rate */
1786 pj_ansi_snprintf( report, sizeof(report),
1787 " ------\n"
1788 " TOTAL responses: %7d (rate=%d/sec)\n",
1789 app.client.total_responses,
1790 app.client.total_responses*1000/msec_res);
Benny Prijono1479b652006-07-03 14:18:17 +00001791
1792 write_report(report);
1793
1794 pj_ansi_sprintf(report, "Maximum outstanding job: %d",
1795 app.client.stat_max_window);
1796 write_report(report);
1797
Benny Prijono514ca6b2006-07-03 01:30:01 +00001798
1799 } else {
1800 /* Server mode */
1801 char s[10];
1802 pj_status_t status;
1803 unsigned i;
1804
Benny Prijono514ca6b2006-07-03 01:30:01 +00001805 puts("pjsip-perf started in server-mode");
1806
1807 printf("Receiving requests on the following URIs:\n"
Benny Prijono49f682a2006-07-11 12:25:45 +00001808 " sip:0@%.*s:%d%s for stateless handling\n"
1809 " sip:1@%.*s:%d%s for stateful handling\n"
1810 " sip:2@%.*s:%d%s for call handling\n",
Benny Prijono514ca6b2006-07-03 01:30:01 +00001811 (int)app.local_addr.slen,
1812 app.local_addr.ptr,
1813 app.local_port,
Benny Prijono49f682a2006-07-11 12:25:45 +00001814 (app.use_tcp ? ";transport=tcp" : ""),
Benny Prijono514ca6b2006-07-03 01:30:01 +00001815 (int)app.local_addr.slen,
1816 app.local_addr.ptr,
1817 app.local_port,
Benny Prijono49f682a2006-07-11 12:25:45 +00001818 (app.use_tcp ? ";transport=tcp" : ""),
Benny Prijono514ca6b2006-07-03 01:30:01 +00001819 (int)app.local_addr.slen,
1820 app.local_addr.ptr,
Benny Prijonoc03a3c52006-07-03 02:31:10 +00001821 app.local_port,
Benny Prijono49f682a2006-07-11 12:25:45 +00001822 (app.use_tcp ? ";transport=tcp" : ""));
Benny Prijonodcc0cbf2006-07-16 10:40:37 +00001823 printf("INVITE with non-matching user part will be handled call-statefully\n");
Benny Prijono514ca6b2006-07-03 01:30:01 +00001824
1825 for (i=0; i<app.thread_count; ++i) {
Benny Prijono7db431e2006-07-23 14:38:49 +00001826 status = pj_thread_create(app.pool, NULL, &server_thread,
1827 (void*)(long)i, 0, 0, &app.thread[i]);
Benny Prijono514ca6b2006-07-03 01:30:01 +00001828 if (status != PJ_SUCCESS) {
1829 app_perror(THIS_FILE, "Unable to create thread", status);
1830 return 1;
1831 }
1832 }
1833
Benny Prijono49f682a2006-07-11 12:25:45 +00001834 puts("\nPress <ENTER> to quit\n");
Benny Prijono514ca6b2006-07-03 01:30:01 +00001835 fflush(stdout);
1836 fgets(s, sizeof(s), stdin);
1837
1838 app.thread_quit = PJ_TRUE;
1839 for (i=0; i<app.thread_count; ++i) {
1840 pj_thread_join(app.thread[i]);
1841 app.thread[i] = NULL;
1842 }
Benny Prijono49f682a2006-07-11 12:25:45 +00001843
1844 puts("");
Benny Prijono514ca6b2006-07-03 01:30:01 +00001845 }
1846
1847
1848 destroy_app();
1849
1850 return 0;
1851}
1852