MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
benchmark.cpp
1 /*
2  Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; see the file COPYING. If not, write to the
15  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16  MA 02110-1301 USA.
17 */
18 
19 // benchmark.cpp
20 // TaoCrypt benchmark
21 
22 #include <string.h>
23 #include <stdio.h>
24 
25 #include "runtime.hpp"
26 #include "des.hpp"
27 #include "aes.hpp"
28 #include "twofish.hpp"
29 #include "blowfish.hpp"
30 #include "arc4.hpp"
31 #include "md5.hpp"
32 #include "sha.hpp"
33 #include "ripemd.hpp"
34 #include "rsa.hpp"
35 #include "dh.hpp"
36 #include "dsa.hpp"
37 
38 
39 using namespace TaoCrypt;
40 
41 void bench_aes(bool show);
42 void bench_des();
43 void bench_blowfish();
44 void bench_twofish();
45 void bench_arc4();
46 
47 void bench_md5();
48 void bench_sha();
49 void bench_ripemd();
50 
51 void bench_rsa();
52 void bench_dh();
53 void bench_dsa();
54 
55 double current_time();
56 
57 
58 
59 
60 int main(int argc, char** argv)
61 {
62  bench_aes(false);
63  bench_aes(true);
64  bench_blowfish();
65  bench_twofish();
66  bench_arc4();
67  bench_des();
68 
69  printf("\n");
70 
71  bench_md5();
72  bench_sha();
73  bench_ripemd();
74 
75  printf("\n");
76 
77  bench_rsa();
78  bench_dh();
79  bench_dsa();
80 
81  return 0;
82 }
83 
84 const int megs = 5; // how much to test
85 
86 const byte key[] =
87 {
88  0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
89  0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
90  0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
91 };
92 
93 const byte iv[] =
94 {
95  0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
96  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
97  0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
98 
99 };
100 
101 
102 byte plain [1024*1024];
103 byte cipher[1024*1024];
104 
105 
106 void bench_des()
107 {
109  enc.SetKey(key, 16, iv);
110 
111  double start = current_time();
112 
113  for(int i = 0; i < megs; i++)
114  enc.Process(plain, cipher, sizeof(plain));
115 
116  double total = current_time() - start;
117 
118  double persec = 1 / total * megs;
119 
120  printf("3DES %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
121  persec);
122 }
123 
124 
125 void bench_aes(bool show)
126 {
127  AES_CBC_Encryption enc;
128  enc.SetKey(key, 16, iv);
129 
130  double start = current_time();
131 
132  for(int i = 0; i < megs; i++)
133  enc.Process(plain, cipher, sizeof(plain));
134 
135  double total = current_time() - start;
136 
137  double persec = 1 / total * megs;
138 
139  if (show)
140  printf("AES %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
141  persec);
142 }
143 
144 
145 void bench_twofish()
146 {
148  enc.SetKey(key, 16, iv);
149 
150  double start = current_time();
151 
152  for(int i = 0; i < megs; i++)
153  enc.Process(plain, cipher, sizeof(plain));
154 
155  double total = current_time() - start;
156 
157  double persec = 1 / total * megs;
158 
159  printf("Twofish %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
160  persec);
161 
162 }
163 
164 
165 void bench_blowfish()
166 {
168  enc.SetKey(key, 16, iv);
169 
170  double start = current_time();
171 
172  for(int i = 0; i < megs; i++)
173  enc.Process(plain, cipher, sizeof(plain));
174 
175  double total = current_time() - start;
176 
177  double persec = 1 / total * megs;
178 
179  printf("Blowfish %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
180  persec);
181 }
182 
183 
184 void bench_arc4()
185 {
186  ARC4 enc;
187  enc.SetKey(key, 16);
188 
189  double start = current_time();
190 
191  for(int i = 0; i < megs; i++)
192  enc.Process(cipher, plain, sizeof(plain));
193 
194  double total = current_time() - start;
195 
196  double persec = 1 / total * megs;
197 
198  printf("ARC4 %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
199  persec);
200 }
201 
202 
203 void bench_md5()
204 {
205  MD5 hash;
206  byte digest[MD5::DIGEST_SIZE];
207 
208  double start = current_time();
209 
210 
211  for(int i = 0; i < megs; i++)
212  hash.Update(plain, sizeof(plain));
213 
214  hash.Final(digest);
215 
216  double total = current_time() - start;
217 
218  double persec = 1 / total * megs;
219 
220  printf("MD5 %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
221  persec);
222 }
223 
224 
225 void bench_sha()
226 {
227  SHA hash;
228  byte digest[SHA::DIGEST_SIZE];
229 
230  double start = current_time();
231 
232 
233  for(int i = 0; i < megs; i++)
234  hash.Update(plain, sizeof(plain));
235 
236  hash.Final(digest);
237 
238  /*
239  for(int i = 0; i < megs; i++)
240  hash.AsmTransform(plain, 16384);
241  */
242 
243 
244  double total = current_time() - start;
245 
246  double persec = 1 / total * megs;
247 
248  printf("SHA %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
249  persec);
250 }
251 
252 
253 void bench_ripemd()
254 {
255  RIPEMD160 hash;
256  byte digest[RIPEMD160::DIGEST_SIZE];
257 
258  double start = current_time();
259 
260 
261  for(int i = 0; i < megs; i++)
262  hash.Update(plain, sizeof(plain));
263 
264  hash.Final(digest);
265 
266  double total = current_time() - start;
267 
268  double persec = 1 / total * megs;
269 
270  printf("RIPEMD %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
271  persec);
272 }
273 
275 
276 void bench_rsa()
277 {
278  const int times = 100;
279 
280  Source source;
281  FileSource("./rsa1024.der", source);
282 
283  if (source.size() == 0) {
284  printf("can't find ./rsa1024.der\n");
285  return;
286  }
287  RSA_PrivateKey priv(source);
288  RSAES_Encryptor enc(priv);
289 
290  byte message[] = "Everyone gets Friday off.";
291  byte cipher[128]; // for 1024 bit
292  byte plain[128]; // for 1024 bit
293  const int len = (word32)strlen((char*)message);
294 
295  int i;
296  double start = current_time();
297 
298  for (i = 0; i < times; i++)
299  enc.Encrypt(message, len, cipher, rng);
300 
301  double total = current_time() - start;
302  double each = total / times; // per second
303  double milliEach = each * 1000; // milliseconds
304 
305  printf("RSA 1024 encryption took %6.2f milliseconds, avg over %d"
306  " iterations\n", milliEach, times);
307 
308  RSAES_Decryptor dec(priv);
309 
310  start = current_time();
311 
312  for (i = 0; i < times; i++)
313  dec.Decrypt(cipher, 128, plain, rng);
314 
315  total = current_time() - start;
316  each = total / times; // per second
317  milliEach = each * 1000; // milliseconds
318 
319  printf("RSA 1024 decryption took %6.2f milliseconds, avg over %d"
320  " iterations\n", milliEach, times);
321 }
322 
323 
324 void bench_dh()
325 {
326  const int times = 100;
327 
328  Source source;
329  FileSource("./dh1024.der", source);
330 
331  if (source.size() == 0) {
332  printf("can't find ./dh1024.der\n");
333  return;
334  }
335  DH dh(source);
336 
337  byte pub[128]; // for 1024 bit
338  byte priv[128]; // for 1024 bit
339 
340  int i;
341  double start = current_time();
342 
343  for (i = 0; i < times; i++)
344  dh.GenerateKeyPair(rng, priv, pub);
345 
346  double total = current_time() - start;
347  double each = total / times; // per second
348  double milliEach = each * 1000; // milliseconds
349 
350  printf("DH 1024 key generation %6.2f milliseconds, avg over %d"
351  " iterations\n", milliEach, times);
352 
353  DH dh2(dh);
354  byte pub2[128]; // for 1024 bit
355  byte priv2[128]; // for 1024 bit
356  dh2.GenerateKeyPair(rng, priv2, pub2);
357  unsigned char key[256];
358 
359  start = current_time();
360 
361  for (i = 0; i < times; i++)
362  dh.Agree(key, priv, pub2);
363 
364  total = current_time() - start;
365  each = total / times; // per second
366  milliEach = each * 1000; // in milliseconds
367 
368  printf("DH 1024 key agreement %6.2f milliseconds, avg over %d"
369  " iterations\n", milliEach, times);
370 }
371 
372 void bench_dsa()
373 {
374  const int times = 100;
375 
376  Source source;
377  FileSource("./dsa1024.der", source);
378 
379  if (source.size() == 0) {
380  printf("can't find ./dsa1024.der\n");
381  return;
382  }
383 
384  DSA_PrivateKey key(source);
385  DSA_Signer signer(key);
386 
387  SHA sha;
388  byte digest[SHA::DIGEST_SIZE];
389  byte signature[40];
390  const char msg[] = "this is the message";
391  sha.Update((byte*)msg, sizeof(msg));
392  sha.Final(digest);
393 
394  int i;
395  double start = current_time();
396 
397  for (i = 0; i < times; i++)
398  signer.Sign(digest, signature, rng);
399 
400  double total = current_time() - start;
401  double each = total / times; // per second
402  double milliEach = each * 1000; // milliseconds
403 
404  printf("DSA 1024 sign took %6.2f milliseconds, avg over %d"
405  " iterations\n", milliEach, times);
406 
407  DSA_Verifier verifier(key);
408 
409  start = current_time();
410 
411  for (i = 0; i < times; i++)
412  verifier.Verify(digest, signature);
413 
414  total = current_time() - start;
415  each = total / times; // per second
416  milliEach = each * 1000; // in milliseconds
417 
418  printf("DSA 1024 verify took %6.2f milliseconds, avg over %d"
419  " iterations\n", milliEach, times);
420 }
421 
422 
423 
424 #ifdef _WIN32
425 
426  #define WIN32_LEAN_AND_MEAN
427  #include <windows.h>
428 
429  double current_time()
430  {
431  static bool init(false);
432  static LARGE_INTEGER freq;
433 
434  if (!init) {
435  QueryPerformanceFrequency(&freq);
436  init = true;
437  }
438 
439  LARGE_INTEGER count;
440  QueryPerformanceCounter(&count);
441 
442  return static_cast<double>(count.QuadPart) / freq.QuadPart;
443  }
444 
445 #else
446 
447  #include <sys/time.h>
448 
449  double current_time()
450  {
451  struct timeval tv;
452  gettimeofday(&tv, 0);
453 
454  return static_cast<double>(tv.tv_sec)
455  + static_cast<double>(tv.tv_usec) / 1000000;
456  }
457 
458 #endif // _WIN32