Fix license.
[gnutls.git] / src / benchmark.c
blobb54a866d61f3568b8c8173a7e969e633694383d5
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>
32 static unsigned char data[64 * 1024];
34 #define TOTAL_ITER 8*1024
36 static void
37 cipher_bench (int algo, int size)
39 int ret, i;
40 gnutls_cipher_hd_t ctx;
41 void *_key, *_iv;
42 gnutls_datum_t key, iv;
43 struct timespec start, stop;
44 double secs;
45 long data_size = 0;
46 double dd;
47 int blocksize = gnutls_cipher_get_block_size (algo);
48 int keysize = gnutls_cipher_get_key_size (algo);
50 _key = malloc (keysize);
51 if (_key == NULL)
52 return;
53 memset (_key, 0xf0, keysize);
55 _iv = malloc (blocksize);
56 if (_iv == NULL)
57 return;
58 memset (_iv, 0xf0, blocksize);
60 iv.data = _iv;
61 iv.size = blocksize;
63 key.data = _key;
64 key.size = keysize;
66 gnutls_global_init ();
68 printf ("Checking %s (%dkb payload)... ", gnutls_cipher_get_name (algo),
69 size);
70 fflush (stdout);
71 clock_gettime (CLOCK_MONOTONIC, &start);
73 ret = gnutls_cipher_init (&ctx, algo, &key, &iv);
74 if (ret < 0)
76 fprintf (stderr, "error: %s\n", gnutls_strerror (ret));
77 goto leave;
80 for (i = 0; i < TOTAL_ITER; i++)
82 gnutls_cipher_encrypt (ctx, data, size * 1024);
83 data_size += size * 1024;
86 gnutls_cipher_deinit (ctx);
88 clock_gettime (CLOCK_MONOTONIC, &stop);
90 secs =
91 (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 clock_gettime (CLOCK_MONOTONIC, &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 clock_gettime (CLOCK_MONOTONIC, &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);
147 main (void)
149 mac_bench (GNUTLS_MAC_SHA1, 4);
150 mac_bench (GNUTLS_MAC_SHA1, 8);
151 mac_bench (GNUTLS_MAC_SHA1, 16);
153 mac_bench (GNUTLS_MAC_SHA256, 4);
154 mac_bench (GNUTLS_MAC_SHA256, 8);
155 mac_bench (GNUTLS_MAC_SHA256, 16);
157 cipher_bench (GNUTLS_CIPHER_3DES_CBC, 4);
158 cipher_bench (GNUTLS_CIPHER_3DES_CBC, 8);
159 cipher_bench (GNUTLS_CIPHER_3DES_CBC, 16);
161 cipher_bench (GNUTLS_CIPHER_AES_128_CBC, 4);
162 cipher_bench (GNUTLS_CIPHER_AES_128_CBC, 8);
163 cipher_bench (GNUTLS_CIPHER_AES_128_CBC, 16);
165 cipher_bench (GNUTLS_CIPHER_ARCFOUR, 4);
166 cipher_bench (GNUTLS_CIPHER_ARCFOUR, 8);
167 cipher_bench (GNUTLS_CIPHER_ARCFOUR, 16);
169 return 0;