blob: e3d7fc43c533e7035517ddf7e2673deffffacd95 [file] [log] [blame]
Benny Prijono514ca6b2006-07-03 01:30:01 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono514ca6b2006-07-03 01:30:01 +00004 *
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{
Benny Prijonoc95a0f02007-04-09 07:06:08 +0000754 pj_status_t status = -1;
Benny Prijono514ca6b2006-07-03 01:30:01 +0000755
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 Prijonoc95a0f02007-04-09 07:06:08 +0000760 const char *transport_type = NULL;
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 Prijono3569c0d2007-04-06 10:29:20 +0000774 if (0) {
775#if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0
776 } else if (app.use_tcp) {
Benny Prijonoc03a3c52006-07-03 02:31:10 +0000777 pj_sockaddr_in local_addr;
778 pjsip_tpfactory *tpfactory;
779
780 transport_type = "tcp";
Benny Prijono1479b652006-07-03 14:18:17 +0000781 pj_sockaddr_in_init(&local_addr, 0, (pj_uint16_t)app.local_port);
Benny Prijonoc03a3c52006-07-03 02:31:10 +0000782 status = pjsip_tcp_transport_start(app.sip_endpt, &local_addr,
783 app.thread_count, &tpfactory);
784 if (status == PJ_SUCCESS) {
785 app.local_addr = tpfactory->addr_name.host;
786 app.local_port = tpfactory->addr_name.port;
787 }
Benny Prijono3569c0d2007-04-06 10:29:20 +0000788#endif
Benny Prijonoc03a3c52006-07-03 02:31:10 +0000789 } else {
790 pjsip_transport *tp;
791
792 transport_type = "udp";
793 status = pjsip_udp_transport_start(app.sip_endpt, &addr,
794 (app.local_addr.slen ? &addrname:NULL),
795 app.thread_count, &tp);
796 if (status == PJ_SUCCESS) {
797 app.local_addr = tp->local_name.host;
798 app.local_port = tp->local_name.port;
799 }
800
801 }
Benny Prijono514ca6b2006-07-03 01:30:01 +0000802 if (status != PJ_SUCCESS) {
Benny Prijonoc03a3c52006-07-03 02:31:10 +0000803 app_perror(THIS_FILE, "Unable to start transport", status);
Benny Prijono514ca6b2006-07-03 01:30:01 +0000804 return status;
805 }
806
Benny Prijono514ca6b2006-07-03 01:30:01 +0000807 app.local_uri.ptr = pj_pool_alloc(app.pool, 128);
808 app.local_uri.slen = pj_ansi_sprintf(app.local_uri.ptr,
Benny Prijonoc03a3c52006-07-03 02:31:10 +0000809 "<sip:pjsip-perf@%.*s:%d;transport=%s>",
810 (int)app.local_addr.slen,
811 app.local_addr.ptr,
812 app.local_port,
813 transport_type);
Benny Prijono514ca6b2006-07-03 01:30:01 +0000814
815 app.local_contact = app.local_uri;
816 }
817
818 /*
819 * Init transaction layer.
820 * This will create/initialize transaction hash tables etc.
821 */
822 status = pjsip_tsx_layer_init_module(app.sip_endpt);
823 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
824
825 /* Initialize UA layer. */
826 status = pjsip_ua_init_module( app.sip_endpt, NULL );
827 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
828
829 /* Init invite session module. */
830 {
831 pjsip_inv_callback inv_cb;
832
833 /* Init the callback for INVITE session: */
Benny Prijonoac623b32006-07-03 15:19:31 +0000834 pj_bzero(&inv_cb, sizeof(inv_cb));
Benny Prijono514ca6b2006-07-03 01:30:01 +0000835 inv_cb.on_state_changed = &call_on_state_changed;
836 inv_cb.on_new_session = &call_on_forked;
837 inv_cb.on_media_update = &call_on_media_update;
838
839 /* Initialize invite session module: */
840 status = pjsip_inv_usage_init(app.sip_endpt, &inv_cb);
841 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
842 }
843
844 /* Register our module to receive incoming requests. */
845 status = pjsip_endpt_register_module( app.sip_endpt, &mod_test);
846 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
847
848
849 /* Register stateless server module */
850 status = pjsip_endpt_register_module( app.sip_endpt, &mod_stateless_server);
851 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
852
Benny Prijonodcc0cbf2006-07-16 10:40:37 +0000853 /* Register default responder module */
854 status = pjsip_endpt_register_module( app.sip_endpt, &mod_responder);
855 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono514ca6b2006-07-03 01:30:01 +0000856
857 /* Register stateless server module */
858 status = pjsip_endpt_register_module( app.sip_endpt, &mod_stateful_server);
859 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
860
861
862 /* Register call server module */
863 status = pjsip_endpt_register_module( app.sip_endpt, &mod_call_server);
864 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
865
866
867 /* Done */
868 return PJ_SUCCESS;
869}
870
871
872/*
873 * Destroy SIP
874 */
875static void destroy_app()
876{
877 unsigned i;
878
879 app.thread_quit = 1;
880 for (i=0; i<app.thread_count; ++i) {
881 if (app.thread[i]) {
882 pj_thread_join(app.thread[i]);
883 pj_thread_destroy(app.thread[i]);
884 app.thread[i] = NULL;
885 }
886 }
887
888 if (app.sip_endpt) {
889 pjsip_endpt_destroy(app.sip_endpt);
890 app.sip_endpt = NULL;
891 }
892
893 if (app.pool) {
894 pj_pool_release(app.pool);
895 app.pool = NULL;
Benny Prijono1ef06df2006-07-09 10:06:44 +0000896 PJ_LOG(3,(THIS_FILE, "Peak memory size: %uMB",
897 app.cp.peak_used_size / 1000000));
Benny Prijono514ca6b2006-07-03 01:30:01 +0000898 pj_caching_pool_destroy(&app.cp);
899 }
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000900
901 /* Shutdown PJLIB */
902 pj_shutdown();
Benny Prijono514ca6b2006-07-03 01:30:01 +0000903}
904
905
906/*
907 * Init media stack.
908 */
909static pj_status_t init_media()
910{
911 unsigned i;
912 pj_uint16_t rtp_port;
913 pj_status_t status;
914
915
916 /* Initialize media endpoint so that at least error subsystem is properly
917 * initialized.
918 */
919 status = pjmedia_endpt_create(&app.cp.factory,
920 pjsip_endpt_get_ioqueue(app.sip_endpt), 0,
921 &app.med_endpt);
922 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
923
924
925 /* Must register all codecs to be supported */
Benny Prijono06d1d0e2007-01-27 18:09:28 +0000926#if defined(PJMEDIA_HAS_G711_CODEC) && PJMEDIA_HAS_G711_CODEC!=0
Benny Prijono514ca6b2006-07-03 01:30:01 +0000927 pjmedia_codec_g711_init(app.med_endpt);
Benny Prijono06d1d0e2007-01-27 18:09:28 +0000928#endif
929#if defined(PJMEDIA_HAS_GSM_CODEC) && PJMEDIA_HAS_GSM_CODEC!=0
Benny Prijono514ca6b2006-07-03 01:30:01 +0000930 pjmedia_codec_gsm_init(app.med_endpt);
Benny Prijono06d1d0e2007-01-27 18:09:28 +0000931#endif
932#if defined(PJMEDIA_HAS_SPEEX_CODEC) && PJMEDIA_HAS_SPEEX_CODEC!=0
Benny Prijono514ca6b2006-07-03 01:30:01 +0000933 pjmedia_codec_speex_init(app.med_endpt, PJMEDIA_SPEEX_NO_UWB, 3, 3);
Benny Prijono06d1d0e2007-01-27 18:09:28 +0000934#endif
Benny Prijono514ca6b2006-07-03 01:30:01 +0000935
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, &app.local_addr,
944 (pj_uint16_t)rtp_port);
945 pj_sockaddr_in_init(&skinfo->rtp_addr_name, &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 */
968static 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 status;
974
975 status = pjsip_inv_end_session(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE,
976 NULL, &tdata);
977 if (status == PJ_SUCCESS && tdata)
978 status = 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 */
986static 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*)1;
1013 }
1014}
1015
1016
1017/* Not implemented for now */
1018static 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 */
1029static 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 */
1094static pj_status_t verify_sip_url(const char *c_url)
1095{
1096 pjsip_uri *p;
1097 pj_pool_t *pool;
1098 char *url;
1099 int 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_strcpy(url, c_url);
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
1119static 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"
Benny Prijonoc3573762006-07-10 21:39:24 +00001127 " URL The SIP URL to be contacted.\n"
Benny Prijono514ca6b2006-07-03 01:30:01 +00001128 "\n"
Benny Prijonoc3573762006-07-10 21:39:24 +00001129 "Client options:\n"
Benny Prijono775a1b22006-07-11 09:53:27 +00001130 " --method=METHOD, -m Set test method (set to INVITE for call benchmark)\n"
1131 " [default: OPTIONS]\n"
Benny Prijonoc3573762006-07-10 21:39:24 +00001132 " --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"
Benny Prijono514ca6b2006-07-03 01:30:01 +00001140 " --real-sdp Generate real SDP from pjmedia, and also perform\n"
Benny Prijonoc3573762006-07-10 21:39:24 +00001141 " 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"
Benny Prijono775a1b22006-07-11 09:53:27 +00001145 " --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"
Benny Prijonoc3573762006-07-10 21:39:24 +00001148 " --thread-count=N Set number of worker threads [default=1]\n"
Benny Prijonof521eb02006-08-06 23:07:25 +00001149 " --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"
Benny Prijonoc3573762006-07-10 21:39:24 +00001152 "\n"
1153 "Misc options:\n"
Benny Prijono514ca6b2006-07-03 01:30:01 +00001154 " --help, -h Display this screen\n"
Benny Prijono775a1b22006-07-11 09:53:27 +00001155 " --verbose, -v Verbose logging (put more than once for even more)\n"
Benny Prijono514ca6b2006-07-03 01:30:01 +00001156 "\n"
1157 "When started as server, pjsip-perf can be contacted on the following URIs:\n"
Benny Prijonoc3573762006-07-10 21:39:24 +00001158 " - 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",
Benny Prijono1479b652006-07-03 14:18:17 +00001161 DEFAULT_COUNT, JOB_WINDOW);
Benny Prijono514ca6b2006-07-03 01:30:01 +00001162}
1163
1164
1165static int my_atoi(const char *s)
1166{
1167 pj_str_t ss = pj_str((char*)s);
1168 return pj_strtoul(&ss);
1169}
1170
1171
1172static pj_status_t init_options(int argc, char *argv[])
1173{
Benny Prijonof521eb02006-08-06 23:07:25 +00001174 enum { OPT_THREAD_COUNT = 1, OPT_REAL_SDP, OPT_TRYING, OPT_RINGING };
Benny Prijono514ca6b2006-07-03 01:30:01 +00001175 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' },
Benny Prijonoc03a3c52006-07-03 02:31:10 +00001185 { "use-tcp", 0, 0, 'T' },
Benny Prijono1479b652006-07-03 14:18:17 +00001186 { "window", 1, 0, 'w' },
Benny Prijonof521eb02006-08-06 23:07:25 +00001187 { "delay", 1, 0, 'd' },
1188 { "trying", 0, 0, OPT_TRYING},
1189 { "ringing", 0, 0, OPT_RINGING},
Benny Prijono514ca6b2006-07-03 01:30:01 +00001190 { 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_options_method;
1200 app.client.job_window = c = JOB_WINDOW;
1201 app.client.timeout = 60;
Benny Prijonoc3573762006-07-10 21:39:24 +00001202 app.log_level = 3;
Benny Prijono514ca6b2006-07-03 01:30:01 +00001203
1204
1205 /* Parse options */
1206 pj_optind = 0;
Benny Prijonof521eb02006-08-06 23:07:25 +00001207 while((c=pj_getopt_long(argc,argv, "p:c:m:t:w:d:hsv",
Benny Prijono514ca6b2006-07-03 01:30:01 +00001208 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 < 0) {
1222 PJ_LOG(3,(THIS_FILE, "Invalid --local-port %s", pj_optarg));
1223 return -1;
1224 }
1225 if (app.client.job_count > PJSIP_MAX_TSX_COUNT)
1226 PJ_LOG(3,(THIS_FILE,
1227 "Warning: --count value (%d) exceeds maximum "
1228 "transaction count (%d)", app.client.job_count,
1229 PJSIP_MAX_TSX_COUNT));
1230 break;
1231
1232 case OPT_THREAD_COUNT:
1233 app.thread_count = my_atoi(pj_optarg);
1234 if (app.thread_count < 1 || app.thread_count > 16) {
1235 PJ_LOG(3,(THIS_FILE, "Invalid --thread-count %s", pj_optarg));
1236 return -1;
1237 }
1238 break;
1239
1240 case 'm':
1241 {
1242 pj_str_t temp = pj_str((char*)pj_optarg);
1243 pjsip_method_init_np(&app.client.method, &temp);
1244 }
1245 break;
1246
1247 case 'h':
1248 usage();
1249 return -1;
1250
1251 case 's':
1252 app.client.stateless = PJ_TRUE;
1253 break;
1254
1255 case OPT_REAL_SDP:
1256 app.real_sdp = 1;
1257 break;
1258
1259 case 'v':
Benny Prijonoc3573762006-07-10 21:39:24 +00001260 app.log_level++;
Benny Prijono514ca6b2006-07-03 01:30:01 +00001261 break;
1262
1263 case 't':
1264 app.client.timeout = my_atoi(pj_optarg);
1265 if (app.client.timeout < 0 || app.client.timeout > 600) {
1266 PJ_LOG(3,(THIS_FILE, "Invalid --timeout %s", pj_optarg));
1267 return -1;
1268 }
1269 break;
1270
Benny Prijono1479b652006-07-03 14:18:17 +00001271 case 'w':
1272 app.client.job_window = my_atoi(pj_optarg);
1273 if (app.client.job_window <= 0) {
1274 PJ_LOG(3,(THIS_FILE, "Invalid --window %s", pj_optarg));
1275 return -1;
1276 }
1277 break;
1278
Benny Prijonoc03a3c52006-07-03 02:31:10 +00001279 case 'T':
1280 app.use_tcp = PJ_TRUE;
1281 break;
1282
Benny Prijonof521eb02006-08-06 23:07:25 +00001283 case 'd':
1284 app.server.delay = my_atoi(pj_optarg);
1285 if (app.server.delay > 3600) {
1286 PJ_LOG(3,(THIS_FILE, "I think --delay %s is too long",
1287 pj_optarg));
1288 return -1;
1289 }
1290 break;
1291
1292 case OPT_TRYING:
1293 app.server.send_trying = 1;
1294 break;
1295
1296 case OPT_RINGING:
1297 app.server.send_ringing = 1;
1298 break;
1299
Benny Prijono514ca6b2006-07-03 01:30:01 +00001300 default:
1301 PJ_LOG(1,(THIS_FILE,
1302 "Invalid argument. Use --help to see help"));
1303 return -1;
1304 }
1305 }
1306
1307 if (pj_optind != argc) {
1308
1309 if (verify_sip_url(argv[pj_optind]) != PJ_SUCCESS) {
1310 PJ_LOG(1,(THIS_FILE, "Invalid SIP URI %s", argv[pj_optind]));
1311 return -1;
1312 }
1313 app.client.dst_uri = pj_str(argv[pj_optind]);
1314
1315 pj_optind++;
1316
1317 }
1318
1319 if (pj_optind != argc) {
1320 PJ_LOG(1,(THIS_FILE, "Error: unknown options %s", argv[pj_optind]));
1321 return -1;
1322 }
1323
1324 return 0;
1325}
1326
1327
1328/* Send one stateless request */
1329static pj_status_t submit_stateless_job(void)
1330{
1331 pjsip_tx_data *tdata;
1332 pj_status_t status;
1333
1334 status = pjsip_endpt_create_request(app.sip_endpt, &app.client.method,
1335 &app.client.dst_uri, &app.local_uri,
1336 &app.client.dst_uri, &app.local_contact,
1337 NULL, -1, NULL, &tdata);
1338 if (status != PJ_SUCCESS) {
1339 app_perror(THIS_FILE, "Error creating request", status);
1340 report_completion(701);
1341 return status;
1342 }
1343
1344 status = pjsip_endpt_send_request_stateless(app.sip_endpt, tdata, NULL,
1345 NULL);
1346 if (status != PJ_SUCCESS) {
1347 pjsip_tx_data_dec_ref(tdata);
1348 app_perror(THIS_FILE, "Error sending stateless request", status);
1349 report_completion(701);
1350 return status;
1351 }
1352
1353 return PJ_SUCCESS;
1354}
1355
1356
1357/* This callback is called when client transaction state has changed */
1358static void tsx_completion_cb(void *token, pjsip_event *event)
1359{
1360 pjsip_transaction *tsx;
1361
1362 PJ_UNUSED_ARG(token);
1363
1364 if (event->type != PJSIP_EVENT_TSX_STATE)
1365 return;
1366
1367 tsx = event->body.tsx_state.tsx;
1368
1369 if (tsx->mod_data[mod_test.id] != NULL) {
1370 /* This transaction has been calculated before */
1371 return;
1372 }
1373
1374 if (tsx->state==PJSIP_TSX_STATE_TERMINATED) {
1375 report_completion(tsx->status_code);
1376 tsx->mod_data[mod_test.id] = (void*)1;
1377 }
1378 else if (tsx->method.id == PJSIP_INVITE_METHOD &&
1379 tsx->state == PJSIP_TSX_STATE_CONFIRMED) {
1380
1381 report_completion(tsx->status_code);
1382 tsx->mod_data[mod_test.id] = (void*)1;
1383
1384 } else if (tsx->state == PJSIP_TSX_STATE_COMPLETED) {
1385
1386 report_completion(tsx->status_code);
1387 tsx->mod_data[mod_test.id] = (void*)1;
1388
Benny Prijono1479b652006-07-03 14:18:17 +00001389 TERMINATE_TSX(tsx, tsx->status_code);
Benny Prijono514ca6b2006-07-03 01:30:01 +00001390 }
1391}
1392
1393
1394/* Send one stateful request */
1395static pj_status_t submit_job(void)
1396{
1397 pjsip_tx_data *tdata;
1398 pj_status_t status;
1399
1400 status = pjsip_endpt_create_request(app.sip_endpt, &app.client.method,
1401 &app.client.dst_uri, &app.local_uri,
1402 &app.client.dst_uri, &app.local_contact,
1403 NULL, -1, NULL, &tdata);
1404 if (status != PJ_SUCCESS) {
1405 app_perror(THIS_FILE, "Error creating request", status);
1406 report_completion(701);
1407 return status;
1408 }
1409
1410 status = pjsip_endpt_send_request(app.sip_endpt, tdata, -1, NULL,
1411 &tsx_completion_cb);
1412 if (status != PJ_SUCCESS) {
1413 app_perror(THIS_FILE, "Error sending stateful request", status);
1414 //should have been reported by tsx_completion_cb().
1415 //report_completion(701);
Benny Prijono27f01dd2006-10-16 21:07:19 +00001416 //No longer necessary (r777)
1417 //pjsip_tx_data_dec_ref(tdata);
Benny Prijono514ca6b2006-07-03 01:30:01 +00001418 }
1419 return status;
1420}
1421
1422
1423/* Client worker thread */
1424static int client_thread(void *arg)
1425{
Benny Prijono775a1b22006-07-11 09:53:27 +00001426 pj_time_val end_time, last_report, now;
Benny Prijono7db431e2006-07-23 14:38:49 +00001427 unsigned thread_index = (unsigned)(long)arg;
Benny Prijono49f682a2006-07-11 12:25:45 +00001428 unsigned cycle = 0, last_cycle = 0;
Benny Prijono514ca6b2006-07-03 01:30:01 +00001429
1430 pj_thread_sleep(100);
1431
1432 pj_gettimeofday(&end_time);
1433 end_time.sec += app.client.timeout;
1434
Benny Prijono775a1b22006-07-11 09:53:27 +00001435 pj_gettimeofday(&last_report);
1436
Benny Prijono514ca6b2006-07-03 01:30:01 +00001437 if (app.client.first_request.sec == 0) {
1438 pj_gettimeofday(&app.client.first_request);
1439 }
1440
1441 /* Submit all jobs */
Benny Prijono775a1b22006-07-11 09:53:27 +00001442 while (app.client.job_submitted < app.client.job_count && !app.thread_quit){
Benny Prijonoc3573762006-07-10 21:39:24 +00001443 pj_time_val timeout = { 0, 1 };
Benny Prijono514ca6b2006-07-03 01:30:01 +00001444 unsigned i;
1445 int outstanding;
1446 pj_status_t status;
1447
Benny Prijono1479b652006-07-03 14:18:17 +00001448 /* Calculate current outstanding job */
1449 outstanding = app.client.job_submitted - app.client.job_finished;
1450
1451 /* Update stats on max outstanding jobs */
1452 if (outstanding > (int)app.client.stat_max_window)
1453 app.client.stat_max_window = outstanding;
1454
Benny Prijono514ca6b2006-07-03 01:30:01 +00001455 /* Wait if there are more pending jobs than allowed in the
Benny Prijono49f682a2006-07-11 12:25:45 +00001456 * window. But spawn a new job anyway if no events are happening
1457 * after we wait for some time.
Benny Prijono514ca6b2006-07-03 01:30:01 +00001458 */
Benny Prijono49f682a2006-07-11 12:25:45 +00001459 for (i=0; outstanding > (int)app.client.job_window && i<1000; ++i) {
1460 pj_time_val wait = { 0, 500 };
1461 unsigned count = 0;
1462
1463 pjsip_endpt_handle_events2(app.sip_endpt, &wait, &count);
Benny Prijono514ca6b2006-07-03 01:30:01 +00001464 outstanding = app.client.job_submitted - app.client.job_finished;
Benny Prijono49f682a2006-07-11 12:25:45 +00001465
1466 if (count == 0)
1467 break;
1468
1469 ++cycle;
Benny Prijono514ca6b2006-07-03 01:30:01 +00001470 }
1471
Benny Prijono1479b652006-07-03 14:18:17 +00001472
1473 /* Submit one job */
Benny Prijono514ca6b2006-07-03 01:30:01 +00001474 if (app.client.method.id == PJSIP_INVITE_METHOD) {
1475 status = make_call(&app.client.dst_uri);
1476 } else if (app.client.stateless) {
1477 status = submit_stateless_job();
1478 } else {
1479 status = submit_job();
1480 }
1481
1482 ++app.client.job_submitted;
Benny Prijono49f682a2006-07-11 12:25:45 +00001483 ++cycle;
Benny Prijono514ca6b2006-07-03 01:30:01 +00001484
Benny Prijono1479b652006-07-03 14:18:17 +00001485 /* Handle event */
1486 pjsip_endpt_handle_events2(app.sip_endpt, &timeout, NULL);
1487
Benny Prijono775a1b22006-07-11 09:53:27 +00001488 /* Check for time out, also print report */
Benny Prijono49f682a2006-07-11 12:25:45 +00001489 if (cycle - last_cycle >= 500) {
Benny Prijono1479b652006-07-03 14:18:17 +00001490 pj_gettimeofday(&now);
Benny Prijono775a1b22006-07-11 09:53:27 +00001491 if (PJ_TIME_VAL_GTE(now, end_time)) {
Benny Prijono514ca6b2006-07-03 01:30:01 +00001492 break;
Benny Prijono775a1b22006-07-11 09:53:27 +00001493 }
Benny Prijono49f682a2006-07-11 12:25:45 +00001494 last_cycle = cycle;
Benny Prijono775a1b22006-07-11 09:53:27 +00001495
1496
1497 if (thread_index == 0 && now.sec-last_report.sec >= 2) {
1498 printf("\r%d jobs started, %d completed... ",
1499 app.client.job_submitted, app.client.job_finished);
1500 fflush(stdout);
1501 last_report = now;
1502 }
Benny Prijono514ca6b2006-07-03 01:30:01 +00001503 }
1504 }
1505
Benny Prijonoc3573762006-07-10 21:39:24 +00001506 if (app.client.requests_sent.sec == 0) {
1507 pj_gettimeofday(&app.client.requests_sent);
1508 }
1509
1510
Benny Prijono775a1b22006-07-11 09:53:27 +00001511 if (thread_index == 0) {
1512 printf("\r%d jobs started, %d completed%s\n",
1513 app.client.job_submitted, app.client.job_finished,
1514 (app.client.job_submitted!=app.client.job_finished ?
1515 ", waiting..." : ".") );
1516 fflush(stdout);
1517 }
1518
Benny Prijono514ca6b2006-07-03 01:30:01 +00001519 /* Wait until all jobs completes, or timed out */
Benny Prijonoc3573762006-07-10 21:39:24 +00001520 pj_gettimeofday(&now);
Benny Prijono775a1b22006-07-11 09:53:27 +00001521 while (PJ_TIME_VAL_LT(now, end_time) &&
Benny Prijonoc3573762006-07-10 21:39:24 +00001522 app.client.job_finished < app.client.job_count &&
1523 !app.thread_quit)
1524 {
1525 pj_time_val timeout = { 0, 1 };
Benny Prijono514ca6b2006-07-03 01:30:01 +00001526 unsigned i;
1527
Benny Prijonoc3573762006-07-10 21:39:24 +00001528 for (i=0; i<1000; ++i) {
Benny Prijono775a1b22006-07-11 09:53:27 +00001529 unsigned count;
1530 count = 0;
1531 pjsip_endpt_handle_events2(app.sip_endpt, &timeout, &count);
1532 if (count == 0)
1533 break;
1534 }
1535
1536 pj_gettimeofday(&now);
1537 }
1538
Benny Prijono49f682a2006-07-11 12:25:45 +00001539 /* Wait couple of seconds to let jobs completes (e.g. ACKs to be sent) */
Benny Prijono775a1b22006-07-11 09:53:27 +00001540 pj_gettimeofday(&now);
1541 end_time = now;
1542 end_time.sec += 2;
Benny Prijono49f682a2006-07-11 12:25:45 +00001543 while (PJ_TIME_VAL_LT(now, end_time))
Benny Prijono775a1b22006-07-11 09:53:27 +00001544 {
1545 pj_time_val timeout = { 0, 1 };
1546 unsigned i;
1547
1548 for (i=0; i<1000; ++i) {
1549 unsigned count;
1550 count = 0;
1551 pjsip_endpt_handle_events2(app.sip_endpt, &timeout, &count);
1552 if (count == 0)
1553 break;
Benny Prijono514ca6b2006-07-03 01:30:01 +00001554 }
1555
1556 pj_gettimeofday(&now);
Benny Prijonoc3573762006-07-10 21:39:24 +00001557 }
Benny Prijono514ca6b2006-07-03 01:30:01 +00001558
1559 return 0;
1560}
1561
1562
1563static const char *good_number(char *buf, pj_int32_t val)
1564{
1565 if (val < 1000) {
1566 pj_ansi_sprintf(buf, "%d", val);
1567 } else if (val < 1000000) {
1568 pj_ansi_sprintf(buf, "%d.%dK",
1569 val / 1000,
1570 (val % 1000) / 100);
1571 } else {
1572 pj_ansi_sprintf(buf, "%d.%02dM",
1573 val / 1000000,
1574 (val % 1000000) / 10000);
1575 }
1576
1577 return buf;
1578}
1579
1580
1581static int server_thread(void *arg)
1582{
Benny Prijonoc3573762006-07-10 21:39:24 +00001583 pj_time_val timeout = { 0, 1 };
Benny Prijono7db431e2006-07-23 14:38:49 +00001584 unsigned thread_index = (unsigned)(long)arg;
Benny Prijono514ca6b2006-07-03 01:30:01 +00001585 pj_time_val last_report, next_report;
1586
1587 pj_gettimeofday(&last_report);
1588 next_report = last_report;
1589 next_report.sec++;
1590
1591 while (!app.thread_quit) {
1592 pj_time_val now;
1593 unsigned i;
1594
1595 for (i=0; i<100; ++i) {
1596 unsigned count = 0;
1597 pjsip_endpt_handle_events2(app.sip_endpt, &timeout, &count);
1598 if (count == 0)
1599 break;
1600 }
1601
1602 if (thread_index == 0) {
1603 pj_gettimeofday(&now);
1604
1605 if (PJ_TIME_VAL_GTE(now, next_report)) {
1606 pj_time_val tmp;
1607 unsigned msec;
1608 unsigned stateless, stateful, call;
1609 char str_stateless[32], str_stateful[32], str_call[32];
1610
1611 tmp = now;
1612 PJ_TIME_VAL_SUB(tmp, last_report);
1613 msec = PJ_TIME_VAL_MSEC(tmp);
1614
1615 last_report = now;
1616 next_report = last_report;
1617 next_report.sec++;
1618
1619 stateless = app.server.cur_state.stateless_cnt - app.server.prev_state.stateless_cnt;
1620 stateful = app.server.cur_state.stateful_cnt - app.server.prev_state.stateful_cnt;
1621 call = app.server.cur_state.call_cnt - app.server.prev_state.call_cnt;
1622
1623 good_number(str_stateless, app.server.cur_state.stateless_cnt);
1624 good_number(str_stateful, app.server.cur_state.stateful_cnt);
1625 good_number(str_call, app.server.cur_state.call_cnt);
1626
Benny Prijono1479b652006-07-03 14:18:17 +00001627 printf("Total(rate): stateless:%s (%d/s), statefull:%s (%d/s), call:%s (%d/s) \r",
Benny Prijono514ca6b2006-07-03 01:30:01 +00001628 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
Benny Prijono1479b652006-07-03 14:18:17 +00001641static void write_report(const char *msg)
1642{
1643 puts(msg);
1644
1645#if defined(PJ_WIN32) && PJ_WIN32!=0
1646 OutputDebugString(msg);
1647 OutputDebugString("\n");
1648#endif
1649}
1650
1651
Benny Prijono514ca6b2006-07-03 01:30:01 +00001652int main(int argc, char *argv[])
1653{
Benny Prijono1479b652006-07-03 14:18:17 +00001654 static char report[1024];
Benny Prijono514ca6b2006-07-03 01:30:01 +00001655
Benny Prijonodcc0cbf2006-07-16 10:40:37 +00001656 printf("PJSIP Performance Measurement Tool v%s\n"
1657 "(c)2006 pjsip.org\n\n",
1658 PJ_VERSION);
Benny Prijono775a1b22006-07-11 09:53:27 +00001659
Benny Prijono514ca6b2006-07-03 01:30:01 +00001660 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
Benny Prijonoc3573762006-07-10 21:39:24 +00001672 pj_log_set_level(app.log_level);
1673
Benny Prijonodcc0cbf2006-07-16 10:40:37 +00001674 if (app.log_level > 4) {
Benny Prijono514ca6b2006-07-03 01:30:01 +00001675 pjsip_endpt_register_module(app.sip_endpt, &msg_logger);
1676 }
1677
1678
1679 /* Misc infos */
1680 if (app.client.dst_uri.slen != 0) {
Benny Prijonoc3573762006-07-10 21:39:24 +00001681 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 }
Benny Prijono514ca6b2006-07-03 01:30:01 +00001687 }
Benny Prijonoc3573762006-07-10 21:39:24 +00001688
Benny Prijono514ca6b2006-07-03 01:30:01 +00001689 }
1690
Benny Prijono514ca6b2006-07-03 01:30:01 +00001691
1692
1693 if (app.client.dst_uri.slen) {
1694 /* Client mode */
1695 pj_status_t status;
1696 char test_type[64];
Benny Prijonoc3573762006-07-10 21:39:24 +00001697 unsigned msec_req, msec_res;
Benny Prijono514ca6b2006-07-03 01:30:01 +00001698 unsigned i;
1699
1700 /* Get the job name */
1701 if (app.client.method.id == PJSIP_INVITE_METHOD) {
1702 pj_ansi_strcpy(test_type, "INVITE calls");
1703 } else if (app.client.stateless) {
1704 pj_ansi_sprintf(test_type, "stateless %.*s requests",
1705 (int)app.client.method.name.slen,
1706 app.client.method.name.ptr);
1707 } else {
1708 pj_ansi_sprintf(test_type, "stateful %.*s requests",
1709 (int)app.client.method.name.slen,
1710 app.client.method.name.ptr);
1711 }
1712
1713
Benny Prijono775a1b22006-07-11 09:53:27 +00001714 printf("Sending %d %s to '%.*s' with %d maximum outstanding jobs, please wait..\n",
Benny Prijonoc03a3c52006-07-03 02:31:10 +00001715 app.client.job_count, test_type,
Benny Prijonoc3573762006-07-10 21:39:24 +00001716 (int)app.client.dst_uri.slen, app.client.dst_uri.ptr,
Benny Prijono775a1b22006-07-11 09:53:27 +00001717 app.client.job_window);
Benny Prijono514ca6b2006-07-03 01:30:01 +00001718
1719 for (i=0; i<app.thread_count; ++i) {
Benny Prijono7db431e2006-07-23 14:38:49 +00001720 status = pj_thread_create(app.pool, NULL, &client_thread,
1721 (void*)(long)i, 0, 0, &app.thread[i]);
Benny Prijono514ca6b2006-07-03 01:30:01 +00001722 if (status != PJ_SUCCESS) {
1723 app_perror(THIS_FILE, "Unable to create thread", status);
1724 return 1;
1725 }
1726 }
1727
1728 for (i=0; i<app.thread_count; ++i) {
1729 pj_thread_join(app.thread[i]);
1730 app.thread[i] = NULL;
1731 }
1732
1733 if (app.client.last_completion.sec) {
1734 pj_time_val duration;
1735 duration = app.client.last_completion;
1736 PJ_TIME_VAL_SUB(duration, app.client.first_request);
Benny Prijonoc3573762006-07-10 21:39:24 +00001737 msec_res = PJ_TIME_VAL_MSEC(duration);
Benny Prijono514ca6b2006-07-03 01:30:01 +00001738 } else {
Benny Prijonoc3573762006-07-10 21:39:24 +00001739 msec_res = app.client.timeout * 1000;
Benny Prijono514ca6b2006-07-03 01:30:01 +00001740 }
1741
Benny Prijonoc3573762006-07-10 21:39:24 +00001742 if (msec_res == 0) msec_res = 1;
1743
1744 if (app.client.requests_sent.sec) {
1745 pj_time_val duration;
1746 duration = app.client.requests_sent;
1747 PJ_TIME_VAL_SUB(duration, app.client.first_request);
1748 msec_req = PJ_TIME_VAL_MSEC(duration);
1749 } else {
1750 msec_req = app.client.timeout * 1000;
1751 }
1752
1753 if (msec_req == 0) msec_req = 1;
1754
Benny Prijono775a1b22006-07-11 09:53:27 +00001755 if (app.client.job_submitted < app.client.job_count)
1756 puts("\ntimed-out!\n");
1757 else
1758 puts("\ndone.\n");
Benny Prijono514ca6b2006-07-03 01:30:01 +00001759
Benny Prijono1479b652006-07-03 14:18:17 +00001760 pj_ansi_snprintf(
1761 report, sizeof(report),
Benny Prijono775a1b22006-07-11 09:53:27 +00001762 "Total %d %s sent in %d ms at rate of %d/sec\n"
Benny Prijono49f682a2006-07-11 12:25:45 +00001763 "Total %d responses receieved in %d ms at rate of %d/sec:",
Benny Prijonoc3573762006-07-10 21:39:24 +00001764 app.client.job_submitted, test_type, msec_req,
1765 app.client.job_submitted * 1000 / msec_req,
1766 app.client.total_responses, msec_res,
Benny Prijono49f682a2006-07-11 12:25:45 +00001767 app.client.total_responses*1000/msec_res);
1768 write_report(report);
1769
1770 /* Print detailed response code received */
1771 pj_ansi_sprintf(report, "\nDetailed responses received:");
1772 write_report(report);
1773
1774 for (i=0; i<PJ_ARRAY_SIZE(app.client.response_codes); ++i) {
1775 const pj_str_t *reason;
1776
1777 if (app.client.response_codes[i] == 0)
1778 continue;
1779
1780 reason = pjsip_get_status_text(i);
1781 pj_ansi_snprintf( report, sizeof(report),
1782 " - %d responses: %7d (%.*s)",
1783 i, app.client.response_codes[i],
1784 (int)reason->slen, reason->ptr);
1785 write_report(report);
1786 }
1787
1788 /* Total responses and rate */
1789 pj_ansi_snprintf( report, sizeof(report),
1790 " ------\n"
1791 " TOTAL responses: %7d (rate=%d/sec)\n",
1792 app.client.total_responses,
1793 app.client.total_responses*1000/msec_res);
Benny Prijono1479b652006-07-03 14:18:17 +00001794
1795 write_report(report);
1796
1797 pj_ansi_sprintf(report, "Maximum outstanding job: %d",
1798 app.client.stat_max_window);
1799 write_report(report);
1800
Benny Prijono514ca6b2006-07-03 01:30:01 +00001801
1802 } else {
1803 /* Server mode */
1804 char s[10];
1805 pj_status_t status;
1806 unsigned i;
1807
Benny Prijono514ca6b2006-07-03 01:30:01 +00001808 puts("pjsip-perf started in server-mode");
1809
1810 printf("Receiving requests on the following URIs:\n"
Benny Prijono49f682a2006-07-11 12:25:45 +00001811 " sip:0@%.*s:%d%s for stateless handling\n"
1812 " sip:1@%.*s:%d%s for stateful handling\n"
1813 " sip:2@%.*s:%d%s for call handling\n",
Benny Prijono514ca6b2006-07-03 01:30:01 +00001814 (int)app.local_addr.slen,
1815 app.local_addr.ptr,
1816 app.local_port,
Benny Prijono49f682a2006-07-11 12:25:45 +00001817 (app.use_tcp ? ";transport=tcp" : ""),
Benny Prijono514ca6b2006-07-03 01:30:01 +00001818 (int)app.local_addr.slen,
1819 app.local_addr.ptr,
1820 app.local_port,
Benny Prijono49f682a2006-07-11 12:25:45 +00001821 (app.use_tcp ? ";transport=tcp" : ""),
Benny Prijono514ca6b2006-07-03 01:30:01 +00001822 (int)app.local_addr.slen,
1823 app.local_addr.ptr,
Benny Prijonoc03a3c52006-07-03 02:31:10 +00001824 app.local_port,
Benny Prijono49f682a2006-07-11 12:25:45 +00001825 (app.use_tcp ? ";transport=tcp" : ""));
Benny Prijonodcc0cbf2006-07-16 10:40:37 +00001826 printf("INVITE with non-matching user part will be handled call-statefully\n");
Benny Prijono514ca6b2006-07-03 01:30:01 +00001827
1828 for (i=0; i<app.thread_count; ++i) {
Benny Prijono7db431e2006-07-23 14:38:49 +00001829 status = pj_thread_create(app.pool, NULL, &server_thread,
1830 (void*)(long)i, 0, 0, &app.thread[i]);
Benny Prijono514ca6b2006-07-03 01:30:01 +00001831 if (status != PJ_SUCCESS) {
1832 app_perror(THIS_FILE, "Unable to create thread", status);
1833 return 1;
1834 }
1835 }
1836
Benny Prijono49f682a2006-07-11 12:25:45 +00001837 puts("\nPress <ENTER> to quit\n");
Benny Prijono514ca6b2006-07-03 01:30:01 +00001838 fflush(stdout);
1839 fgets(s, sizeof(s), stdin);
1840
1841 app.thread_quit = PJ_TRUE;
1842 for (i=0; i<app.thread_count; ++i) {
1843 pj_thread_join(app.thread[i]);
1844 app.thread[i] = NULL;
1845 }
Benny Prijono49f682a2006-07-11 12:25:45 +00001846
1847 puts("");
Benny Prijono514ca6b2006-07-03 01:30:01 +00001848 }
1849
1850
1851 destroy_app();
1852
1853 return 0;
1854}
1855