#include <sys/resource.h>
#define MAXMSGS (5000 - 1)
register int i
, kb
, msgs
= 0;
int msglen
= 0, ms
, lens
[MAXMSGS
];
printf("usage: %s port [ #msgs ] [ max-msg-length ]\n",
s
= socket(AF_INET
, SOCK_DGRAM
, 0);
sinlen
= sizeof (myname
);
gethostname(myname
, &sinlen
);
hp
= gethostbyname(myname
);
printf("%s: host unknown\n", myname
);
sin
.sin_family
= AF_INET
;
bcopy(hp
->h_addr
, &sin
.sin_addr
, hp
->h_length
);
if (bind(s
, &sin
, sizeof (sin
)) < 0) {
sin
.sin_port
= htons(atoi(argv
[1]));
if (msgs
<= 0 || msgs
> MAXMSGS
)
if (msglen
<= 0 || msglen
>= MAXMSGLEN
)
printf("couldn't allocate data buffer\n");
for (i
= 0; i
< msgs
; i
++)
lens
[i
] = random() % msglen
;
for (i
= 0; i
< msglen
; i
++)
buf
[i
] = random() & 0xff;
printf("%d messages, max message length %d bytes\n",
(void) sendto(s
, "begin", 5, 0, &sin
, sizeof (sin
));
getrusage(RUSAGE_SELF
, &r1
);
gettimeofday(&t1
, (struct timezone
*)0);
for (i
= 0; i
< msgs
; i
++) {
if (sendto(s
, buf
, lens
[i
], 0, &sin
, sizeof (sin
)) < 0)
perror("inetdg: sendto");
(void) sendto(s
, "end", 3, 0, &sin
, sizeof (sin
));
gettimeofday(&t2
, (struct timezone
*)0);
getrusage(RUSAGE_SELF
, &r2
);
printf("%d msgs (%d bytes) in %d.%d secs", msgs
,
kb
, t2
.tv_sec
, ms
/ 100);
printf(", %d bytes/msg, %6.2f kb/s, %4.1f ms/msg\n",
kb
/ msgs
, (8. * kb
) / (t2
.tv_sec
* 1024.),
(1000. * t2
.tv_sec
+ ms
) / msgs
);
timevalsub(&r2
.ru_stime
, &r1
.ru_stime
);
timevalsub(&r2
.ru_utime
, &r1
.ru_utime
);
r2
.ru_nvcsw
-= r1
.ru_nvcsw
;
r2
.ru_nivcsw
-= r1
.ru_nivcsw
;
printf("System %d.%d, user %d.%d, %d vcsw, %d ivcsw\n",
r2
.ru_stime
.tv_sec
, r2
.ru_stime
.tv_usec
/ 100000,
r2
.ru_utime
.tv_sec
, r2
.ru_utime
.tv_usec
/ 100000,
r2
.ru_nvcsw
, r2
.ru_nivcsw
);
* Add and subtract routines for timevals.
* N.B.: subtract routine doesn't deal with
* results which are before the beginning,
* it just gets very confused in this case.
t1
->tv_sec
+= t2
->tv_sec
;
t1
->tv_usec
+= t2
->tv_usec
;
t1
->tv_sec
-= t2
->tv_sec
;
t1
->tv_usec
-= t2
->tv_usec
;
if (t1
->tv_usec
>= 1000000) {