home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xntp3.zip / authstuff / authspeed.c.or < prev    next >
Text File  |  1992-08-12  |  6KB  |  303 lines

  1. /*
  2.  * authspeed - figure out how long it takes to do an NTP encryption
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/time.h>
  8. #include <sys/resource.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #if defined(hpux)
  12. #include <sys/param.h>
  13. #include <sys/times.h>
  14. #endif
  15.  
  16. #include "ntp_fp.h"
  17.  
  18. #define    STREQ(a, b)    (*(a) == *(b) && strcmp((a), (b)) == 0)
  19.  
  20. #define DEFLOOPS    -1
  21.  
  22. #define    DEFDELAYLOOPS    20000
  23. #define    DEFCOSTLOOPS    2000
  24.  
  25. char *progname;
  26. int debug;
  27.  
  28. struct timeval tstart, tend;
  29. #if defined(hpux)
  30. struct tms rstart, rend;
  31. #define    getrusage(foo, t)    times(t)
  32. #define    RUSAGE_SELF    0
  33. #else
  34. struct rusage rstart, rend;
  35. #endif
  36.  
  37. l_fp dummy1, dummy2;
  38. u_long dummy3;
  39.  
  40. u_long pkt[15];
  41.  
  42. int totalcost = 0;
  43. double rtime;
  44. double vtime;
  45.  
  46. int domd5 = 0;
  47.  
  48. /*
  49.  * main - parse arguments and handle options
  50.  */
  51. main(argc, argv)
  52. int argc;
  53. char *argv[];
  54. {
  55.     int c;
  56.     int loops;
  57.     int i;
  58.     int errflg = 0;
  59.     extern int optind;
  60.     extern char *optarg;
  61.  
  62.     progname = argv[0];
  63.     loops = DEFLOOPS;
  64.     while ((c = getopt(argc, argv, "cdmn:")) != EOF)
  65.         switch (c) {
  66.         case 'c':
  67.             totalcost++;
  68.             break;
  69.         case 'd':
  70.             ++debug;
  71.             break;
  72.         case 'm':
  73.             domd5 = 16;    /* offset into list of keys */
  74.             break;
  75.         case 'n':
  76.             loops = atoi(optarg);
  77.             if (loops <= 0) {
  78.                 (void) fprintf(stderr, 
  79.             "%s: %s is unlikely to be a useful number of loops\n",
  80.                     progname, optarg);
  81.                 errflg++;
  82.             }
  83.             break;
  84.         default:
  85.             errflg++;
  86.             break;
  87.         }
  88.     if (errflg || optind == argc) {
  89.         (void) fprintf(stderr,
  90.             "usage: %s [-d] [-n loops] [ -c ] auth.samplekeys\n",
  91.             progname);
  92.         exit(2);
  93.     }
  94.     printf("Compute timing for ");
  95.     if (domd5)
  96.         printf("MD5");
  97.     else
  98.         printf("DES");
  99.     printf(" based authentication.\n");
  100.  
  101.     init_auth();
  102.     authreadkeys(argv[optind]);
  103.     for (i = 0; i < 16; i++) {
  104.         if (!auth_havekey(i + domd5)) {
  105.             errflg++;
  106.             (void) fprintf(stderr, "%s: key %d missing\n",
  107.                 progname, i + domd5);
  108.         }
  109.     }
  110.  
  111.     if (errflg) {
  112.         (void) fprintf(stderr,
  113.     "%s: check syslog for errors, or use file with complete set of keys\n",
  114.             progname);
  115.         exit(1);
  116.     }
  117.  
  118.     if (loops == DEFLOOPS) {
  119.         if (totalcost)
  120.             loops = DEFCOSTLOOPS;
  121.         else
  122.             loops = DEFDELAYLOOPS;
  123.     }
  124.  
  125.     dummy1.l_ui = 0x80808080;
  126.     dummy1.l_uf = 0xffffff00;
  127.     dummy3 = 0x0aaaaaaa;
  128.  
  129.     for (i = 0; i < 12; i++)
  130.         pkt[i] = i * 0x22222;
  131.  
  132.     if (totalcost) {
  133.         if (totalcost > 1)
  134.             docheap(loops);
  135.         else
  136.             docost(loops);
  137.     } else {
  138.         dodelay(loops);
  139.     }
  140.  
  141.     printf("total real time: %.3f\n", rtime);
  142.     printf("total CPU time: %.3f\n", vtime);
  143.     if (totalcost) {
  144.         printf("real cost (in seconds): %.6f\n",
  145.             rtime/(double)loops);
  146.         printf("CPU cost (in seconds): %.6f\n",
  147.             vtime/(double)loops);
  148.         printf("\nThis includes the cost of a decryption plus the\n");
  149.         printf("the cost of an encryption, i.e. the cost to process\n");
  150.         printf("a single authenticated packet.\n");
  151.     } else {
  152.         printf("authdelay in the configuration file\n");
  153.         printf("real authentication delay: %.6f\n",
  154.             rtime/(double)loops);
  155.         printf("authentication delay in CPU time: %.6f\n",
  156.             vtime/(double)loops);
  157.         printf("\nThe CPU delay is probably the best bet for\n");
  158.         printf("authdelay in the configuration file\n");
  159.     }
  160.     exit(0);
  161. }
  162.  
  163.  
  164. /*
  165.  * dodelay - do the delay measurement
  166.  */
  167. dodelay(loops)
  168.     int loops;
  169. {
  170.     double vtime1, rtime1, vtime2, rtime2;
  171.     register int loopcount;
  172.     /*
  173.      *  If we're attempting to compute the cost of an auth2crypt()
  174.      *  for first compute the total cost, then compute the
  175.      *  cost of only doing the first step, auth1crypt().  What 
  176.      *  remains is the cost of auth2crypt.
  177.      */
  178.     loopcount = loops;
  179.     (void) gettimeofday(&tstart, (struct timezone *)0);
  180.     (void) getrusage(RUSAGE_SELF, &rstart);
  181.     
  182.     while (loopcount-- > 0) {
  183.         auth1crypt((loops & 0xf) + domd5, pkt, 48);
  184.         L_ADDUF(&dummy1, dummy3);
  185.         auth2crypt((loops & 0xf) + domd5, pkt, 48);
  186.     }
  187.     
  188.     (void) getrusage(RUSAGE_SELF, &rend);
  189.     (void) gettimeofday(&tend, (struct timezone *)0);
  190.     
  191.     subtime(&tstart, &tend, &rtime1);
  192. #if defined(hpux)
  193.     vtime1 = (rend.tms_utime - rstart.tms_utime) * 1.0 / HZ;
  194. #else
  195.     subtime(&rstart.ru_utime, &rend.ru_utime, &vtime1);
  196. #endif
  197. printf("Time for full encryptions is %f rusage %f real\n", vtime1, rtime1);
  198.     loopcount = loops;
  199.     (void) gettimeofday(&tstart, (struct timezone *)0);
  200.     (void) getrusage(RUSAGE_SELF, &rstart);
  201.  
  202.     while (loopcount-- > 0) {
  203.         auth1crypt((loops & 0xf) + domd5, pkt, 48);
  204.     }
  205.  
  206.     (void) getrusage(RUSAGE_SELF, &rend);
  207.     (void) gettimeofday(&tend, (struct timezone *)0);
  208.  
  209.     subtime(&tstart, &tend, &rtime2);
  210. #if defined(hpux)
  211.     vtime2 = (rend.tms_utime - rstart.tms_utime) * 1.0 / HZ;
  212. #else
  213.     subtime(&rstart.ru_utime, &rend.ru_utime, &vtime2);
  214. #endif
  215.  
  216. printf("Time for auth1crypt is %f rusage %f real\n", vtime2, rtime2);
  217.     vtime = vtime1 - vtime2;
  218.     rtime = rtime1 - rtime2;
  219. }
  220.  
  221.  
  222. /*
  223.  * docheap - do the cost measurement the cheap way
  224.  */
  225. docheap(loops)
  226.     register int loops;
  227. {
  228.  
  229.     (void) authhavekey(3 + domd5);
  230.  
  231.     (void) gettimeofday(&tstart, (struct timezone *)0);
  232.     (void) getrusage(RUSAGE_SELF, &rstart);
  233.  
  234.     while (loops-- > 0) {
  235.         auth1crypt(3 + domd5, pkt, 48);
  236.         L_ADDUF(&dummy1, dummy3);
  237.         auth2crypt(3 + domd5, pkt, 48);
  238.         (void) authdecrypt(3 + domd5, pkt, 48);
  239.     }
  240.  
  241.     (void) getrusage(RUSAGE_SELF, &rend);
  242.     (void) gettimeofday(&tend, (struct timezone *)0);
  243.  
  244.     subtime(&tstart, &tend, &rtime);
  245. #if defined(hpux)
  246.     vtime = (rend.tms_utime - rstart.tms_utime) * 1.0 / HZ;
  247. #else
  248.     subtime(&rstart.ru_utime, &rend.ru_utime, &vtime);
  249. #endif
  250. }
  251.  
  252.  
  253. /*
  254.  * docost - do the cost measurement
  255.  */
  256. docost(loops)
  257.     register int loops;
  258. {
  259.  
  260.     (void) gettimeofday(&tstart, (struct timezone *)0);
  261.     (void) getrusage(RUSAGE_SELF, &rstart);
  262.  
  263.     while (loops-- > 0) {
  264.         auth1crypt((loops & 0xf) + domd5, pkt, 48);
  265.         L_ADDUF(&dummy1, dummy3);
  266.         auth2crypt((loops & 0xf) + domd5, pkt, 48);
  267.         (void) authdecrypt(((loops+1) & 0xf) + domd5, pkt, 48);
  268.     }
  269.  
  270.     (void) getrusage(RUSAGE_SELF, &rend);
  271.     (void) gettimeofday(&tend, (struct timezone *)0);
  272.  
  273.     subtime(&tstart, &tend, &rtime);
  274. #if defined(hpux)
  275.     vtime = (rend.tms_utime - rstart.tms_utime) * 1.0 / HZ;
  276. #else
  277.     subtime(&rstart.ru_utime, &rend.ru_utime, &vtime);
  278. #endif
  279. }
  280.  
  281.  
  282. /*
  283.  * subtime - subtract two struct timevals, return double result
  284.  */
  285. subtime(tvs, tve, res)
  286.     struct timeval *tvs, *tve;
  287.     double *res;
  288. {
  289.     long sec;
  290.     long usec;
  291.  
  292.     sec = tve->tv_sec - tvs->tv_sec;
  293.     usec = tve->tv_usec - tvs->tv_usec;
  294.  
  295.     if (usec < 0) {
  296.         usec += 1000000;
  297.         sec--;
  298.     }
  299.  
  300.     *res = (double)sec + (double)usec/1000000.;
  301.     return;
  302. }
  303.