Debian Codegraph @

Xpjproject/pjsip-apps/src/samples/pjsip-perf.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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
69 #if (defined(PJ_WIN32) && PJ_WIN32!=0) || (defined(PJ_WIN64) && PJ_WIN64!=0)
70 # include <windows.h>
71 #endif
72
73 #define THIS_FILE "pjsip-perf.c"
74 #define DEFAULT_COUNT (pjsip_cfg()->tsx.max_count/2>10000?10000:pjsip_cfg()->tsx.max_count/2)
75 #define JOB_WINDOW 1000
76 #define TERMINATE_TSX(x,c)
77
78
79 #ifndef CACHING_POOL_SIZE
80 # define CACHING_POOL_SIZE (256*1024*1024)
81 #endif
82
83
84 /* Static message body for INVITE, when stateful processing is
85 * invoked (instead of call-stateful, where SDP is generated
86 * dynamically.
87 */
88 static 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"
95 "m=audio 4000 RTP/AVP 0 8 3 103 102 101\r\n"
96 "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
108 static pj_str_t mime_application = { "application", 11};
109 static pj_str_t mime_sdp = {"sdp", 3};
110
111
112 struct srv_state
113 {
114 unsigned stateless_cnt;
115 unsigned stateful_cnt;
116 unsigned call_cnt;
117 };
118
119
120 struct app
121 {
122 pj_caching_pool cp;
123 pj_pool_t *pool;
124 pj_bool_t use_tcp;
125 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;
140
141 int log_level;
142
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;
152 unsigned stat_max_window;
153 pj_time_val first_request;
154 pj_time_val requests_sent;
155 pj_time_val last_completion;
156 unsigned total_responses;
157 unsigned response_codes[800];
158 } client;
159
160 struct {
161 pj_bool_t send_trying;
162 pj_bool_t send_ringing;
163 unsigned delay;
164 struct srv_state prev_state;
165 struct srv_state cur_state;
166 } server;
167
168
169 } app;
170
171 struct call
172 {
173 pjsip_inv_session *inv;
174 pj_timer_entry ans_timer;
175 };
176
177
178 static 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 */
191 static pj_bool_t mod_stateless_on_rx_request(pjsip_rx_data *rdata);
192
193 /* Module to handle incoming requests statelessly.
194 */
195 static 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
213 static 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/SIPS scheme */
222 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(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 */
252 static pj_bool_t mod_stateful_on_rx_request(pjsip_rx_data *rdata);
253
254 /* Module to handle incoming requests statefully.
255 */
256 static 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
274 static 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/SIPS scheme */
283 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(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 */
327 static pj_bool_t mod_call_on_rx_request(pjsip_rx_data *rdata);
328
329 /* Module to handle incoming requests callly.
330 */
331 static 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
349 static 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
394 static 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
399 PJ_UNUSED_ARG(h);
400
401 entry->id = 0;
402 send_response(call->inv, NULL, 200, &has_initial);
403 }
404
405 static 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;
414 pj_bool_t has_initial = PJ_FALSE;
415 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/SIPS scheme */
420 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
421 return PJ_FALSE;
422
423 sip_uri = (pjsip_sip_uri*) uri;
424
425 /* Only want to handle INVITE requests. */
426 if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD) {
427 return PJ_FALSE;
428 }
429
430
431 /* 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
447 /* 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 status = pjsip_endpt_send_response(app.sip_endpt, &res_addr, tdata,
463 NULL, NULL);
464 if (status != PJ_SUCCESS) pjsip_tx_data_dec_ref(tdata);
465 } else {
466 /* Respond with 500 (Internal Server Error) */
467 pjsip_endpt_respond_stateless(app.sip_endpt, rdata, 500, NULL,
468 NULL, NULL);
469 }
470
471 return PJ_TRUE;
472 }
473 }
474
475 /* Create UAS dialog */
476 status = pjsip_dlg_create_uas_and_inc_lock( pjsip_ua_instance(), rdata,
477 &app.local_contact, &dlg);
478 if (status != PJ_SUCCESS) {
479 const pj_str_t reason = pj_str("Unable to create dialog");
480 pjsip_endpt_respond_stateless( app.sip_endpt, rdata,
481 500, &reason,
482 NULL, NULL);
483 return PJ_TRUE;
484 }
485
486 /* Alloc call structure. */
487 call = pj_pool_zalloc(dlg->pool, sizeof(struct call));
488
489 /* Create SDP from PJMEDIA */
490 if (app.real_sdp) {
491 status = pjmedia_endpt_create_sdp(app.med_endpt, rdata->tp_info.pool,
492 app.skinfo_cnt, app.skinfo,
493 &sdp);
494 } else {
495 sdp = app.dummy_sdp;
496 }
497
498 /* Create UAS invite session */
499 status = pjsip_inv_create_uas( dlg, rdata, sdp, 0, &call->inv);
500 if (status != PJ_SUCCESS) {
501 pjsip_dlg_create_response(dlg, rdata, 500, NULL, &tdata);
502 pjsip_dlg_send_response(dlg, pjsip_rdata_get_tsx(rdata), tdata);
503 pjsip_dlg_dec_lock(dlg);
504 return PJ_TRUE;
505 }
506
507 /* Invite session has been created, decrement & release dialog lock. */
508 pjsip_dlg_dec_lock(dlg);
509
510 /* Send 100/Trying if needed */
511 if (app.server.send_trying) {
512 status = send_response(call->inv, rdata, 100, &has_initial);
513 if (status != PJ_SUCCESS)
514 return PJ_TRUE;
515 }
516
517 /* Send 180/Ringing if needed */
518 if (app.server.send_ringing) {
519 status = send_response(call->inv, rdata, 180, &has_initial);
520 if (status != PJ_SUCCESS)
521 return PJ_TRUE;
522 }
523
524 /* Simulate call processing delay */
525 if (app.server.delay) {
526 pj_time_val delay;
527
528 call->ans_timer.id = 1;
529 call->ans_timer.user_data = call;
530 call->ans_timer.cb = &answer_timer_cb;
531
532 delay.sec = 0;
533 delay.msec = app.server.delay;
534 pj_time_val_normalize(&delay);
535
536 pjsip_endpt_schedule_timer(app.sip_endpt, &call->ans_timer, &delay);
537
538 } else {
539 /* Send the 200 response immediately . */
540 status = send_response(call->inv, rdata, 200, &has_initial);
541 PJ_ASSERT_ON_FAIL(status == PJ_SUCCESS, return PJ_TRUE);
542 }
543
544 /* Done */
545 app.server.cur_state.call_cnt++;
546
547 return PJ_TRUE;
548 }
549
550
551
552 /**************************************************************************
553 * Default handler when incoming request is not handled by any other
554 * modules.
555 */
556 static pj_bool_t mod_responder_on_rx_request(pjsip_rx_data *rdata);
557
558 /* Module to handle incoming requests statelessly.
559 */
560 static pjsip_module mod_responder =
561 {
562 NULL, NULL, /* prev, next. */
563 { "mod-responder", 13 }, /* Name. */
564 -1, /* Id */
565 PJSIP_MOD_PRIORITY_APPLICATION+1, /* Priority */
566 NULL, /* load() */
567 NULL, /* start() */
568 NULL, /* stop() */
569 NULL, /* unload() */
570 &mod_responder_on_rx_request, /* on_rx_request() */
571 NULL, /* on_rx_response() */
572 NULL, /* on_tx_request. */
573 NULL, /* on_tx_response() */
574 NULL, /* on_tsx_state() */
575 };
576
577
578 static pj_bool_t mod_responder_on_rx_request(pjsip_rx_data *rdata)
579 {
580 const pj_str_t reason = pj_str("Not expecting request at this URI");
581
582 /*
583 * Respond any requests (except ACK!) with 500.
584 */
585 if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) {
586 pjsip_endpt_respond_stateless(app.sip_endpt, rdata, 500, &reason,
587 NULL, NULL);
588 }
589
590 return PJ_TRUE;
591 }
592
593
594
595 /*****************************************************************************
596 * Below is a simple module to log all incoming and outgoing SIP messages
597 */
598
599
600 /* Notification on incoming messages */
601 static pj_bool_t logger_on_rx_msg(pjsip_rx_data *rdata)
602 {
603 PJ_LOG(3,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
604 "%.*s\n"
605 "--end msg--",
606 rdata->msg_info.len,
607 pjsip_rx_data_get_info(rdata),
608 rdata->tp_info.transport->type_name,
609 rdata->pkt_info.src_name,
610 rdata->pkt_info.src_port,
611 (int)rdata->msg_info.len,
612 rdata->msg_info.msg_buf));
613
614 /* Always return false, otherwise messages will not get processed! */
615 return PJ_FALSE;
616 }
617
618 /* Notification on outgoing messages */
619 static pj_status_t logger_on_tx_msg(pjsip_tx_data *tdata)
620 {
621
622 /* Important note:
623 * tp_info field is only valid after outgoing messages has passed
624 * transport layer. So don't try to access tp_info when the module
625 * has lower priority than transport layer.
626 */
627
628 PJ_LOG(3,(THIS_FILE, "TX %ld bytes %s to %s %s:%d:\n"
629 "%.*s\n"
630 "--end msg--",
631 (tdata->buf.cur - tdata->buf.start),
632 pjsip_tx_data_get_info(tdata),
633 tdata->tp_info.transport->type_name,
634 tdata->tp_info.dst_name,
635 tdata->tp_info.dst_port,
636 (int)(tdata->buf.cur - tdata->buf.start),
637 tdata->buf.start));
638
639 /* Always return success, otherwise message will not get sent! */
640 return PJ_SUCCESS;
641 }
642
643 /* The module instance. */
644 static pjsip_module msg_logger =
645 {
646 NULL, NULL, /* prev, next. */
647 { "mod-siprtp-log", 14 }, /* Name. */
648 -1, /* Id */
649 PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
650 NULL, /* load() */
651 NULL, /* start() */
652 NULL, /* stop() */
653 NULL, /* unload() */
654 &logger_on_rx_msg, /* on_rx_request() */
655 &logger_on_rx_msg, /* on_rx_response() */
656 &logger_on_tx_msg, /* on_tx_request. */
657 &logger_on_tx_msg, /* on_tx_response() */
658 NULL, /* on_tsx_state() */
659
660 };
661
662
663
664 /**************************************************************************
665 * Test Client.
666 */
667
668 static pj_bool_t mod_test_on_rx_response(pjsip_rx_data *rdata);
669
670 static void call_on_media_update( pjsip_inv_session *inv,
671 pj_status_t status);
672 static void call_on_state_changed( pjsip_inv_session *inv,
673 pjsip_event *e);
674 static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e);
675
676
677 /* Module to handle incoming requests callly.
678 */
679 static pjsip_module mod_test =
680 {
681 NULL, NULL, /* prev, next. */
682 { "mod-test", 8 }, /* Name. */
683 -1, /* Id */
684 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
685 NULL, /* load() */
686 NULL, /* start() */
687 NULL, /* stop() */
688 NULL, /* unload() */
689 NULL, /* on_rx_request() */
690 &mod_test_on_rx_response, /* on_rx_response() */
691 NULL, /* on_tx_request. */
692 NULL, /* on_tx_response() */
693 NULL, /* on_tsx_state() */
694 };
695
696
697 static void report_completion(int status_code)
698 {
699 app.client.job_finished++;
700 if (status_code >= 200 && status_code < 800)
701 app.client.response_codes[status_code]++;
702 app.client.total_responses++;
703 pj_gettimeofday(&app.client.last_completion);
704 }
705
706
707 /* Handler when response is received. */
708 static pj_bool_t mod_test_on_rx_response(pjsip_rx_data *rdata)
709 {
710 if (pjsip_rdata_get_tsx(rdata) == NULL) {
711 report_completion(rdata->msg_info.msg->line.status.code);
712 }
713
714 return PJ_TRUE;
715 }
716
717
718 /*
719 * Create app
720 */
721 static pj_status_t create_app(void)
722 {
723 pj_status_t status;
724
725 status = pj_init();
726 if (status != PJ_SUCCESS) {
727 app_perror(THIS_FILE, "Error initializing pjlib", status);
728 return status;
729 }
730
731 /* init PJLIB-UTIL: */
732 status = pjlib_util_init();
733 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
734
735 /* Must create a pool factory before we can allocate any memory. */
736 pj_caching_pool_init(&app.cp, &pj_pool_factory_default_policy,
737 CACHING_POOL_SIZE);
738
739 /* Create application pool for misc. */
740 app.pool = pj_pool_create(&app.cp.factory, "app", 1000, 1000, NULL);
741
742 /* Create the endpoint: */
743 status = pjsip_endpt_create(&app.cp.factory, pj_gethostname()->ptr,
744 &app.sip_endpt);
745 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
746
747
748 return status;
749 }
750
751
752 /*
753 * Init SIP stack
754 */
755 static pj_status_t init_sip()
756 {
757 pj_status_t status = -1;
758
759 /* Add UDP/TCP transport. */
760 {
761 pj_sockaddr_in addr;
762 pjsip_host_port addrname;
763 const char *transport_type = NULL;
764
765 pj_bzero(&addr, sizeof(addr));
766 addr.sin_family = pj_AF_INET();
767 addr.sin_addr.s_addr = 0;
768 addr.sin_port = pj_htons((pj_uint16_t)app.local_port);
769
770 if (app.local_addr.slen) {
771 addrname.host = app.local_addr;
772 addrname.port = 5060;
773 }
774 if (app.local_port != 0)
775 addrname.port = app.local_port;
776
777 if (0) {
778 #if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0
779 } else if (app.use_tcp) {
780 pj_sockaddr_in local_addr;
781 pjsip_tpfactory *tpfactory;
782
783 transport_type = "tcp";
784 pj_sockaddr_in_init(&local_addr, 0, (pj_uint16_t)app.local_port);
785 status = pjsip_tcp_transport_start(app.sip_endpt, &local_addr,
786 app.thread_count, &tpfactory);
787 if (status == PJ_SUCCESS) {
788 app.local_addr = tpfactory->addr_name.host;
789 app.local_port = tpfactory->addr_name.port;
790 }
791 #endif
792 } else {
793 pjsip_transport *tp;
794
795 transport_type = "udp";
796 status = pjsip_udp_transport_start(app.sip_endpt, &addr,
797 (app.local_addr.slen ? &addrname:NULL),
798 app.thread_count, &tp);
799 if (status == PJ_SUCCESS) {
800 app.local_addr = tp->local_name.host;
801 app.local_port = tp->local_name.port;
802 }
803
804 }
805 if (status != PJ_SUCCESS) {
806 app_perror(THIS_FILE, "Unable to start transport", status);
807 return status;
808 }
809
810 app.local_uri.ptr = pj_pool_alloc(app.pool, 128);
811 app.local_uri.slen = pj_ansi_snprintf(app.local_uri.ptr, 128,
812 "<sip:pjsip-perf@%.*s:%d;transport=%s>",
813 (int)app.local_addr.slen,
814 app.local_addr.ptr,
815 app.local_port,
816 transport_type);
817
818 app.local_contact = app.local_uri;
819 }
820
821 /*
822 * Init transaction layer.
823 * This will create/initialize transaction hash tables etc.
824 */
825 status = pjsip_tsx_layer_init_module(app.sip_endpt);
826 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
827
828 /* Initialize UA layer. */
829 status = pjsip_ua_init_module( app.sip_endpt, NULL );
830 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
831
832 /* Initialize 100rel support */
833 status = pjsip_100rel_init_module(app.sip_endpt);
834 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
835
836 /* Init invite session module. */
837 {
838 pjsip_inv_callback inv_cb;
839
840 /* Init the callback for INVITE session: */
841 pj_bzero(&inv_cb, sizeof(inv_cb));
842 inv_cb.on_state_changed = &call_on_state_changed;
843 inv_cb.on_new_session = &call_on_forked;
844 inv_cb.on_media_update = &call_on_media_update;
845
846 /* Initialize invite session module: */
847 status = pjsip_inv_usage_init(app.sip_endpt, &inv_cb);
848 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
849 }
850
851 /* Register our module to receive incoming requests. */
852 status = pjsip_endpt_register_module( app.sip_endpt, &mod_test);
853 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
854
855
856 /* Register stateless server module */
857 status = pjsip_endpt_register_module( app.sip_endpt, &mod_stateless_server);
858 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
859
860 /* Register default responder module */
861 status = pjsip_endpt_register_module( app.sip_endpt, &mod_responder);
862 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
863
864 /* Register stateless server module */
865 status = pjsip_endpt_register_module( app.sip_endpt, &mod_stateful_server);
866 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
867
868
869 /* Register call server module */
870 status = pjsip_endpt_register_module( app.sip_endpt, &mod_call_server);
871 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
872
873
874 /* Done */
875 return PJ_SUCCESS;
876 }
877
878
879 /*
880 * Destroy SIP
881 */
882 static void destroy_app()
883 {
884 unsigned i;
885
886 app.thread_quit = 1;
887 for (i=0; i<app.thread_count; ++i) {
888 if (app.thread[i]) {
889 pj_thread_join(app.thread[i]);
890 pj_thread_destroy(app.thread[i]);
891 app.thread[i] = NULL;
892 }
893 }
894
895 if (app.sip_endpt) {
896 pjsip_endpt_destroy(app.sip_endpt);
897 app.sip_endpt = NULL;
898 }
899
900 if (app.pool) {
901 pj_pool_release(app.pool);
902 app.pool = NULL;
903 /* Field peak_used_size is deprecated by #3897 */
904 //PJ_LOG(3,(THIS_FILE, "Peak memory size: %luMB",
905 // app.cp.peak_used_size / 1000000));
906 pj_caching_pool_destroy(&app.cp);
907 }
908
909 /* Shutdown PJLIB */
910 pj_shutdown();
911 }
912
913
914 /*
915 * Init media stack.
916 */
917 static pj_status_t init_media()
918 {
919 unsigned i;
920 pj_uint16_t rtp_port;
921 pj_status_t status;
922
923
924 /* Initialize media endpoint so that at least error subsystem is properly
925 * initialized.
926 */
927 status = pjmedia_endpt_create(&app.cp.factory,
928 pjsip_endpt_get_ioqueue(app.sip_endpt), 0,
929 &app.med_endpt);
930 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
931
932
933 /* Must register all codecs to be supported */
934 pjmedia_codec_register_audio_codecs(app.med_endpt, NULL);
935
936 /* Init dummy socket addresses */
937 app.skinfo_cnt = 0;
938 for (i=0, rtp_port=4000; i<PJ_ARRAY_SIZE(app.skinfo); ++i, rtp_port+=2) {
939 pjmedia_sock_info *skinfo;
940
941 skinfo = &app.skinfo[i];
942
943 pj_sockaddr_in_init(&skinfo->rtp_addr_name.ipv4, &app.local_addr,
944 (pj_uint16_t)rtp_port);
945 pj_sockaddr_in_init(&skinfo->rtp_addr_name.ipv4, &app.local_addr,
946 (pj_uint16_t)(rtp_port+1));
947 app.skinfo_cnt++;
948 }
949
950 /* Generate dummy SDP */
951 dummy_sdp_str.slen = pj_ansi_strlen(dummy_sdp_str.ptr);
952 status = pjmedia_sdp_parse(app.pool, dummy_sdp_str.ptr, dummy_sdp_str.slen,
953 &app.dummy_sdp);
954 if (status != PJ_SUCCESS) {
955 app_perror(THIS_FILE, "Error parsing dummy SDP", status);
956 return status;
957 }
958
959
960 /* Done */
961 return PJ_SUCCESS;
962 }
963
964
965 /* This is notification from the call about media negotiation
966 * status. This is called for client calls only.
967 */
968 static void call_on_media_update( pjsip_inv_session *inv,
969 pj_status_t status)
970 {
971 if (status != PJ_SUCCESS) {
972 pjsip_tx_data *tdata;
973 pj_status_t status2;
974
975 status2 = pjsip_inv_end_session(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE,
976 NULL, &tdata);
977 if (status2 == PJ_SUCCESS && tdata)
978 status2 = pjsip_inv_send_msg(inv, tdata);
979 }
980 }
981
982
983 /* This is notification from the call when the call state has changed.
984 * This is called for client calls only.
985 */
986 static void call_on_state_changed( pjsip_inv_session *inv,
987 pjsip_event *e)
988 {
989 PJ_UNUSED_ARG(e);
990
991 /* Bail out if the session has been counted before */
992 if (inv->mod_data[mod_test.id] != NULL)
993 return;
994
995 /* Bail out if this is not an outgoing call */
996 if (inv->role != PJSIP_UAC_ROLE)
997 return;
998
999 if (inv->state == PJSIP_INV_STATE_CONFIRMED) {
1000 pjsip_tx_data *tdata;
1001 pj_status_t status;
1002
1003 //report_completion(200);
1004 //inv->mod_data[mod_test.id] = (void*)1;
1005
1006 status = pjsip_inv_end_session(inv, PJSIP_SC_OK, NULL, &tdata);
1007 if (status == PJ_SUCCESS && tdata)
1008 status = pjsip_inv_send_msg(inv, tdata);
1009
1010 } else if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
1011 report_completion(inv->cause);
1012 inv->mod_data[mod_test.id] = (void*)(pj_ssize_t)1;
1013 }
1014 }
1015
1016
1017 /* Not implemented for now */
1018 static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e)
1019 {
1020 /* Do nothing */
1021 PJ_UNUSED_ARG(inv);
1022 PJ_UNUSED_ARG(e);
1023 }
1024
1025
1026 /*
1027 * Make outgoing call.
1028 */
1029 static pj_status_t make_call(const pj_str_t *dst_uri)
1030 {
1031 struct call *call;
1032 pjsip_dialog *dlg;
1033 pjmedia_sdp_session *sdp;
1034 pjsip_tx_data *tdata;
1035 pj_status_t status;
1036
1037
1038 /* Create UAC dialog */
1039 status = pjsip_dlg_create_uac( pjsip_ua_instance(),
1040 &app.local_uri, /* local URI */
1041 &app.local_contact, /* local Contact */
1042 dst_uri, /* remote URI */
1043 dst_uri, /* remote target */
1044 &dlg); /* dialog */
1045 if (status != PJ_SUCCESS) {
1046 return status;
1047 }
1048
1049 /* Create call */
1050 call = pj_pool_zalloc(dlg->pool, sizeof(struct call));
1051
1052 /* Create SDP */
1053 if (app.real_sdp) {
1054 status = pjmedia_endpt_create_sdp(app.med_endpt, dlg->pool, 1,
1055 app.skinfo, &sdp);
1056 if (status != PJ_SUCCESS) {
1057 pjsip_dlg_terminate(dlg);
1058 return status;
1059 }
1060 } else
1061 sdp = app.dummy_sdp;
1062
1063 /* Create the INVITE session. */
1064 status = pjsip_inv_create_uac( dlg, sdp, 0, &call->inv);
1065 if (status != PJ_SUCCESS) {
1066 pjsip_dlg_terminate(dlg);
1067 return status;
1068 }
1069
1070
1071 /* Create initial INVITE request.
1072 * This INVITE request will contain a perfectly good request and
1073 * an SDP body as well.
1074 */
1075 status = pjsip_inv_invite(call->inv, &tdata);
1076 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
1077
1078
1079 /* Send initial INVITE request.
1080 * From now on, the invite session's state will be reported to us
1081 * via the invite session callbacks.
1082 */
1083 status = pjsip_inv_send_msg(call->inv, tdata);
1084 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
1085
1086
1087 return PJ_SUCCESS;
1088 }
1089
1090
1091 /*
1092 * Verify that valid SIP url is given.
1093 */
1094 static pj_status_t verify_sip_url(const char *c_url)
1095 {
1096 pjsip_uri *p;
1097 pj_pool_t *pool;
1098 char *url;
1099 pj_size_t len = (c_url ? pj_ansi_strlen(c_url) : 0);
1100
1101 if (!len) return -1;
1102
1103 pool = pj_pool_create(&app.cp.factory, "check%p", 1024, 0, NULL);
1104 if (!pool) return PJ_ENOMEM;
1105
1106 url = pj_pool_alloc(pool, len+1);
1107 pj_ansi_strxcpy(url, c_url, len+1);
1108 url[len] = '\0';
1109
1110 p = pjsip_parse_uri(pool, url, len, 0);
1111 if (!p || pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0)
1112 p = NULL;
1113
1114 pj_pool_release(pool);
1115 return p ? 0 : -1;
1116 }
1117
1118
1119 static void usage(void)
1120 {
1121 printf(
1122 "Usage:\n"
1123 " pjsip-perf [OPTIONS] -- to start as server\n"
1124 " pjsip-perf [OPTIONS] URL -- to call server (possibly itself)\n"
1125 "\n"
1126 "where:\n"
1127 " URL The SIP URL to be contacted.\n"
1128 "\n"
1129 "Client options:\n"
1130 " --method=METHOD, -m Set test method (set to INVITE for call benchmark)\n"
1131 " [default: OPTIONS]\n"
1132 " --count=N, -n Set total number of requests to initiate\n"
1133 " [default=%d]\n"
1134 " --stateless, -s Set to operate in stateless mode\n"
1135 " [default: stateful]\n"
1136 " --timeout=SEC, -t Set client timeout [default=60 sec]\n"
1137 " --window=COUNT, -w Set maximum outstanding job [default: %d]\n"
1138 "\n"
1139 "SDP options (client and server):\n"
1140 " --real-sdp Generate real SDP from pjmedia, and also perform\n"
1141 " proper SDP negotiation [default: dummy]\n"
1142 "\n"
1143 "Client and Server options:\n"
1144 " --local-port=PORT, -p Set local port [default: 5060]\n"
1145 " --use-tcp, -T Use TCP instead of UDP. Note that when started as\n"
1146 " client, you must add ;transport=tcp parameter to URL\n"
1147 " [default: no]\n"
1148 " --thread-count=N Set number of worker threads [default=1]\n"
1149 " --trying Send 100/Trying response (server, default no)\n"
1150 " --ringing Send 180/Ringing response (server, default no)\n"
1151 " --delay=MS, -d Delay answering call by MS (server, default no)\n"
1152 "\n"
1153 "Misc options:\n"
1154 " --help, -h Display this screen\n"
1155 " --verbose, -v Verbose logging (put more than once for even more)\n"
1156 "\n"
1157 "When started as server, pjsip-perf can be contacted on the following URIs:\n"
1158 " - sip:0@server-addr To handle requests statelessly.\n"
1159 " - sip:1@server-addr To handle requests statefully.\n"
1160 " - sip:2@server-addr To handle INVITE call.\n",
1161 DEFAULT_COUNT, JOB_WINDOW);
1162 }
1163
1164
1165 static int my_atoi(const char *s)
1166 {
1167 pj_str_t ss = pj_str((char*)s);
1168 return pj_strtoul(&ss);
1169 }
1170
1171
1172 static pj_status_t init_options(int argc, char *argv[])
1173 {
1174 enum { OPT_THREAD_COUNT = 1, OPT_REAL_SDP, OPT_TRYING, OPT_RINGING };
1175 struct pj_getopt_option long_options[] = {
1176 { "local-port", 1, 0, 'p' },
1177 { "count", 1, 0, 'c' },
1178 { "thread-count", 1, 0, OPT_THREAD_COUNT },
1179 { "method", 1, 0, 'm' },
1180 { "help", 0, 0, 'h' },
1181 { "stateless", 0, 0, 's' },
1182 { "timeout", 1, 0, 't' },
1183 { "real-sdp", 0, 0, OPT_REAL_SDP },
1184 { "verbose", 0, 0, 'v' },
1185 { "use-tcp", 0, 0, 'T' },
1186 { "window", 1, 0, 'w' },
1187 { "delay", 1, 0, 'd' },
1188 { "trying", 0, 0, OPT_TRYING},
1189 { "ringing", 0, 0, OPT_RINGING},
1190 { NULL, 0, 0, 0 },
1191 };
1192 int c;
1193 int option_index;
1194
1195 /* Init default application configs */
1196 app.local_port = 5060;
1197 app.thread_count = 1;
1198 app.client.job_count = DEFAULT_COUNT;
1199 app.client.method = *pjsip_get_options_method();
1200 app.client.job_window = c = JOB_WINDOW;
1201 app.client.timeout = 60;
1202 app.log_level = 3;
1203
1204
1205 /* Parse options */
1206 pj_optind = 0;
1207 while((c=pj_getopt_long(argc,argv, "p:c:m:t:w:d:hsv",
1208 long_options, &option_index))!=-1)
1209 {
1210 switch (c) {
1211 case 'p':
1212 app.local_port = my_atoi(pj_optarg);
1213 if (app.local_port < 0 || app.local_port > 65535) {
1214 PJ_LOG(3,(THIS_FILE, "Invalid --local-port %s", pj_optarg));
1215 return -1;
1216 }
1217 break;
1218
1219 case 'c':
1220 app.client.job_count = my_atoi(pj_optarg);
1221 if (app.client.job_count > pjsip_cfg()->tsx.max_count)
1222 PJ_LOG(3,(THIS_FILE,
1223 "Warning: --count value (%d) exceeds maximum "
1224 "transaction count (%d)", app.client.job_count,
1225 pjsip_cfg()->tsx.max_count));
1226 break;
1227
1228 case OPT_THREAD_COUNT:
1229 app.thread_count = my_atoi(pj_optarg);
1230 if (app.thread_count < 1 || app.thread_count > 16) {
1231 PJ_LOG(3,(THIS_FILE, "Invalid --thread-count %s", pj_optarg));
1232 return -1;
1233 }
1234 break;
1235
1236 case 'm':
1237 {
1238 pj_str_t temp = pj_str((char*)pj_optarg);
1239 pjsip_method_init_np(&app.client.method, &temp);
1240 }
1241 break;
1242
1243 case 'h':
1244 usage();
1245 return -1;
1246
1247 case 's':
1248 app.client.stateless = PJ_TRUE;
1249 break;
1250
1251 case OPT_REAL_SDP:
1252 app.real_sdp = 1;
1253 break;
1254
1255 case 'v':
1256 app.log_level++;
1257 break;
1258
1259 case 't':
1260 app.client.timeout = my_atoi(pj_optarg);
1261 if (app.client.timeout > 600) {
1262 PJ_LOG(3,(THIS_FILE, "Invalid --timeout %s", pj_optarg));
1263 return -1;
1264 }
1265 break;
1266
1267 case 'w':
1268 app.client.job_window = my_atoi(pj_optarg);
1269 if (app.client.job_window <= 0) {
1270 PJ_LOG(3,(THIS_FILE, "Invalid --window %s", pj_optarg));
1271 return -1;
1272 }
1273 break;
1274
1275 case 'T':
1276 app.use_tcp = PJ_TRUE;
1277 break;
1278
1279 case 'd':
1280 app.server.delay = my_atoi(pj_optarg);
1281 if (app.server.delay > 3600) {
1282 PJ_LOG(3,(THIS_FILE, "I think --delay %s is too long",
1283 pj_optarg));
1284 return -1;
1285 }
1286 break;
1287
1288 case OPT_TRYING:
1289 app.server.send_trying = 1;
1290 break;
1291
1292 case OPT_RINGING:
1293 app.server.send_ringing = 1;
1294 break;
1295
1296 default:
1297 PJ_LOG(1,(THIS_FILE,
1298 "Invalid argument. Use --help to see help"));
1299 return -1;
1300 }
1301 }
1302
1303 if (pj_optind != argc) {
1304
1305 if (verify_sip_url(argv[pj_optind]) != PJ_SUCCESS) {
1306 PJ_LOG(1,(THIS_FILE, "Invalid SIP URI %s", argv[pj_optind]));
1307 return -1;
1308 }
1309 app.client.dst_uri = pj_str(argv[pj_optind]);
1310
1311 pj_optind++;
1312
1313 }
1314
1315 if (pj_optind != argc) {
1316 PJ_LOG(1,(THIS_FILE, "Error: unknown options %s", argv[pj_optind]));
1317 return -1;
1318 }
1319
1320 return 0;
1321 }
1322
1323
1324 /* Send one stateless request */
1325 static pj_status_t submit_stateless_job(void)
1326 {
1327 pjsip_tx_data *tdata;
1328 pj_status_t status;
1329
1330 status = pjsip_endpt_create_request(app.sip_endpt, &app.client.method,
1331 &app.client.dst_uri, &app.local_uri,
1332 &app.client.dst_uri, &app.local_contact,
1333 NULL, -1, NULL, &tdata);
1334 if (status != PJ_SUCCESS) {
1335 app_perror(THIS_FILE, "Error creating request", status);
1336 report_completion(701);
1337 return status;
1338 }
1339
1340 status = pjsip_endpt_send_request_stateless(app.sip_endpt, tdata, NULL,
1341 NULL);
1342 if (status != PJ_SUCCESS) {
1343 pjsip_tx_data_dec_ref(tdata);
1344 app_perror(THIS_FILE, "Error sending stateless request", status);
1345 report_completion(701);
1346 return status;
1347 }
1348
1349 return PJ_SUCCESS;
1350 }
1351
1352
1353 /* This callback is called when client transaction state has changed */
1354 static void tsx_completion_cb(void *token, pjsip_event *event)
1355 {
1356 pjsip_transaction *tsx;
1357
1358 PJ_UNUSED_ARG(token);
1359
1360 if (event->type != PJSIP_EVENT_TSX_STATE)
1361 return;
1362
1363 tsx = event->body.tsx_state.tsx;
1364
1365 if (tsx->mod_data[mod_test.id] != NULL) {
1366 /* This transaction has been calculated before */
1367 return;
1368 }
1369
1370 if (tsx->state==PJSIP_TSX_STATE_TERMINATED) {
1371 report_completion(tsx->status_code);
1372 tsx->mod_data[mod_test.id] = (void*)(pj_ssize_t)1;
1373 }
1374 else if (tsx->method.id == PJSIP_INVITE_METHOD &&
1375 tsx->state == PJSIP_TSX_STATE_CONFIRMED) {
1376
1377 report_completion(tsx->status_code);
1378 tsx->mod_data[mod_test.id] = (void*)(pj_ssize_t)1;
1379
1380 } else if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
1381
1382 report_completion(tsx->status_code);
1383 tsx->mod_data[mod_test.id] = (void*)(pj_ssize_t)1;
1384
1385 TERMINATE_TSX(tsx, tsx->status_code);
1386 }
1387 }
1388
1389
1390 /* Send one stateful request */
1391 static pj_status_t submit_job(void)
1392 {
1393 pjsip_tx_data *tdata;
1394 pj_status_t status;
1395
1396 status = pjsip_endpt_create_request(app.sip_endpt, &app.client.method,
1397 &app.client.dst_uri, &app.local_uri,
1398 &app.client.dst_uri, &app.local_contact,
1399 NULL, -1, NULL, &tdata);
1400 if (status != PJ_SUCCESS) {
1401 app_perror(THIS_FILE, "Error creating request", status);
1402 report_completion(701);
1403 return status;
1404 }
1405
1406 status = pjsip_endpt_send_request(app.sip_endpt, tdata, -1, NULL,
1407 &tsx_completion_cb);
1408 if (status != PJ_SUCCESS) {
1409 app_perror(THIS_FILE, "Error sending stateful request", status);
1410 //should have been reported by tsx_completion_cb().
1411 //report_completion(701);
1412 //No longer necessary (r777)
1413 //pjsip_tx_data_dec_ref(tdata);
1414 }
1415 return status;
1416 }
1417
1418
1419 /* Client worker thread */
1420 static int client_thread(void *arg)
1421 {
1422 pj_time_val end_time, last_report, now;
1423 unsigned thread_index = (unsigned)(long)(pj_ssize_t)arg;
1424 unsigned cycle = 0, last_cycle = 0;
1425
1426 pj_thread_sleep(100);
1427
1428 pj_gettimeofday(&end_time);
1429 end_time.sec += app.client.timeout;
1430
1431 pj_gettimeofday(&last_report);
1432
1433 if (app.client.first_request.sec == 0) {
1434 pj_gettimeofday(&app.client.first_request);
1435 }
1436
1437 /* Submit all jobs */
1438 while (app.client.job_submitted < app.client.job_count && !app.thread_quit){
1439 pj_time_val timeout = { 0, 1 };
1440 unsigned i;
1441 int outstanding;
1442 pj_status_t status;
1443
1444 /* Calculate current outstanding job */
1445 outstanding = app.client.job_submitted - app.client.job_finished;
1446
1447 /* Update stats on max outstanding jobs */
1448 if (outstanding > (int)app.client.stat_max_window)
1449 app.client.stat_max_window = outstanding;
1450
1451 /* Wait if there are more pending jobs than allowed in the
1452 * window. But spawn a new job anyway if no events are happening
1453 * after we wait for some time.
1454 */
1455 for (i=0; outstanding > (int)app.client.job_window && i<1000; ++i) {
1456 pj_time_val wait = { 0, 500 };
1457 unsigned count = 0;
1458
1459 pjsip_endpt_handle_events2(app.sip_endpt, &wait, &count);
1460 outstanding = app.client.job_submitted - app.client.job_finished;
1461
1462 if (count == 0)
1463 break;
1464
1465 ++cycle;
1466 }
1467
1468
1469 /* Submit one job */
1470 if (app.client.method.id == PJSIP_INVITE_METHOD) {
1471 status = make_call(&app.client.dst_uri);
1472 } else if (app.client.stateless) {
1473 status = submit_stateless_job();
1474 } else {
1475 status = submit_job();
1476 }
1477 PJ_UNUSED_ARG(status);
1478
1479 ++app.client.job_submitted;
1480 ++cycle;
1481
1482 /* Handle event */
1483 pjsip_endpt_handle_events2(app.sip_endpt, &timeout, NULL);
1484
1485 /* Check for time out, also print report */
1486 if (cycle - last_cycle >= 500) {
1487 pj_gettimeofday(&now);
1488 if (PJ_TIME_VAL_GTE(now, end_time)) {
1489 break;
1490 }
1491 last_cycle = cycle;
1492
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 }
1500 }
1501 }
1502
1503 if (app.client.requests_sent.sec == 0) {
1504 pj_gettimeofday(&app.client.requests_sent);
1505 }
1506
1507
1508 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
1516 /* Wait until all jobs completes, or timed out */
1517 pj_gettimeofday(&now);
1518 while (PJ_TIME_VAL_LT(now, end_time) &&
1519 app.client.job_finished < app.client.job_count &&
1520 !app.thread_quit)
1521 {
1522 pj_time_val timeout = { 0, 1 };
1523 unsigned i;
1524
1525 for (i=0; i<1000; ++i) {
1526 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
1536 /* Wait couple of seconds to let jobs completes (e.g. ACKs to be sent) */
1537 pj_gettimeofday(&now);
1538 end_time = now;
1539 end_time.sec += 2;
1540 while (PJ_TIME_VAL_LT(now, end_time))
1541 {
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;
1551 }
1552
1553 pj_gettimeofday(&now);
1554 }
1555
1556 return 0;
1557 }
1558
1559
1560 static const char *good_number(char *buf, unsigned buf_size, pj_int32_t val)
1561 {
1562 if (val < 1000) {
1563 pj_ansi_snprintf(buf, buf_size, "%d", val);
1564 } else if (val < 1000000) {
1565 pj_ansi_snprintf(buf, buf_size, "%d.%dK",
1566 val / 1000,
1567 (val % 1000) / 100);
1568 } else {
1569 pj_ansi_snprintf(buf, buf_size, "%d.%02dM",
1570 val / 1000000,
1571 (val % 1000000) / 10000);
1572 }
1573
1574 return buf;
1575 }
1576
1577
1578 static int server_thread(void *arg)
1579 {
1580 pj_time_val timeout = { 0, 1 };
1581 unsigned thread_index = (unsigned)(long)(pj_ssize_t)arg;
1582 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, sizeof(str_stateless),
1621 app.server.cur_state.stateless_cnt);
1622 good_number(str_stateful, sizeof(str_stateful),
1623 app.server.cur_state.stateful_cnt);
1624 good_number(str_call, sizeof(str_call),
1625 app.server.cur_state.call_cnt);
1626
1627 printf("Total(rate): stateless:%s (%d/s), statefull:%s (%d/s), call:%s (%d/s) \r",
1628 str_stateless, stateless*1000/msec,
1629 str_stateful, stateful*1000/msec,
1630 str_call, call*1000/msec);
1631 fflush(stdout);
1632
1633 app.server.prev_state = app.server.cur_state;
1634 }
1635 }
1636 }
1637
1638 return 0;
1639 }
1640
1641 static void write_report(const char *msg)
1642 {
1643 puts(msg);
1644
1645 #if (defined(PJ_WIN32) && PJ_WIN32!=0) || (defined(PJ_WIN64) && PJ_WIN64!=0)
1646 OutputDebugString(msg);
1647 OutputDebugString("\n");
1648 #endif
1649 }
1650
1651
1652 int main(int argc, char *argv[])
1653 {
1654 static char report[1024];
1655
1656 printf("PJSIP Performance Measurement Tool v%s\n"
1657 "(c)2006 pjsip.org\n\n",
1658 PJ_VERSION);
1659
1660 if (create_app() != 0)
1661 return 1;
1662
1663 if (init_options(argc, argv) != 0)
1664 return 1;
1665
1666 if (init_sip() != 0)
1667 return 1;
1668
1669 if (init_media() != 0)
1670 return 1;
1671
1672 pj_log_set_level(app.log_level);
1673
1674 if (app.log_level > 4) {
1675 pjsip_endpt_register_module(app.sip_endpt, &msg_logger);
1676 }
1677
1678
1679 /* Misc infos */
1680 if (app.client.dst_uri.slen != 0) {
1681 if (app.client.method.id == PJSIP_INVITE_METHOD) {
1682 if (app.client.stateless) {
1683 PJ_LOG(3,(THIS_FILE,
1684 "Info: --stateless option makes no sense for INVITE,"
1685 " ignored."));
1686 }
1687 }
1688
1689 }
1690
1691
1692
1693 if (app.client.dst_uri.slen) {
1694 /* Client mode */
1695 pj_status_t status;
1696 char test_type[64];
1697 unsigned msec_req, msec_res;
1698 unsigned i;
1699
1700 /* Get the job name */
1701 if (app.client.method.id == PJSIP_INVITE_METHOD) {
1702 pj_ansi_strxcpy(test_type, "INVITE calls", sizeof(test_type));
1703 } else if (app.client.stateless) {
1704 pj_ansi_snprintf(test_type, sizeof(test_type),
1705 "stateless %.*s requests",
1706 (int)app.client.method.name.slen,
1707 app.client.method.name.ptr);
1708 } else {
1709 pj_ansi_snprintf(test_type, sizeof(test_type),
1710 "stateful %.*s requests",
1711 (int)app.client.method.name.slen,
1712 app.client.method.name.ptr);
1713 }
1714
1715
1716 printf("Sending %d %s to '%.*s' with %d maximum outstanding jobs, please wait..\n",
1717 app.client.job_count, test_type,
1718 (int)app.client.dst_uri.slen, app.client.dst_uri.ptr,
1719 app.client.job_window);
1720
1721 for (i=0; i<app.thread_count; ++i) {
1722 status = pj_thread_create(app.pool, NULL, &client_thread,
1723 (void*)(pj_ssize_t)i, 0, 0,
1724 &app.thread[i]);
1725 if (status != PJ_SUCCESS) {
1726 app_perror(THIS_FILE, "Unable to create thread", status);
1727 return 1;
1728 }
1729 }
1730
1731 for (i=0; i<app.thread_count; ++i) {
1732 pj_thread_join(app.thread[i]);
1733 app.thread[i] = NULL;
1734 }
1735
1736 if (app.client.last_completion.sec) {
1737 pj_time_val duration;
1738 duration = app.client.last_completion;
1739 PJ_TIME_VAL_SUB(duration, app.client.first_request);
1740 msec_res = PJ_TIME_VAL_MSEC(duration);
1741 } else {
1742 msec_res = app.client.timeout * 1000;
1743 }
1744
1745 if (msec_res == 0) msec_res = 1;
1746
1747 if (app.client.requests_sent.sec) {
1748 pj_time_val duration;
1749 duration = app.client.requests_sent;
1750 PJ_TIME_VAL_SUB(duration, app.client.first_request);
1751 msec_req = PJ_TIME_VAL_MSEC(duration);
1752 } else {
1753 msec_req = app.client.timeout * 1000;
1754 }
1755
1756 if (msec_req == 0) msec_req = 1;
1757
1758 if (app.client.job_submitted < app.client.job_count)
1759 puts("\ntimed-out!\n");
1760 else
1761 puts("\ndone.\n");
1762
1763 pj_ansi_snprintf(
1764 report, sizeof(report),
1765 "Total %d %s sent in %d ms at rate of %d/sec\n"
1766 "Total %d responses received in %d ms at rate of %d/sec:",
1767 app.client.job_submitted, test_type, msec_req,
1768 app.client.job_submitted * 1000 / msec_req,
1769 app.client.total_responses, msec_res,
1770 app.client.total_responses*1000/msec_res);
1771 write_report(report);
1772
1773 /* Print detailed response code received */
1774 pj_ansi_snprintf(report, sizeof(report),
1775 "\nDetailed responses received:");
1776 write_report(report);
1777
1778 for (i=0; i<PJ_ARRAY_SIZE(app.client.response_codes); ++i) {
1779 const pj_str_t *reason;
1780
1781 if (app.client.response_codes[i] == 0)
1782 continue;
1783
1784 reason = pjsip_get_status_text(i);
1785 pj_ansi_snprintf( report, sizeof(report),
1786 " - %d responses: %7d (%.*s)",
1787 i, app.client.response_codes[i],
1788 (int)reason->slen, reason->ptr);
1789 write_report(report);
1790 }
1791
1792 /* Total responses and rate */
1793 pj_ansi_snprintf( report, sizeof(report),
1794 " ------\n"
1795 " TOTAL responses: %7d (rate=%d/sec)\n",
1796 app.client.total_responses,
1797 app.client.total_responses*1000/msec_res);
1798
1799 write_report(report);
1800
1801 pj_ansi_snprintf(report, sizeof(report),
1802 "Maximum outstanding job: %d",
1803 app.client.stat_max_window);
1804 write_report(report);
1805
1806
1807 } else {
1808 /* Server mode */
1809 char s[10], *unused;
1810 pj_status_t status;
1811 unsigned i;
1812
1813 puts("pjsip-perf started in server-mode");
1814
1815 printf("Receiving requests on the following URIs:\n"
1816 " sip:0@%.*s:%d%s for stateless handling\n"
1817 " sip:1@%.*s:%d%s for stateful handling\n"
1818 " sip:2@%.*s:%d%s for call handling\n",
1819 (int)app.local_addr.slen,
1820 app.local_addr.ptr,
1821 app.local_port,
1822 (app.use_tcp ? ";transport=tcp" : ""),
1823 (int)app.local_addr.slen,
1824 app.local_addr.ptr,
1825 app.local_port,
1826 (app.use_tcp ? ";transport=tcp" : ""),
1827 (int)app.local_addr.slen,
1828 app.local_addr.ptr,
1829 app.local_port,
1830 (app.use_tcp ? ";transport=tcp" : ""));
1831 printf("INVITE with non-matching user part will be handled call-statefully\n");
1832
1833 for (i=0; i<app.thread_count; ++i) {
1834 status = pj_thread_create(app.pool, NULL, &server_thread,
1835 (void*)(pj_ssize_t)i, 0, 0,
1836 &app.thread[i]);
1837 if (status != PJ_SUCCESS) {
1838 app_perror(THIS_FILE, "Unable to create thread", status);
1839 return 1;
1840 }
1841 }
1842
1843 puts("\nPress <ENTER> to quit\n");
1844 fflush(stdout);
1845 unused = fgets(s, sizeof(s), stdin);
1846 PJ_UNUSED_ARG(unused);
1847
1848 app.thread_quit = PJ_TRUE;
1849 for (i=0; i<app.thread_count; ++i) {
1850 pj_thread_join(app.thread[i]);
1851 app.thread[i] = NULL;
1852 }
1853
1854 puts("");
1855 }
1856
1857
1858 destroy_app();
1859
1860 return 0;
1861 }
1862