-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
send: Add network bandwidth benchmark
Copy from `~buczek/src/small/send.c`.
- Loading branch information
Showing
1 changed file
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <stdlib.h> | ||
#include <netinet/in.h> | ||
#include <netdb.h> | ||
#include <signal.h> | ||
#include <errno.h> | ||
#include <time.h> | ||
#include <sys/time.h> | ||
#include <sys/socket.h> | ||
|
||
#ifndef IPPORT_DISCARD | ||
#define IPPORT_DISCARD 9 | ||
#endif | ||
|
||
void usage() { | ||
fprintf(stderr,"usage: send host [port]\n"); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
static int gotAlarm=0; | ||
|
||
void sigAlarm() { | ||
gotAlarm=1; | ||
} | ||
|
||
void setLinger(int s) | ||
{ | ||
int status; | ||
struct linger linger = {1,300}; /* on, seconds */ | ||
/* DigitalUnix bug : close does not | ||
return error if timeout. */ | ||
|
||
status = setsockopt(s,SOL_SOCKET,SO_LINGER,&linger,sizeof(linger)); | ||
if (status<0) {perror("setsockopt");exit(EXIT_FAILURE);} | ||
} | ||
|
||
static char buffer[8192]; | ||
|
||
|
||
main(int argc,char *argv[]) { | ||
|
||
char *host; | ||
short port; | ||
struct hostent *he; | ||
struct sockaddr_in sin; | ||
int s; | ||
int i; | ||
int status; | ||
int byteCount; | ||
double bps; | ||
struct timeval startTime; | ||
struct timeval endTime; | ||
double elapsed; | ||
|
||
|
||
int seconds=10; | ||
|
||
if (argc<2 || argc>3) usage(); | ||
host=argv[1]; | ||
port = argc==3 ? atoi(argv[2]) : IPPORT_DISCARD; | ||
|
||
memset(buffer,0xA3,sizeof(buffer)); | ||
|
||
he=gethostbyname(host); | ||
|
||
if (!he) {fprintf(stderr,"unknown host: %s\n",host);exit(EXIT_FAILURE);} | ||
|
||
host=(char *)he->h_name; | ||
|
||
s=socket(he->h_addrtype,SOCK_STREAM,0); | ||
if (s<0) {perror("socket");exit(EXIT_FAILURE);} | ||
|
||
setLinger(s); | ||
|
||
memset((void *)&sin,0,sizeof(sin)); | ||
sin.sin_family = he->h_addrtype; | ||
sin.sin_port = htons(port); | ||
memcpy(&sin.sin_addr,he->h_addr,he->h_length); | ||
|
||
status=connect(s,(struct sockaddr *)&sin,sizeof(sin)); | ||
if (status<0) {perror("connect");exit(EXIT_FAILURE);} | ||
|
||
signal(SIGALRM,sigAlarm); | ||
alarm(seconds); | ||
|
||
byteCount=0; | ||
|
||
status = gettimeofday(&startTime,NULL); | ||
if (status<0) {perror("gettimeofday");exit(EXIT_FAILURE);} | ||
|
||
fprintf(stderr,"start : %d.%d\n",startTime.tv_sec,startTime.tv_usec); | ||
|
||
while (1) { | ||
int len; | ||
|
||
len = send(s,buffer,sizeof(buffer),0); | ||
if (len<=0) { | ||
if (errno == EINTR) break; | ||
perror("send");exit(EXIT_FAILURE); | ||
} | ||
/* fprintf(stderr,"len: %d\n",len); */ | ||
/* write(2,"#",1); */ | ||
byteCount += len; | ||
if (gotAlarm) break;; | ||
} | ||
|
||
/* | ||
status = gettimeofday(&endTime,NULL); | ||
if (status<0) {perror("gettimeofday");exit(EXIT_FAILURE);} | ||
fprintf(stderr,"shutdown : %d.%d\n",endTime.tv_sec,endTime.tv_usec); | ||
status = shutdown(s,SHUT_RDWR); | ||
if (status<0) {perror("shutdown");exit(EXIT_FAILURE);} | ||
*/ | ||
|
||
status = gettimeofday(&endTime,NULL); | ||
if (status<0) {perror("gettimeofday");exit(EXIT_FAILURE);} | ||
fprintf(stderr,"close : %d.%d\n",endTime.tv_sec,endTime.tv_usec); | ||
|
||
status = close(s); | ||
if (status<0) {perror("close");exit(EXIT_FAILURE);} | ||
|
||
status = gettimeofday(&endTime,NULL); | ||
if (status<0) {perror("gettimeofday");exit(EXIT_FAILURE);} | ||
fprintf(stderr,"done : %d.%d\n",endTime.tv_sec,endTime.tv_usec); | ||
|
||
{ double sec; | ||
sec = endTime.tv_sec - startTime.tv_sec + | ||
(double)(endTime.tv_usec - startTime.tv_usec)/1000000; | ||
printf ("%d bytes in %f seconds\n",byteCount,sec); | ||
printf ("%f bytes / seconds\n",(double)byteCount/sec); | ||
printf ("%f bits / seconds\n",(double)byteCount*8/sec); | ||
printf ("%f kBit/s / seconds\n",(double)byteCount*8/1000/sec); | ||
printf ("%f MBit/s / seconds\n",(double)byteCount*8/1000000/sec); | ||
} | ||
} |