f7507fd789f0493791441e996c34856aed43b8f8
[dragonfly.git] / secure / lib / libcipher / test / speedcrypt.c
1 #include <sys/types.h>
2 #include <sys/time.h>
3 #include <sys/resource.h>
4 #include <signal.h>
5 #include <stdio.h>
6
7 int             keep_going, count, alternate, seconds;
8 struct rusage   prior, now;
9
10 void
11 finish()
12 {
13         keep_going = 0;
14 }
15
16
17 main(int argc, char *argv[])
18 {
19         struct itimerval itv;
20         u_long          msecs, key1[8], key2[8];
21         char            *k1, *k2;
22
23         if (argc < 2 || sscanf(argv[1], "%d", &seconds) != 1)
24                 seconds = 20;
25
26         if (argc < 3 || sscanf(argv[2], "%d", &alternate) != 1)
27                 alternate = 0;
28
29         printf ("Running crypt%s for %d seconds of vtime...\n",
30                 alternate ? " with alternate keys" : "", seconds);
31
32         bzero(&itv, sizeof (itv));
33         signal (SIGVTALRM, finish);
34         itv.it_value.tv_sec = seconds;
35         itv.it_value.tv_usec = 0;
36         setitimer(ITIMER_VIRTUAL, &itv, NULL);
37
38         keep_going = 1;
39         if (getrusage(0, &prior) < 0) {
40                 perror("getrusage");
41                 exit(1);
42         }
43
44         k1 = (char *) key1;
45         k2 = (char *) key2;
46         strcpy(k1, "fredfredfredfredfred");
47         strcpy(k2, "joejoejoejoejoejoejo");
48
49         if (alternate)
50                 for (count = 0; keep_going; count++)
51                 {
52 #if defined(LONGCRYPT)
53                         crypt((count & 1) ? k1 : k2, "_ara.X...");
54 #else
55                         crypt((count & 1) ? k1 : k2, "eek");
56 #endif
57                 }
58         else
59                 for (count = 0; keep_going; count++)
60                 {
61 #if defined(LONGCRYPT)
62                         crypt(k1, "_ara.X...");
63 #else
64                         crypt(k1, "eek");
65 #endif
66                 }
67
68         if (getrusage(0, &now) < 0) {
69                 perror("getrusage");
70                 exit(1);
71         }
72         msecs = (now.ru_utime.tv_sec - prior.ru_utime.tv_sec) * 1000
73               + (now.ru_utime.tv_usec - prior.ru_utime.tv_usec) / 1000;
74         printf ("\tDid %d crypt()s per second.\n", 1000 * count / msecs);
75         exit(0);
76 }