Debian Codegraph @

Xpjproject/pjsip/src/test/tsx_bench.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 #include "test.h"
20 #include <pjsip.h>
21 #include <pjlib.h>
22
23 #define THIS_FILE "tsx_bench.c"
24
25
26 static pjsip_module mod_tsx_user;
27
28 static int uac_tsx_bench(unsigned working_set, pj_timestamp *p_elapsed)
29 {
30 unsigned i;
31 pjsip_tx_data *request;
32 pjsip_transaction **tsx;
33 pj_timestamp t1, t2, elapsed;
34 pjsip_via_hdr *via;
35 int rc;
36
37 /* Create the request first. */
38 pj_str_t str_target = pj_str("sip:[email protected]");
39 pj_str_t str_from = pj_str("\"Local User\" <sip:[email protected]>");
40 pj_str_t str_to = pj_str("\"Remote User\" <sip:[email protected]>");
41 pj_str_t str_contact = str_from;
42
43 PJ_TEST_SUCCESS(pjsip_endpt_create_request(endpt, &pjsip_invite_method,
44 &str_target, &str_from, &str_to,
45 &str_contact, NULL, -1, NULL,
46 &request),
47 NULL, return -110);
48
49 via = (pjsip_via_hdr*) pjsip_msg_find_hdr(request->msg, PJSIP_H_VIA,
50 NULL);
51
52 /* Create transaction array */
53 tsx = (pjsip_transaction**) pj_pool_zalloc(request->pool, working_set * sizeof(pjsip_transaction*));
54
55 pj_bzero(&mod_tsx_user, sizeof(mod_tsx_user));
56 mod_tsx_user.id = -1;
57
58 /* Benchmark */
59 elapsed.u64 = 0;
60 pj_get_timestamp(&t1);
61 for (i=0; i<working_set; ++i) {
62 PJ_TEST_SUCCESS(pjsip_tsx_create_uac(&mod_tsx_user, request, &tsx[i]),
63 NULL, {rc=-120; goto on_error;});
64
65 /* Reset branch param */
66 via->branch_param.slen = 0;
67 }
68 pj_get_timestamp(&t2);
69 pj_sub_timestamp(&t2, &t1);
70 pj_add_timestamp(&elapsed, &t2);
71
72 p_elapsed->u64 = elapsed.u64;
73 rc = 0;
74
75 on_error:
76 for (i=0; i<working_set; ++i) {
77 if (tsx[i]) {
78 pj_timer_heap_t *th;
79
80 pjsip_tsx_terminate(tsx[i], 601);
81 tsx[i] = NULL;
82
83 th = pjsip_endpt_get_timer_heap(endpt);
84 pj_timer_heap_poll(th, NULL);
85 }
86 }
87 pjsip_tx_data_dec_ref(request);
88 flush_events(2000);
89 return rc;
90 }
91
92
93
94 static int uas_tsx_bench(unsigned working_set, pj_timestamp *p_elapsed)
95 {
96 unsigned i;
97 pjsip_transport *loop = NULL;
98 pjsip_tx_data *request = NULL;
99 pjsip_via_hdr *via;
100 pjsip_rx_data rdata;
101 pjsip_transaction **tsx;
102 pj_timestamp t1, t2, elapsed;
103 char branch_buf[80] = PJSIP_RFC3261_BRANCH_ID "0000000000";
104 int rc;
105
106 /* Create the request first. */
107 pj_str_t str_target = pj_str("sip:[email protected]");
108 pj_str_t str_from = pj_str("\"Local User\" <sip:[email protected]>");
109 pj_str_t str_to = pj_str("\"Remote User\" <sip:[email protected]>");
110 pj_str_t str_contact = str_from;
111
112 PJ_TEST_SUCCESS( pjsip_endpt_create_request(endpt, &pjsip_invite_method,
113 &str_target, &str_from, &str_to,
114 &str_contact, NULL, -1, NULL,
115 &request),
116 NULL, return -210);
117
118 /* Create Via */
119 via = pjsip_via_hdr_create(request->pool);
120 via->sent_by.host = pj_str("192.168.0.7");
121 via->sent_by.port = 5061;
122 via->transport = pj_str("udp");
123 via->rport_param = 1;
124 via->recvd_param = pj_str("192.168.0.7");
125 pjsip_msg_insert_first_hdr(request->msg, (pjsip_hdr*)via);
126
127
128 /* Create "dummy" rdata from the tdata */
129 pj_bzero(&rdata, sizeof(pjsip_rx_data));
130 rdata.tp_info.pool = request->pool;
131 rdata.msg_info.msg = request->msg;
132 rdata.msg_info.from = (pjsip_from_hdr*) pjsip_msg_find_hdr(request->msg, PJSIP_H_FROM, NULL);
133 rdata.msg_info.to = (pjsip_to_hdr*) pjsip_msg_find_hdr(request->msg, PJSIP_H_TO, NULL);
134 rdata.msg_info.cseq = (pjsip_cseq_hdr*) pjsip_msg_find_hdr(request->msg, PJSIP_H_CSEQ, NULL);
135 rdata.msg_info.cid = (pjsip_cid_hdr*) pjsip_msg_find_hdr(request->msg, PJSIP_H_CALL_ID, NULL);
136 rdata.msg_info.via = via;
137
138 PJ_TEST_SUCCESS(pjsip_loop_start(endpt, &loop), NULL,
139 { pjsip_tx_data_dec_ref(request); return -220; });
140 pjsip_transport_add_ref(loop);
141 rdata.tp_info.transport = loop;
142
143 /* Create transaction array */
144 tsx = (pjsip_transaction**) pj_pool_zalloc(request->pool, working_set * sizeof(pjsip_transaction*));
145
146 pj_bzero(&mod_tsx_user, sizeof(mod_tsx_user));
147 mod_tsx_user.id = -1;
148
149
150 /* Benchmark */
151 elapsed.u64 = 0;
152 pj_get_timestamp(&t1);
153 for (i=0; i<working_set; ++i) {
154 via->branch_param.ptr = branch_buf;
155 via->branch_param.slen = PJSIP_RFC3261_BRANCH_LEN +
156 pj_ansi_snprintf(branch_buf+PJSIP_RFC3261_BRANCH_LEN,
157 sizeof(branch_buf)-PJSIP_RFC3261_BRANCH_LEN,
158 "-%d", i);
159 PJ_TEST_SUCCESS(pjsip_tsx_create_uas(&mod_tsx_user, &rdata, &tsx[i]),
160 NULL, { rc=-230; goto on_error; });
161 }
162 pj_get_timestamp(&t2);
163 pj_sub_timestamp(&t2, &t1);
164 pj_add_timestamp(&elapsed, &t2);
165
166 p_elapsed->u64 = elapsed.u64;
167 rc = 0;
168
169 on_error:
170 for (i=0; i<working_set; ++i) {
171 if (tsx[i]) {
172 pj_timer_heap_t *th;
173
174 pjsip_tsx_terminate(tsx[i], 601);
175 tsx[i] = NULL;
176
177 th = pjsip_endpt_get_timer_heap(endpt);
178 pj_timer_heap_poll(th, NULL);
179
180 }
181 }
182 pjsip_tx_data_dec_ref(request);
183 if (loop) {
184 /* Order must be shutdown then dec_ref so it gets destroyed */
185 pjsip_transport_shutdown(loop);
186 pjsip_transport_dec_ref(loop);
187 }
188 flush_events(2000);
189 return rc;
190 }
191
192
193
194 int tsx_bench(void)
195 {
196 enum { WORKING_SET=10000, REPEAT = 4 };
197 unsigned i, speed;
198 pj_timestamp usec[REPEAT], min, freq;
199 char desc[250];
200 int status;
201
202 status = pj_get_timestamp_freq(&freq);
203 if (status != PJ_SUCCESS)
204 return status;
205
206
207 /*
208 * Benchmark UAC
209 */
210 PJ_LOG(3,(THIS_FILE, " benchmarking UAC transaction creation:"));
211 for (i=0; i<REPEAT; ++i) {
212 PJ_LOG(3,(THIS_FILE, " test %d of %d..",
213 i+1, REPEAT));
214 PJ_LOG(3,(THIS_FILE, " number of current tsx: %d",
215 pjsip_tsx_layer_get_tsx_count()));
216 status = uac_tsx_bench(WORKING_SET, &usec[i]);
217 if (status != PJ_SUCCESS)
218 return status;
219 }
220
221 min.u64 = PJ_UINT64(0xFFFFFFFFFFFFFFF);
222 for (i=0; i<REPEAT; ++i) {
223 if (usec[i].u64 < min.u64) min.u64 = usec[i].u64;
224 }
225
226
227 /* Report time */
228 pj_ansi_snprintf(desc, sizeof(desc),
229 "Time to create %d UAC transactions, in miliseconds",
230 WORKING_SET);
231 report_ival("create-uac-time", (unsigned)(min.u64 * 1000 / freq.u64), "msec", desc);
232
233
234 /* Write speed */
235 speed = (unsigned)(freq.u64 * WORKING_SET / min.u64);
236 PJ_LOG(3,(THIS_FILE, " UAC created at %d tsx/sec", speed));
237
238 pj_ansi_snprintf(desc, sizeof(desc),
239 "Number of UAC transactions that potentially can be created per second "
240 "with <tt>pjsip_tsx_create_uac()</tt>, based on the time "
241 "to create %d simultaneous transactions above.",
242 WORKING_SET);
243
244 report_ival("create-uac-tsx-per-sec",
245 speed, "tsx/sec", desc);
246
247
248
249 /*
250 * Benchmark UAS
251 */
252 PJ_LOG(3,(THIS_FILE, " benchmarking UAS transaction creation:"));
253 for (i=0; i<REPEAT; ++i) {
254 PJ_LOG(3,(THIS_FILE, " test %d of %d..",
255 i+1, REPEAT));
256 PJ_LOG(3,(THIS_FILE, " number of current tsx: %d",
257 pjsip_tsx_layer_get_tsx_count()));
258 status = uas_tsx_bench(WORKING_SET, &usec[i]);
259 if (status != PJ_SUCCESS)
260 return status;
261 }
262
263 min.u64 = PJ_UINT64(0xFFFFFFFFFFFFFFF);
264 for (i=0; i<REPEAT; ++i) {
265 if (usec[i].u64 < min.u64) min.u64 = usec[i].u64;
266 }
267
268
269 /* Report time */
270 pj_ansi_snprintf(desc, sizeof(desc),
271 "Time to create %d UAS transactions, in miliseconds",
272 WORKING_SET);
273 report_ival("create-uas-time", (unsigned)(min.u64 * 1000 / freq.u64), "msec", desc);
274
275
276 /* Write speed */
277 speed = (unsigned)(freq.u64 * WORKING_SET / min.u64);
278 PJ_LOG(3,(THIS_FILE, " UAS created at %d tsx/sec", speed));
279
280 pj_ansi_snprintf(desc, sizeof(desc),
281 "Number of UAS transactions that potentially can be created per second "
282 "with <tt>pjsip_tsx_create_uas()</tt>, based on the time "
283 "to create %d simultaneous transactions above.",
284 WORKING_SET);
285
286 report_ival("create-uas-tsx-per-sec",
287 speed, "tsx/sec", desc);
288
289 return PJ_SUCCESS;
290 }
291