blob: 0cca64e7689ecaa44638d7b657af7b9278b09bd9 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
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 <pjlib.h>
21
22static pj_atomic_t *total_bytes;
23
24static int worker_thread(void *arg)
25{
26 pj_sock_t sock = (pj_sock_t)arg;
27 char buf[512];
28 pj_status_t last_recv_err = PJ_SUCCESS, last_write_err = PJ_SUCCESS;
29
30 for (;;) {
31 pj_ssize_t len;
32 pj_status_t rc;
33 pj_sockaddr_in addr;
34 int addrlen;
35
36 len = sizeof(buf);
37 addrlen = sizeof(addr);
38 rc = pj_sock_recvfrom(sock, buf, &len, 0, &addr, &addrlen);
39 if (rc != 0) {
40 if (rc != last_recv_err) {
41 app_perror("...recv error", rc);
42 last_recv_err = rc;
43 }
44 continue;
45 }
46
47 pj_atomic_add(total_bytes, len);
48
49 rc = pj_sock_sendto(sock, buf, &len, 0, &addr, addrlen);
50 if (rc != PJ_SUCCESS) {
51 if (rc != last_write_err) {
52 app_perror("...send error", rc);
53 last_write_err = rc;
54 }
55 continue;
56 }
57 }
Benny Prijonod8410532006-06-15 11:04:33 +000058 return 0;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000059}
60
61
62int echo_srv_sync(void)
63{
64 pj_pool_t *pool;
65 pj_sock_t sock;
66 pj_thread_t *thread[ECHO_SERVER_MAX_THREADS];
67 pj_status_t rc;
68 int i;
69
70 pool = pj_pool_create(mem, NULL, 4000, 4000, NULL);
71 if (!pool)
72 return -5;
73
74 rc = pj_atomic_create(pool, 0, &total_bytes);
75 if (rc != PJ_SUCCESS) {
76 app_perror("...unable to create atomic_var", rc);
77 return -6;
78 }
79
80 rc = app_socket(PJ_AF_INET, PJ_SOCK_DGRAM,0, ECHO_SERVER_START_PORT, &sock);
81 if (rc != PJ_SUCCESS) {
82 app_perror("...socket error", rc);
83 return -10;
84 }
85
86 for (i=0; i<ECHO_SERVER_MAX_THREADS; ++i) {
87 rc = pj_thread_create(pool, NULL, &worker_thread, (void*)sock,
88 PJ_THREAD_DEFAULT_STACK_SIZE, 0,
89 &thread[i]);
90 if (rc != PJ_SUCCESS) {
91 app_perror("...unable to create thread", rc);
92 return -20;
93 }
94 }
95
96 PJ_LOG(3,("", "...UDP echo server running with %d threads at port %d",
97 ECHO_SERVER_MAX_THREADS, ECHO_SERVER_START_PORT));
98 PJ_LOG(3,("", "...Press Ctrl-C to abort"));
99
100 echo_srv_common_loop(total_bytes);
101 return 0;
102}
103
104
105int echo_srv_common_loop(pj_atomic_t *bytes_counter)
106{
107 pj_highprec_t last_received, avg_bw, highest_bw;
108 pj_time_val last_print;
109 unsigned count;
110 const char *ioqueue_name;
111
112 last_received = 0;
113 pj_gettimeofday(&last_print);
114 avg_bw = highest_bw = 0;
115 count = 0;
116
117 ioqueue_name = pj_ioqueue_name();
118
119 for (;;) {
120 pj_highprec_t received, cur_received, bw;
121 unsigned msec;
122 pj_time_val now, duration;
123
124 pj_thread_sleep(1000);
125
126 received = cur_received = pj_atomic_get(bytes_counter);
127 cur_received = cur_received - last_received;
128
129 pj_gettimeofday(&now);
130 duration = now;
131 PJ_TIME_VAL_SUB(duration, last_print);
132 msec = PJ_TIME_VAL_MSEC(duration);
133
134 bw = cur_received;
135 pj_highprec_mul(bw, 1000);
136 pj_highprec_div(bw, msec);
137
138 last_print = now;
139 last_received = received;
140
141 avg_bw = avg_bw + bw;
142 count++;
143
144 PJ_LOG(3,("", "%s UDP (%d threads): %u KB/s (avg=%u KB/s) %s",
145 ioqueue_name,
146 ECHO_SERVER_MAX_THREADS,
147 (unsigned)(bw / 1000),
148 (unsigned)(avg_bw / count / 1000),
149 (count==20 ? "<ses avg>" : "")));
150
151 if (count==20) {
152 if (avg_bw/count > highest_bw)
153 highest_bw = avg_bw/count;
154
155 count = 0;
156 avg_bw = 0;
157
158 PJ_LOG(3,("", "Highest average bandwidth=%u KB/s",
159 (unsigned)(highest_bw/1000)));
160 }
161 }
Benny Prijonod8410532006-06-15 11:04:33 +0000162 return 0;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000163}
164
165