blob: f7465f198e8577cbfd20e1289eb6de0785982e1e [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Nanang Izzuddina62ffc92011-05-05 06:14:19 +00003 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
Benny Prijono844653c2008-12-23 17:27:53 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00005 *
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 <pjlib.h>
22#include <pj/compat/high_precision.h>
23
24
25/**
26 * \page page_pjlib_sock_perf_test Test: Socket Performance
27 *
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
31 * consumer.
32 *
33 * This file is <b>pjlib-test/sock_perf.c</b>
34 *
35 * \include pjlib-test/sock_perf.c
36 */
37
38#if INCLUDE_SOCK_PERF_TEST
39
40/*
41 * sock_producer_consumer()
42 *
43 * Simple producer-consumer benchmarking. Send loop number of
44 * buf_size size packets as fast as possible.
45 */
46static int sock_producer_consumer(int sock_type,
47 unsigned buf_size,
48 unsigned loop,
49 unsigned *p_bandwidth)
50{
51 pj_sock_t consumer, producer;
52 pj_pool_t *pool;
53 char *outgoing_buffer, *incoming_buffer;
54 pj_timestamp start, stop;
55 unsigned i;
56 pj_highprec_t elapsed, bandwidth;
57 pj_size_t total_received;
58 pj_status_t rc;
59
60 /* Create pool. */
61 pool = pj_pool_create(mem, NULL, 4096, 4096, NULL);
62 if (!pool)
63 return -10;
64
65 /* Create producer-consumer pair. */
Benny Prijono8ab968f2007-07-20 08:08:30 +000066 rc = app_socketpair(pj_AF_INET(), sock_type, 0, &consumer, &producer);
Benny Prijono5dcb38d2005-11-21 01:55:47 +000067 if (rc != PJ_SUCCESS) {
68 app_perror("...error: create socket pair", rc);
69 return -20;
70 }
71
72 /* Create buffers. */
Benny Prijonof260e462007-04-30 21:03:32 +000073 outgoing_buffer = (char*) pj_pool_alloc(pool, buf_size);
74 incoming_buffer = (char*) pj_pool_alloc(pool, buf_size);
Benny Prijono5dcb38d2005-11-21 01:55:47 +000075
76 /* Start loop. */
77 pj_get_timestamp(&start);
78 total_received = 0;
79 for (i=0; i<loop; ++i) {
80 pj_ssize_t sent, part_received, received;
81 pj_time_val delay;
82
83 sent = buf_size;
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);
87 return -61;
88 }
89
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.
93 */
94 received = 0;
95 do {
96 part_received = buf_size-received;
97 rc = pj_sock_recv(consumer, incoming_buffer+received,
98 &part_received, 0);
99 if (rc != PJ_SUCCESS) {
100 app_perror("...recv error", rc);
101 return -70;
102 }
103 if (part_received <= 0) {
104 PJ_LOG(3,("", "...error: socket has closed (part_received=%d)!",
105 part_received));
106 return -73;
107 }
108 if ((pj_size_t)part_received != buf_size-received) {
Benny Prijono8ab968f2007-07-20 08:08:30 +0000109 if (sock_type != pj_SOCK_STREAM()) {
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000110 PJ_LOG(3,("", "...error: expecting %u bytes, got %u bytes",
111 buf_size-received, part_received));
112 return -76;
113 }
114 }
115 received += part_received;
116 } while ((pj_size_t)received < buf_size);
117
118 total_received += received;
119
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);
123 if (delay.sec > 10)
124 break;
125 }
126
127 /* Stop timer. */
128 pj_get_timestamp(&stop);
129
130 elapsed = pj_elapsed_usec(&start, &stop);
131
132 /* bandwidth = total_received * 1000 / elapsed */
133 bandwidth = total_received;
134 pj_highprec_mul(bandwidth, 1000);
135 pj_highprec_div(bandwidth, elapsed);
136
137 *p_bandwidth = (pj_uint32_t)bandwidth;
138
139 /* Close sockets. */
140 pj_sock_close(consumer);
141 pj_sock_close(producer);
142
143 /* Done */
144 pj_pool_release(pool);
145
146 return 0;
147}
148
149/*
150 * sock_perf_test()
151 *
152 * Main test entry.
153 */
154int sock_perf_test(void)
155{
156 enum { LOOP = 64 * 1024 };
157 int rc;
158 unsigned bandwidth;
159
160 PJ_LOG(3,("", "...benchmarking socket "
161 "(2 sockets, packet=512, single threaded):"));
Benny Prijonoaeeb1d12007-05-01 10:42:22 +0000162
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
166 */
167#if !defined(PJ_SYMBIAN) || PJ_SYMBIAN==0
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000168 /* Benchmarking UDP */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000169 rc = sock_producer_consumer(pj_SOCK_DGRAM(), 512, LOOP, &bandwidth);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000170 if (rc != 0) return rc;
171 PJ_LOG(3,("", "....bandwidth UDP = %d KB/s", bandwidth));
Benny Prijonoaeeb1d12007-05-01 10:42:22 +0000172#endif
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000173
174 /* Benchmarking TCP */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000175 rc = sock_producer_consumer(pj_SOCK_STREAM(), 512, LOOP, &bandwidth);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000176 if (rc != 0) return rc;
177 PJ_LOG(3,("", "....bandwidth TCP = %d KB/s", bandwidth));
178
179 return rc;
180}
181
182
183#else
184/* To prevent warning about "translation unit is empty"
185 * when this test is disabled.
186 */
187int dummy_sock_perf_test;
188#endif /* INCLUDE_SOCK_PERF_TEST */
189
190