home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / sys / tests / benchmarks / inet / inetdgs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-05-06  |  2.7 KB  |  129 lines

  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <sys/time.h>
  4. #include <sys/resource.h>
  5. #include <netinet/in.h>
  6.  
  7. #include <stdio.h>
  8. #include <signal.h>
  9. #include <setjmp.h>
  10. #include <errno.h>
  11.  
  12. struct    sockaddr_in sin;
  13. jmp_buf    j;
  14. int    catchsig();
  15.  
  16. main(argc, argv)
  17.     char *argv[];
  18. {
  19.     int s, n;
  20.     char buf[2 * BUFSIZ];
  21.     struct rusage r1, r2;
  22.  
  23.     if (argc < 2) {
  24.         printf("usage: %s port\n", argv[0]);
  25.         exit(1);
  26.     }
  27.     s = socket(AF_INET, SOCK_DGRAM, 0);
  28.     if (s < 0) {
  29.         perror("inetdgs: socket");
  30.         exit(1);
  31.     }
  32.     sin.sin_family = AF_INET;
  33.     sin.sin_port = htons(atoi(argv[1]));
  34.     if (bind(s, &sin, sizeof (sin)) < 0) {
  35.         perror("inetdgs: bind");
  36.         exit(1);
  37.     }
  38.     for (;;) {
  39.         int count, ms, kb, fromlen;
  40.         struct timeval t1, t2;
  41.         struct sockaddr_in from;
  42.         extern int errno;
  43.  
  44.         for (;;) {
  45.             fromlen = sizeof (from);
  46.             n = recvfrom(s, buf, sizeof (buf), 0, &from, &fromlen);
  47.             if (n < 0) {
  48.                 if (errno != EINTR)
  49.                     perror("inetdgs: recvfrom");
  50.                 continue;
  51.             }
  52.             if (strncmp(buf, "begin", 5) == 0)
  53.                 break;
  54.         }
  55.         count = kb = 0;
  56.         getrusage(RUSAGE_SELF, &r1);
  57.         gettimeofday(&t1, (struct timezone *)0);
  58.         for (;;) {
  59.             fromlen = sizeof (from);
  60.             n = recvfrom(s, buf, sizeof (buf), 0, &from, &fromlen);
  61.             if (n < 0) {
  62.                 if (errno != EINTR)
  63.                     perror("inetdgs: recvfrom");
  64.                 continue;
  65.             }
  66.             count++, kb += n;
  67.             if (strncmp(buf, "end", 3) == 0)
  68.                 break;
  69.         }
  70.         gettimeofday(&t2, (struct timezone *)0);
  71.         getrusage(RUSAGE_SELF, &r2);
  72.         sleep(2);
  73.         timevalsub(&t2, &t1);
  74.         ms = t2.tv_usec / 1000;
  75.         printf("%d msgs (%d bytes) in %d.%d secs", count,
  76.             kb, t2.tv_sec, ms / 100);
  77.         printf(", %d bytes/msg, %6.2f kb/s, %4.1f ms/msg\n",
  78.             kb / count, (8. * kb) / (t2.tv_sec * 1024.),
  79.            (1000. * t2.tv_sec + ms) / count);
  80.         timevalsub(&r2.ru_stime, &r1.ru_stime);
  81.         timevalsub(&r2.ru_utime, &r1.ru_utime);
  82.         r2.ru_nvcsw -= r1.ru_nvcsw;
  83.         r2.ru_nivcsw -= r1.ru_nivcsw;
  84.         printf("System %d.%d, user %d.%d, %d vcsw, %d ivcsw\n",
  85.             r2.ru_stime.tv_sec, r2.ru_stime.tv_usec / 100000,
  86.             r2.ru_utime.tv_sec, r2.ru_utime.tv_usec / 100000,
  87.             r2.ru_nvcsw, r2.ru_nivcsw);
  88.     }
  89. }
  90.  
  91. /*
  92.  * Add and subtract routines for timevals.
  93.  * N.B.: subtract routine doesn't deal with
  94.  * results which are before the beginning,
  95.  * it just gets very confused in this case.
  96.  * Caveat emptor.
  97.  */
  98. timevaladd(t1, t2)
  99.     struct timeval *t1, *t2;
  100. {
  101.  
  102.     t1->tv_sec += t2->tv_sec;
  103.     t1->tv_usec += t2->tv_usec;
  104.     timevalfix(t1);
  105. }
  106.  
  107. timevalsub(t1, t2)
  108.     struct timeval *t1, *t2;
  109. {
  110.  
  111.     t1->tv_sec -= t2->tv_sec;
  112.     t1->tv_usec -= t2->tv_usec;
  113.     timevalfix(t1);
  114. }
  115.  
  116. timevalfix(t1)
  117.     struct timeval *t1;
  118. {
  119.  
  120.     if (t1->tv_usec < 0) {
  121.         t1->tv_sec--;
  122.         t1->tv_usec += 1000000;
  123.     }
  124.     if (t1->tv_usec >= 1000000) {
  125.         t1->tv_sec++;
  126.         t1->tv_usec -= 1000000;
  127.     }
  128. }
  129.