~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.1.0/pjsip/src/test/tsx_bench.c

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-01-07 14:51:16 UTC
  • mfrom: (4.3.5 sid)
  • Revision ID: package-import@ubuntu.com-20150107145116-yxnafinf4lrdvrmx
Tags: 1.4.1-0.1ubuntu1
* Merge with Debian, remaining changes:
 - Drop soprano, nepomuk build-dep
* Drop ubuntu patches, now upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: tsx_bench.c 3553 2011-05-05 06:14:19Z nanang $ */
2
 
/* 
3
 
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
 
 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
9
 
 * (at your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 * GNU General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
19
 
 */
20
 
#include "test.h"
21
 
#include <pjsip.h>
22
 
#include <pjlib.h>
23
 
 
24
 
#define THIS_FILE   "tsx_uas_test.c"
25
 
 
26
 
 
27
 
static pjsip_module mod_tsx_user;
28
 
 
29
 
static int uac_tsx_bench(unsigned working_set, pj_timestamp *p_elapsed)
30
 
{
31
 
    unsigned i;
32
 
    pjsip_tx_data *request;
33
 
    pjsip_transaction **tsx;
34
 
    pj_timestamp t1, t2, elapsed;
35
 
    pjsip_via_hdr *via;
36
 
    pj_status_t status;
37
 
 
38
 
    /* Create the request first. */
39
 
    pj_str_t str_target = pj_str("sip:someuser@someprovider.com");
40
 
    pj_str_t str_from = pj_str("\"Local User\" <sip:localuser@serviceprovider.com>");
41
 
    pj_str_t str_to = pj_str("\"Remote User\" <sip:remoteuser@serviceprovider.com>");
42
 
    pj_str_t str_contact = str_from;
43
 
 
44
 
    status = pjsip_endpt_create_request(endpt, &pjsip_invite_method,
45
 
                                        &str_target, &str_from, &str_to,
46
 
                                        &str_contact, NULL, -1, NULL,
47
 
                                        &request);
48
 
    if (status != PJ_SUCCESS) {
49
 
        app_perror("    error: unable to create request", status);
50
 
        return status;
51
 
    }
52
 
 
53
 
    via = (pjsip_via_hdr*) pjsip_msg_find_hdr(request->msg, PJSIP_H_VIA,
54
 
                                              NULL);
55
 
 
56
 
    /* Create transaction array */
57
 
    tsx = (pjsip_transaction**) pj_pool_zalloc(request->pool, working_set * sizeof(pj_pool_t*));
58
 
 
59
 
    pj_bzero(&mod_tsx_user, sizeof(mod_tsx_user));
60
 
    mod_tsx_user.id = -1;
61
 
 
62
 
    /* Benchmark */
63
 
    elapsed.u64 = 0;
64
 
    pj_get_timestamp(&t1);
65
 
    for (i=0; i<working_set; ++i) {
66
 
        status = pjsip_tsx_create_uac(&mod_tsx_user, request, &tsx[i]);
67
 
        if (status != PJ_SUCCESS)
68
 
            goto on_error;
69
 
        /* Reset branch param */
70
 
        via->branch_param.slen = 0;
71
 
    }
72
 
    pj_get_timestamp(&t2);
73
 
    pj_sub_timestamp(&t2, &t1);
74
 
    pj_add_timestamp(&elapsed, &t2);
75
 
 
76
 
    p_elapsed->u64 = elapsed.u64;
77
 
    status = PJ_SUCCESS;
78
 
    
79
 
on_error:
80
 
    for (i=0; i<working_set; ++i) {
81
 
        if (tsx[i]) {
82
 
            pjsip_tsx_terminate(tsx[i], 601);
83
 
            tsx[i] = NULL;
84
 
        }
85
 
    }
86
 
    pjsip_tx_data_dec_ref(request);
87
 
    flush_events(2000);
88
 
    return status;
89
 
}
90
 
 
91
 
 
92
 
 
93
 
static int uas_tsx_bench(unsigned working_set, pj_timestamp *p_elapsed)
94
 
{
95
 
    unsigned i;
96
 
    pjsip_tx_data *request;
97
 
    pjsip_via_hdr *via;
98
 
    pjsip_rx_data rdata;
99
 
    pj_sockaddr_in remote;
100
 
    pjsip_transaction **tsx;
101
 
    pj_timestamp t1, t2, elapsed;
102
 
    char branch_buf[80] = PJSIP_RFC3261_BRANCH_ID "0000000000";
103
 
    pj_status_t status;
104
 
 
105
 
    /* Create the request first. */
106
 
    pj_str_t str_target = pj_str("sip:someuser@someprovider.com");
107
 
    pj_str_t str_from = pj_str("\"Local User\" <sip:localuser@serviceprovider.com>");
108
 
    pj_str_t str_to = pj_str("\"Remote User\" <sip:remoteuser@serviceprovider.com>");
109
 
    pj_str_t str_contact = str_from;
110
 
 
111
 
    status = pjsip_endpt_create_request(endpt, &pjsip_invite_method,
112
 
                                        &str_target, &str_from, &str_to,
113
 
                                        &str_contact, NULL, -1, NULL,
114
 
                                        &request);
115
 
    if (status != PJ_SUCCESS) {
116
 
        app_perror("    error: unable to create request", status);
117
 
        return status;
118
 
    }
119
 
 
120
 
    /* Create  Via */
121
 
    via = pjsip_via_hdr_create(request->pool);
122
 
    via->sent_by.host = pj_str("192.168.0.7");
123
 
    via->sent_by.port = 5061;
124
 
    via->transport = pj_str("udp");
125
 
    via->rport_param = 1;
126
 
    via->recvd_param = pj_str("192.168.0.7");
127
 
    pjsip_msg_insert_first_hdr(request->msg, (pjsip_hdr*)via);
128
 
    
129
 
 
130
 
    /* Create "dummy" rdata from the tdata */
131
 
    pj_bzero(&rdata, sizeof(pjsip_rx_data));
132
 
    rdata.tp_info.pool = request->pool;
133
 
    rdata.msg_info.msg = request->msg;
134
 
    rdata.msg_info.from = (pjsip_from_hdr*) pjsip_msg_find_hdr(request->msg, PJSIP_H_FROM, NULL);
135
 
    rdata.msg_info.to = (pjsip_to_hdr*) pjsip_msg_find_hdr(request->msg, PJSIP_H_TO, NULL);
136
 
    rdata.msg_info.cseq = (pjsip_cseq_hdr*) pjsip_msg_find_hdr(request->msg, PJSIP_H_CSEQ, NULL);
137
 
    rdata.msg_info.cid = (pjsip_cid_hdr*) pjsip_msg_find_hdr(request->msg, PJSIP_H_FROM, NULL);
138
 
    rdata.msg_info.via = via;
139
 
    
140
 
    pj_sockaddr_in_init(&remote, 0, 0);
141
 
    status = pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_LOOP_DGRAM, 
142
 
                                           &remote, sizeof(pj_sockaddr_in),
143
 
                                           NULL, &rdata.tp_info.transport);
144
 
    if (status != PJ_SUCCESS) {
145
 
        app_perror("    error: unable to get loop transport", status);
146
 
        return status;
147
 
    }
148
 
 
149
 
 
150
 
    /* Create transaction array */
151
 
    tsx = (pjsip_transaction**) pj_pool_zalloc(request->pool, working_set * sizeof(pj_pool_t*));
152
 
 
153
 
    pj_bzero(&mod_tsx_user, sizeof(mod_tsx_user));
154
 
    mod_tsx_user.id = -1;
155
 
 
156
 
 
157
 
    /* Benchmark */
158
 
    elapsed.u64 = 0;
159
 
    pj_get_timestamp(&t1);
160
 
    for (i=0; i<working_set; ++i) {
161
 
        via->branch_param.ptr = branch_buf;
162
 
        via->branch_param.slen = PJSIP_RFC3261_BRANCH_LEN + 
163
 
                                    pj_ansi_sprintf(branch_buf+PJSIP_RFC3261_BRANCH_LEN,
164
 
                                                    "-%d", i);
165
 
        status = pjsip_tsx_create_uas(&mod_tsx_user, &rdata, &tsx[i]);
166
 
        if (status != PJ_SUCCESS)
167
 
            goto on_error;
168
 
 
169
 
    }
170
 
    pj_get_timestamp(&t2);
171
 
    pj_sub_timestamp(&t2, &t1);
172
 
    pj_add_timestamp(&elapsed, &t2);
173
 
 
174
 
    p_elapsed->u64 = elapsed.u64;
175
 
    status = PJ_SUCCESS;
176
 
    
177
 
on_error:
178
 
    for (i=0; i<working_set; ++i) {
179
 
        if (tsx[i]) {
180
 
            pjsip_tsx_terminate(tsx[i], 601);
181
 
            tsx[i] = NULL;
182
 
        }
183
 
    }
184
 
    pjsip_tx_data_dec_ref(request);
185
 
    flush_events(2000);
186
 
    return status;
187
 
}
188
 
 
189
 
 
190
 
 
191
 
int tsx_bench(void)
192
 
{
193
 
    enum { WORKING_SET=10000, REPEAT = 4 };
194
 
    unsigned i, speed;
195
 
    pj_timestamp usec[REPEAT], min, freq;
196
 
    char desc[250];
197
 
    int status;
198
 
 
199
 
    status = pj_get_timestamp_freq(&freq);
200
 
    if (status != PJ_SUCCESS)
201
 
        return status;
202
 
 
203
 
 
204
 
    /*
205
 
     * Benchmark UAC
206
 
     */
207
 
    PJ_LOG(3,(THIS_FILE, "   benchmarking UAC transaction creation:"));
208
 
    for (i=0; i<REPEAT; ++i) {
209
 
        PJ_LOG(3,(THIS_FILE, "    test %d of %d..",
210
 
                  i+1, REPEAT));
211
 
        status = uac_tsx_bench(WORKING_SET, &usec[i]);
212
 
        if (status != PJ_SUCCESS)
213
 
            return status;
214
 
    }
215
 
 
216
 
    min.u64 = PJ_UINT64(0xFFFFFFFFFFFFFFF);
217
 
    for (i=0; i<REPEAT; ++i) {
218
 
        if (usec[i].u64 < min.u64) min.u64 = usec[i].u64;
219
 
    }
220
 
 
221
 
    
222
 
    /* Report time */
223
 
    pj_ansi_sprintf(desc, "Time to create %d UAC transactions, in miliseconds",
224
 
                          WORKING_SET);
225
 
    report_ival("create-uac-time", (unsigned)(min.u64 * 1000 / freq.u64), "msec", desc);
226
 
 
227
 
 
228
 
    /* Write speed */
229
 
    speed = (unsigned)(freq.u64 * WORKING_SET / min.u64);
230
 
    PJ_LOG(3,(THIS_FILE, "    UAC created at %d tsx/sec", speed));
231
 
 
232
 
    pj_ansi_sprintf(desc, "Number of UAC transactions that potentially can be created per second "
233
 
                          "with <tt>pjsip_tsx_create_uac()</tt>, based on the time "
234
 
                          "to create %d simultaneous transactions above.",
235
 
                          WORKING_SET);
236
 
 
237
 
    report_ival("create-uac-tsx-per-sec", 
238
 
                speed, "tsx/sec", desc);
239
 
 
240
 
 
241
 
 
242
 
    /*
243
 
     * Benchmark UAS
244
 
     */
245
 
    PJ_LOG(3,(THIS_FILE, "   benchmarking UAS transaction creation:"));
246
 
    for (i=0; i<REPEAT; ++i) {
247
 
        PJ_LOG(3,(THIS_FILE, "    test %d of %d..",
248
 
                  i+1, REPEAT));
249
 
        status = uas_tsx_bench(WORKING_SET, &usec[i]);
250
 
        if (status != PJ_SUCCESS)
251
 
            return status;
252
 
    }
253
 
 
254
 
    min.u64 = PJ_UINT64(0xFFFFFFFFFFFFFFF);
255
 
    for (i=0; i<REPEAT; ++i) {
256
 
        if (usec[i].u64 < min.u64) min.u64 = usec[i].u64;
257
 
    }
258
 
 
259
 
    
260
 
    /* Report time */
261
 
    pj_ansi_sprintf(desc, "Time to create %d UAS transactions, in miliseconds",
262
 
                          WORKING_SET);
263
 
    report_ival("create-uas-time", (unsigned)(min.u64 * 1000 / freq.u64), "msec", desc);
264
 
 
265
 
 
266
 
    /* Write speed */
267
 
    speed = (unsigned)(freq.u64 * WORKING_SET / min.u64);
268
 
    PJ_LOG(3,(THIS_FILE, "    UAS created at %d tsx/sec", speed));
269
 
 
270
 
    pj_ansi_sprintf(desc, "Number of UAS transactions that potentially can be created per second "
271
 
                          "with <tt>pjsip_tsx_create_uas()</tt>, based on the time "
272
 
                          "to create %d simultaneous transactions above.",
273
 
                          WORKING_SET);
274
 
 
275
 
    report_ival("create-uas-tsx-per-sec", 
276
 
                speed, "tsx/sec", desc);
277
 
 
278
 
    return PJ_SUCCESS;
279
 
}
280