~ubuntu-branches/ubuntu/trusty/gnutls26/trusty

« back to all changes in this revision

Viewing changes to src/benchmark.c

  • Committer: Package Import Robot
  • Author(s): Andreas Metzler
  • Date: 2011-10-01 15:28:13 UTC
  • mfrom: (12.1.20 sid)
  • Revision ID: package-import@ubuntu.com-20111001152813-yygm1c4cxonfxhzy
* New upstream version.
  + Allow CA importing of 0 certificates to succeed. Closes: #640639
* Add libp11-kit-dev to libgnutls-dev dependencies. (see #643811)
* [20_guiledocstring.diff] guile: Fix docstring extraction with CPP 4.5+.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
#include <gnutls/gnutls.h>
29
29
#include <gnutls/crypto.h>
30
30
#include <time.h>
31
 
#include "timespec.h"           /* gnulib gettime */
 
31
#include <signal.h>
 
32
#include "timespec.h"           /* gnulib gettime */
32
33
 
33
34
static unsigned char data[64 * 1024];
34
35
 
35
 
#define TOTAL_ITER 8*1024
 
36
static int must_finish = 0;
 
37
 
 
38
#if !defined(_WIN32)
 
39
static void
 
40
alarm_handler (int signo)
 
41
{
 
42
  must_finish = 1;
 
43
}
 
44
#else
 
45
#include <windows.h>
 
46
DWORD WINAPI alarm_handler (LPVOID lpParameter);
 
47
DWORD WINAPI
 
48
alarm_handler (LPVOID lpParameter)
 
49
{
 
50
  HANDLE wtimer = *((HANDLE *) lpParameter);
 
51
  WaitForSingleObject (wtimer, INFINITE);
 
52
  must_finish = 1;
 
53
  return 0;
 
54
}
 
55
 
 
56
#define W32_ALARM_VARIABLES HANDLE wtimer = NULL, wthread = NULL; \
 
57
  LARGE_INTEGER alarm_timeout
 
58
#define W32_ALARM_TRIGGER(timeout, leave) { \
 
59
  wtimer = CreateWaitableTimer (NULL, TRUE, NULL); \
 
60
  if (wtimer == NULL) \
 
61
    { \
 
62
      fprintf (stderr, "error: CreateWaitableTimer %u\n", GetLastError ()); \
 
63
      leave; \
 
64
    } \
 
65
  wthread = CreateThread (NULL, 0, alarm_handler, &wtimer, 0, NULL); \
 
66
  if (wthread == NULL) \
 
67
    { \
 
68
      fprintf (stderr, "error: CreateThread %u\n", GetLastError ()); \
 
69
      leave; \
 
70
    } \
 
71
  alarm_timeout.QuadPart = timeout * 10000000; \
 
72
  if (SetWaitableTimer (wtimer, &alarm_timeout, 0, NULL, NULL, FALSE) == 0) \
 
73
    { \
 
74
      fprintf (stderr, "error: SetWaitableTimer %u\n", GetLastError ()); \
 
75
      leave; \
 
76
    } \
 
77
  }
 
78
#define W32_ALARM_CLEANUP { \
 
79
  if (wtimer != NULL) \
 
80
    CloseHandle (wtimer); \
 
81
  if (wthread != NULL) \
 
82
    CloseHandle (wthread);}
 
83
#endif
 
84
 
 
85
static void
 
86
tls_log_func (int level, const char *str)
 
87
{
 
88
  fprintf (stderr, "|<%d>| %s", level, str);
 
89
}
 
90
 
 
91
static void
 
92
value2human (double bytes, double time, double *data, double *speed,
 
93
             char *metric)
 
94
{
 
95
  if (bytes > 1000 && bytes < 1000 * 1000)
 
96
    {
 
97
      *data = ((double) bytes) / 1000;
 
98
      *speed = *data / time;
 
99
      strcpy (metric, "Kb");
 
100
      return;
 
101
    }
 
102
  else if (bytes >= 1000 * 1000 && bytes < 1000 * 1000 * 1000)
 
103
    {
 
104
      *data = ((double) bytes) / (1000 * 1000);
 
105
      *speed = *data / time;
 
106
      strcpy (metric, "Mb");
 
107
      return;
 
108
    }
 
109
  else if (bytes >= 1000 * 1000 * 1000)
 
110
    {
 
111
      *data = ((double) bytes) / (1000 * 1000 * 1000);
 
112
      *speed = *data / time;
 
113
      strcpy (metric, "Gb");
 
114
      return;
 
115
    }
 
116
  else
 
117
    {
 
118
      *data = (double) bytes;
 
119
      *speed = *data / time;
 
120
      strcpy (metric, "bytes");
 
121
      return;
 
122
    }
 
123
}
36
124
 
37
125
static void
38
126
cipher_bench (int algo, int size)
39
127
{
40
 
  int ret, i;
 
128
  int ret;
41
129
  gnutls_cipher_hd_t ctx;
42
130
  void *_key, *_iv;
43
131
  gnutls_datum_t key, iv;
44
132
  struct timespec start, stop;
45
133
  double secs;
46
 
  long data_size = 0;
47
 
  double dd;
 
134
  double data_size = 0;
 
135
  double dspeed, ddata;
48
136
  int blocksize = gnutls_cipher_get_block_size (algo);
49
137
  int keysize = gnutls_cipher_get_key_size (algo);
 
138
  char metric[16];
 
139
#if defined(_WIN32)
 
140
  W32_ALARM_VARIABLES;
 
141
#endif
50
142
 
51
143
  _key = malloc (keysize);
52
144
  if (_key == NULL)
64
156
  key.data = _key;
65
157
  key.size = keysize;
66
158
 
67
 
  gnutls_global_init ();
68
 
 
69
159
  printf ("Checking %s (%dkb payload)... ", gnutls_cipher_get_name (algo),
70
 
          size);
 
160
          size);
71
161
  fflush (stdout);
 
162
 
 
163
  must_finish = 0;
 
164
#if !defined(_WIN32)
 
165
  alarm (5);
 
166
#else
 
167
  W32_ALARM_TRIGGER(5, goto leave);
 
168
#endif
 
169
 
72
170
  gettime (&start);
73
171
 
74
172
  ret = gnutls_cipher_init (&ctx, algo, &key, &iv);
78
176
      goto leave;
79
177
    }
80
178
 
81
 
  for (i = 0; i < TOTAL_ITER; i++)
 
179
  do
82
180
    {
83
181
      gnutls_cipher_encrypt (ctx, data, size * 1024);
84
182
      data_size += size * 1024;
85
183
    }
 
184
  while (must_finish == 0);
86
185
 
87
186
  gnutls_cipher_deinit (ctx);
88
187
 
89
188
  gettime (&stop);
90
189
 
91
190
  secs = (stop.tv_sec * 1000 + stop.tv_nsec / (1000 * 1000) -
92
 
          (start.tv_sec * 1000 + start.tv_nsec / (1000 * 1000)));
 
191
          (start.tv_sec * 1000 + start.tv_nsec / (1000 * 1000)));
93
192
  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);
 
193
 
 
194
  value2human (data_size, secs, &ddata, &dspeed, metric);
 
195
  printf ("Encrypted %.2f %s in %.2f secs: ", ddata, metric, secs);
 
196
  printf ("%.2f %s/sec\n", dspeed, metric);
97
197
 
98
198
leave:
99
199
  free (_key);
100
200
  free (_iv);
101
 
 
 
201
#if defined(_WIN32)
 
202
  W32_ALARM_CLEANUP;
 
203
#endif
102
204
}
103
205
 
104
206
static void
105
207
mac_bench (int algo, int size)
106
208
{
107
 
  int i;
108
209
  void *_key;
109
210
  struct timespec start, stop;
110
211
  double secs;
111
 
  long data_size = 0;
112
 
  double dd;
 
212
  double data_size = 0;
 
213
  double ddata, dspeed;
113
214
  int blocksize = gnutls_hmac_get_len (algo);
 
215
  char metric[16];
 
216
#if defined(_WIN32)
 
217
  W32_ALARM_VARIABLES;
 
218
#endif
114
219
 
115
220
  _key = malloc (blocksize);
116
221
  if (_key == NULL)
117
222
    return;
118
223
  memset (_key, 0xf0, blocksize);
119
224
 
120
 
  gnutls_global_init ();
121
 
 
122
225
  printf ("Checking %s (%dkb payload)... ", gnutls_mac_get_name (algo), size);
123
226
  fflush (stdout);
 
227
 
 
228
  must_finish = 0;
 
229
#if !defined(_WIN32)
 
230
  alarm (5);
 
231
#else
 
232
  W32_ALARM_TRIGGER(5, goto leave);
 
233
#endif
 
234
 
124
235
  gettime (&start);
125
236
 
126
 
  for (i = 0; i < TOTAL_ITER; i++)
 
237
  do
127
238
    {
128
239
      gnutls_hmac_fast (algo, _key, blocksize, data, size * 1024, _key);
129
240
      data_size += size * 1024;
130
241
    }
 
242
  while (must_finish == 0);
131
243
 
132
244
  gettime (&stop);
133
245
 
135
247
    (stop.tv_sec * 1000 + stop.tv_nsec / (1000 * 1000) -
136
248
     (start.tv_sec * 1000 + start.tv_nsec / (1000 * 1000)));
137
249
  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);
141
 
 
 
250
 
 
251
  value2human (data_size, secs, &ddata, &dspeed, metric);
 
252
 
 
253
  printf ("Hashed %.2f %s in %.2f secs: ", ddata, metric, secs);
 
254
  printf ("%.2f %s/sec\n", dspeed, metric);
 
255
#if defined(_WIN32)
 
256
leave:
 
257
  W32_ALARM_CLEANUP;
 
258
#endif
142
259
  free (_key);
143
260
}
144
261
 
145
262
int
146
 
main (void)
 
263
main (int argc, char **argv)
147
264
{
 
265
  int debug_level = 0;
 
266
 
 
267
  if (argc > 1)
 
268
    debug_level = 2;
 
269
 
 
270
#if !defined(_WIN32)
 
271
  signal (SIGALRM, alarm_handler);
 
272
#endif
 
273
 
 
274
  gnutls_global_set_log_function (tls_log_func);
 
275
  gnutls_global_set_log_level (debug_level);
 
276
  gnutls_global_init ();
 
277
 
148
278
  mac_bench (GNUTLS_MAC_SHA1, 4);
149
279
  mac_bench (GNUTLS_MAC_SHA1, 8);
150
280
  mac_bench (GNUTLS_MAC_SHA1, 16);