Fix indent bug.
[gnutls.git] / src / benchmark.c
blob29d6474e8e5a8b6b9cc6c8635328db33fc0814aa
1 /*
2 * Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 * This file is part of GNUTLS.
6 * GNUTLS is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GNUTLS is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
20 * Written by Nikos Mavrogiannopoulos <nmav@gnutls.org>.
23 #include <config.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <gnutls/gnutls.h>
29 #include <gnutls/crypto.h>
30 #include <time.h>
31 #include "timespec.h" /* gnulib gettime */
33 static unsigned char data[64 * 1024];
35 #define TOTAL_ITER 8*1024
37 static void
38 cipher_bench (int algo, int size)
40 int ret, i;
41 gnutls_cipher_hd_t ctx;
42 void *_key, *_iv;
43 gnutls_datum_t key, iv;
44 struct timespec start, stop;
45 double secs;
46 long data_size = 0;
47 double dd;
48 int blocksize = gnutls_cipher_get_block_size (algo);
49 int keysize = gnutls_cipher_get_key_size (algo);
51 _key = malloc (keysize);
52 if (_key == NULL)
53 return;
54 memset (_key, 0xf0, keysize);
56 _iv = malloc (blocksize);
57 if (_iv == NULL)
58 return;
59 memset (_iv, 0xf0, blocksize);
61 iv.data = _iv;
62 iv.size = blocksize;
64 key.data = _key;
65 key.size = keysize;
67 gnutls_global_init ();
69 printf ("Checking %s (%dkb payload)... ", gnutls_cipher_get_name (algo),
70 size);
71 fflush (stdout);
72 gettime (&start);
74 ret = gnutls_cipher_init (&ctx, algo, &key, &iv);
75 if (ret < 0)
77 fprintf (stderr, "error: %s\n", gnutls_strerror (ret));
78 goto leave;
81 for (i = 0; i < TOTAL_ITER; i++)
83 gnutls_cipher_encrypt (ctx, data, size * 1024);
84 data_size += size * 1024;
87 gnutls_cipher_deinit (ctx);
89 gettime (&stop);
91 secs = (stop.tv_sec * 1000 + stop.tv_nsec / (1000 * 1000) -
92 (start.tv_sec * 1000 + start.tv_nsec / (1000 * 1000)));
93 secs /= 1000;
94 dd = (((double) data_size / (double) secs)) / 1000;
95 printf ("Encrypted %ld kb in %.2f secs: ", data_size / 1000, secs);
96 printf ("%.2f kbyte/sec\n", dd);
98 leave:
99 free (_key);
100 free (_iv);
104 static void
105 mac_bench (int algo, int size)
107 int i;
108 void *_key;
109 struct timespec start, stop;
110 double secs;
111 long data_size = 0;
112 double dd;
113 int blocksize = gnutls_hmac_get_len (algo);
115 _key = malloc (blocksize);
116 if (_key == NULL)
117 return;
118 memset (_key, 0xf0, blocksize);
120 gnutls_global_init ();
122 printf ("Checking %s (%dkb payload)... ", gnutls_mac_get_name (algo), size);
123 fflush (stdout);
124 gettime (&start);
126 for (i = 0; i < TOTAL_ITER; i++)
128 gnutls_hmac_fast (algo, _key, blocksize, data, size * 1024, _key);
129 data_size += size * 1024;
132 gettime (&stop);
134 secs =
135 (stop.tv_sec * 1000 + stop.tv_nsec / (1000 * 1000) -
136 (start.tv_sec * 1000 + start.tv_nsec / (1000 * 1000)));
137 secs /= 1000;
138 dd = (((double) data_size / (double) secs)) / 1000;
139 printf ("Hashed %ld kb in %.2f secs: ", data_size / 1000, secs);
140 printf ("%.2f kbyte/sec\n", dd);
142 free (_key);
146 main (void)
148 mac_bench (GNUTLS_MAC_SHA1, 4);
149 mac_bench (GNUTLS_MAC_SHA1, 8);
150 mac_bench (GNUTLS_MAC_SHA1, 16);
152 mac_bench (GNUTLS_MAC_SHA256, 4);
153 mac_bench (GNUTLS_MAC_SHA256, 8);
154 mac_bench (GNUTLS_MAC_SHA256, 16);
156 cipher_bench (GNUTLS_CIPHER_3DES_CBC, 4);
157 cipher_bench (GNUTLS_CIPHER_3DES_CBC, 8);
158 cipher_bench (GNUTLS_CIPHER_3DES_CBC, 16);
160 cipher_bench (GNUTLS_CIPHER_AES_128_CBC, 4);
161 cipher_bench (GNUTLS_CIPHER_AES_128_CBC, 8);
162 cipher_bench (GNUTLS_CIPHER_AES_128_CBC, 16);
164 cipher_bench (GNUTLS_CIPHER_ARCFOUR, 4);
165 cipher_bench (GNUTLS_CIPHER_ARCFOUR, 8);
166 cipher_bench (GNUTLS_CIPHER_ARCFOUR, 16);
168 return 0;