1
/* $Id: sock_perf.c 4537 2013-06-19 06:47:43Z riza $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
22
#include <pj/compat/high_precision.h>
26
* \page page_pjlib_sock_perf_test Test: Socket Performance
28
* Test the performance of the socket communication. This will perform
29
* simple producer-consumer type of test, where we calculate how long
30
* does it take to send certain number of packets from producer to
33
* This file is <b>pjlib-test/sock_perf.c</b>
35
* \include pjlib-test/sock_perf.c
38
#if INCLUDE_SOCK_PERF_TEST
41
* sock_producer_consumer()
43
* Simple producer-consumer benchmarking. Send loop number of
44
* buf_size size packets as fast as possible.
46
static int sock_producer_consumer(int sock_type,
49
unsigned *p_bandwidth)
51
pj_sock_t consumer, producer;
53
char *outgoing_buffer, *incoming_buffer;
54
pj_timestamp start, stop;
56
pj_highprec_t elapsed, bandwidth;
57
pj_highprec_t total_received;
61
pool = pj_pool_create(mem, NULL, 4096, 4096, NULL);
65
/* Create producer-consumer pair. */
66
rc = app_socketpair(pj_AF_INET(), sock_type, 0, &consumer, &producer);
67
if (rc != PJ_SUCCESS) {
68
app_perror("...error: create socket pair", rc);
73
outgoing_buffer = (char*) pj_pool_alloc(pool, buf_size);
74
incoming_buffer = (char*) pj_pool_alloc(pool, buf_size);
77
pj_get_timestamp(&start);
79
for (i=0; i<loop; ++i) {
80
pj_ssize_t sent, part_received, received;
84
rc = pj_sock_send(producer, outgoing_buffer, &sent, 0);
85
if (rc != PJ_SUCCESS || sent != (pj_ssize_t)buf_size) {
86
app_perror("...error: send()", rc);
90
/* Repeat recv() until all data is part_received.
91
* This applies only for non-UDP of course, since for UDP
92
* we would expect all data to be part_received in one packet.
96
part_received = buf_size-received;
97
rc = pj_sock_recv(consumer, incoming_buffer+received,
99
if (rc != PJ_SUCCESS) {
100
app_perror("...recv error", rc);
103
if (part_received <= 0) {
104
PJ_LOG(3,("", "...error: socket has closed (part_received=%d)!",
108
if ((pj_size_t)part_received != buf_size-received) {
109
if (sock_type != pj_SOCK_STREAM()) {
110
PJ_LOG(3,("", "...error: expecting %u bytes, got %u bytes",
111
buf_size-received, part_received));
115
received += part_received;
116
} while ((pj_size_t)received < buf_size);
118
total_received += received;
120
/* Stop test if it's been runnign for more than 10 secs. */
121
pj_get_timestamp(&stop);
122
delay = pj_elapsed_time(&start, &stop);
128
pj_get_timestamp(&stop);
130
elapsed = pj_elapsed_usec(&start, &stop);
132
/* bandwidth = total_received * 1000 / elapsed */
133
bandwidth = total_received;
134
pj_highprec_mul(bandwidth, 1000);
135
pj_highprec_div(bandwidth, elapsed);
137
*p_bandwidth = (pj_uint32_t)bandwidth;
140
pj_sock_close(consumer);
141
pj_sock_close(producer);
144
pj_pool_release(pool);
154
int sock_perf_test(void)
156
enum { LOOP = 64 * 1024 };
160
PJ_LOG(3,("", "...benchmarking socket "
161
"(2 sockets, packet=512, single threaded):"));
163
/* Disable this test on Symbian since UDP connect()/send() failed
164
* with S60 3rd edition (including MR2).
165
* See http://www.pjsip.org/trac/ticket/264
167
#if !defined(PJ_SYMBIAN) || PJ_SYMBIAN==0
168
/* Benchmarking UDP */
169
rc = sock_producer_consumer(pj_SOCK_DGRAM(), 512, LOOP, &bandwidth);
170
if (rc != 0) return rc;
171
PJ_LOG(3,("", "....bandwidth UDP = %d KB/s", bandwidth));
174
/* Benchmarking TCP */
175
rc = sock_producer_consumer(pj_SOCK_STREAM(), 512, LOOP, &bandwidth);
176
if (rc != 0) return rc;
177
PJ_LOG(3,("", "....bandwidth TCP = %d KB/s", bandwidth));
184
/* To prevent warning about "translation unit is empty"
185
* when this test is disabled.
187
int dummy_sock_perf_test;
188
#endif /* INCLUDE_SOCK_PERF_TEST */