1 //==========================================================================
5 // Simple TCP throughput test - source component
6 // * CAUTION: host, i.e. non eCos, only *
8 //==========================================================================
9 //####BSDCOPYRIGHTBEGIN####
11 // -------------------------------------------
13 // Portions of this software may have been derived from OpenBSD or other sources,
14 // and are covered by the appropriate copyright disclaimers included herein.
16 // -------------------------------------------
18 //####BSDCOPYRIGHTEND####
19 //==========================================================================
20 //#####DESCRIPTIONBEGIN####
23 // Contributors: gthomas
26 // Description: This is the middle part of a three part test. The idea is
27 // to test the throughput of box in a configuration like this:
29 // +------+ port +----+ port +----+
30 // |SOURCE|=========>|ECHO|============>|SINK|
31 // +------+ 9990 +----+ 9991 +----+
34 //####DESCRIPTIONEND####
36 //==========================================================================
38 // Network throughput test code
45 #include <sys/param.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48 #include <sys/errno.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/in.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip_icmp.h>
59 #define SOURCE_PORT 9990
60 #define SINK_PORT 9991
74 static unsigned char data_buf[MAX_BUF];
84 show_results(struct timeval *start, struct timeval *end,
85 int nbufs, int buflen)
87 struct timeval tot_time;
88 long tot_bytes = nbufs * buflen;
89 double real_time, thru;
90 timersub(end, start, &tot_time);
91 printf("SOURCE complete - %d bufs of %d bytes in %ld.%02ld seconds",
93 tot_time.tv_sec, tot_time.tv_usec / 10000);
94 real_time = tot_time.tv_sec + ((tot_time.tv_usec / 10000) * .01);
95 // Compute bytes / second (rounded up)
96 thru = tot_bytes / real_time;
97 // Convert to Mb / second
98 printf(" - %.2f KB/S", thru / 1024.0);
99 printf(" - %.4f Mbit/S (M = 10^6)", thru * 8.0 / 1000000.0);
104 do_read(int s, unsigned char *buf, int len)
106 int total, slen, rlen;
109 while (total < len) {
110 slen = read(s, buf, rlen);
113 printf("Error after reading %d bytes\n", total);
125 do_write(int s, unsigned char *buf, int len)
127 int total, slen, rlen;
130 while (total < len) {
131 slen = write(s, buf, rlen);
134 printf("Error after writing %d bytes\n", total);
146 source_test(char *echo_node, int load)
149 struct sockaddr_in slave, local;
152 struct hostent *host;
153 struct test_params params;
154 struct test_params nparams;
155 struct test_status status;
156 struct test_status nstatus;
157 struct timeval start_time, end_time;
160 printf("Start TCP test - SOURCE mode to %s\n", echo_node);
162 host = gethostbyname(echo_node);
163 if (host == (struct hostent *)NULL) {
164 pexit("gethostbyname");
167 memset(&slave, 0, sizeof(slave));
168 slave.sin_family = AF_INET;
170 slave.sin_len = sizeof(slave);
172 slave.sin_port = htons(SOURCE_PORT);
173 memcpy(&slave.sin_addr.s_addr, host->h_addr, host->h_length);
175 s_source = socket(AF_INET, SOCK_STREAM, 0);
177 pexit("stream socket");
179 if (setsockopt(s_source, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
180 pexit("setsockopt /source/ SO_REUSEADDR");
182 memset(&local, 0, sizeof(local));
183 local.sin_family = AF_INET;
185 local.sin_len = sizeof(local);
187 local.sin_port = INADDR_ANY;
188 local.sin_addr.s_addr = INADDR_ANY;
189 if(bind(s_source, (struct sockaddr *) &local, sizeof(local)) < 0) {
190 pexit("bind /source/ error");
193 if (connect(s_source, (struct sockaddr *)&slave, sizeof(slave)) < 0) {
194 pexit("Can't connect to target");
197 params.nbufs = NUM_BUF;
198 params.bufsize = MAX_BUF;
201 nparams.nbufs = htonl(params.nbufs);
202 nparams.bufsize = htonl(params.bufsize);
203 nparams.load = htonl(params.load);
205 if ( do_write(s_source, (unsigned char *)&nparams, sizeof(nparams))
207 pexit("Can't send initialization parameters");
209 if (do_read(s_source, (unsigned char *)&nstatus, sizeof(nstatus))
211 pexit("Can't get status from 'echo' client");
214 status.ok = ntohl(nstatus.ok);
217 gettimeofday(&start_time, 0);
218 for (i = 0; i < params.nbufs; i++) {
219 if ((len = do_write(s_source, data_buf, params.bufsize)) != params.bufsize) {
220 printf("Error writing buffer #%d:", i+1);
222 perror("can't write data");
224 printf(" short data, only wrote %d bytes\n", len);
228 gettimeofday(&end_time, 0);
229 show_results(&start_time, &end_time, params.nbufs, params.bufsize);
233 main(int argc, char *argv[])
237 load = atoi(argv[2]);
239 source_test(argv[1], load);