home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / crypt / speeds.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-02  |  2.5 KB  |  140 lines

  1. /*
  2.  * This fcrypt/crypt speed testing program
  3.  * is derived from one floating around in
  4.  * the net. It's distributed along with
  5.  * UFC-crypt but is not covered by any
  6.  * licence.
  7.  *
  8.  * @(#)speeds.c    1.7 3/26/92
  9.  */
  10.  
  11. #ifdef notdef
  12. /* 
  13.  * patch from chip@chinacat.unicom.com (Chip Rosenthal):
  14.  * you may enable it if your system does not include
  15.  * a setitimer() function. You'll have to ensure the
  16.  * existence a environment variable: HZ giving how many
  17.  * ticks goes per second.
  18.  * If not existing in your default environment 50, 60 
  19.  * or even 100 may be the right value. Perhaps you should
  20.  * then use 'time ./ufc 10000' instead of guessing.
  21.  */
  22. #define NO_ITIMER
  23. #endif
  24.  
  25. #ifdef NO_ITIMER
  26. #include <sys/types.h>
  27. #include <sys/times.h>
  28. #else
  29. #include <sys/time.h>
  30. #endif
  31. #include <signal.h>
  32. #include <stdio.h>
  33.  
  34. static int cnt;
  35. #ifdef NO_ITIMER
  36. char *hz;
  37. struct tms tstart, tfinish;
  38. #endif
  39. #define ITIME    10        /* Number of seconds to run test. */
  40.  
  41. void
  42. Stop ()
  43. {
  44.     double elapsed;
  45. #ifdef NO_ITIMER
  46.     (void) times(&tfinish);
  47.     elapsed = ((tfinish.tms_utime + tfinish.tms_stime) -
  48.     (tstart.tms_utime + tstart.tms_stime)) / atoi(hz);
  49.     printf("elapsed time = %d sec,  CPU time = %f sec\n", ITIME, elapsed);
  50. #else
  51.     elapsed = ITIME;
  52. #endif
  53.     printf ("Did %f %s()s per second.\n", ((float) cnt) / elapsed,
  54. #if defined(FCRYPT)
  55.         "fcrypt"
  56. #else
  57.         "crypt"
  58. #endif
  59.     );
  60.     exit (0);
  61. }
  62.  
  63. /*
  64.  * Silly rewrite of 'bzero'. I do so
  65.  * because some machines don't have
  66.  * bzero and some don't have memset.
  67.  */
  68.  
  69. static void clearmem(start, cnt)
  70.   char *start;
  71.   int cnt;
  72.   { while(cnt--)
  73.       *start++ = '\0';
  74.   }
  75.  
  76. main ()
  77. {
  78. #ifdef NO_ITIMER
  79.     extern char *getenv();
  80. #else
  81.     struct itimerval itv;
  82. #endif
  83.  
  84. #ifdef NO_ITIMER
  85.     if ((hz = getenv("HZ")) == NULL) {
  86.     fprintf(stderr, "HZ environment parameter undefined\n");
  87.     exit(1);
  88.     }
  89. #endif
  90.  
  91.     printf ("Running %s for %d seconds of virtual time ...\n",
  92. #ifdef FCRYPT
  93.     "UFC-crypt",
  94. #else
  95.     "crypt(libc)",
  96. #endif
  97.         ITIME);
  98.  
  99. #ifdef FCRYPT
  100.     init_des ();
  101. #endif
  102.  
  103. #ifdef NO_ITIMER
  104.     signal(SIGALRM, Stop);
  105.     switch (fork()) {
  106.     case -1:
  107.     perror("fork failed");
  108.     exit(1);
  109.     case 0:
  110.     sleep(10);
  111.     kill(getppid(), SIGALRM);
  112.     exit(0);
  113.     default:
  114.     (void) times(&tstart);
  115.     }
  116. #else
  117.     clearmem (&itv, sizeof (itv));
  118.     signal (SIGVTALRM, Stop);
  119.     itv.it_value.tv_sec = ITIME;
  120.     itv.it_value.tv_usec = 0;
  121.     setitimer (ITIMER_VIRTUAL, &itv, NULL);
  122. #endif
  123.  
  124.  
  125.     for (cnt = 0;; cnt++)
  126.     {
  127. #ifdef FCRYPT
  128.     fcrypt ("fredfred", "eek");
  129. #else
  130.     crypt ("fredfred", "eek");
  131. #endif
  132.     }
  133. }
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.