glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / util / perf_crypto_hash.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2002, 2003, 2004, 2006 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14 */
15
16 /**
17  * @author Christian Grothoff
18  * @file util/perf_crypto_hash.c
19  * @brief measure performance of hash function
20  */
21 #include "platform.h"
22 #include "gnunet_util_lib.h"
23 #include <gauger.h>
24 #include <gcrypt.h>
25
26
27 static void
28 perfHash ()
29 {
30   struct GNUNET_HashCode hc;
31   unsigned int i;
32   char buf[64 * 1024];
33
34   memset (buf, 1, sizeof (buf));
35   for (i = 0; i < 1024; i++)
36     GNUNET_CRYPTO_hash (buf, sizeof (buf), &hc);
37 }
38
39
40 static void
41 perfHashSmall ()
42 {
43   struct GNUNET_HashCode hc;
44   unsigned int i;
45   char buf[64];
46
47   memset (buf, 1, sizeof (buf));
48   for (i = 0; i < 1024; i++)
49     GNUNET_CRYPTO_hash (buf, sizeof (buf), &hc);
50 }
51
52
53 static void
54 perfHKDF ()
55 {
56   unsigned int i;
57   char res[128];
58   char buf[128];
59   char skm[64];
60
61   memset (buf, 1, sizeof (buf));
62   memset (skm, 2, sizeof (skm));
63   for (i = 0; i < 1024; i++)
64     GNUNET_CRYPTO_hkdf (res, sizeof (res),
65                         GCRY_MD_SHA512, GCRY_MD_SHA256,
66                         buf, sizeof (buf),
67                         skm, sizeof (skm),
68                         "test", (size_t) 4,
69                         NULL, 0);
70 }
71
72
73 int
74 main (int argc, char *argv[])
75 {
76   struct GNUNET_TIME_Absolute start;
77
78   start = GNUNET_TIME_absolute_get ();
79   perfHashSmall ();
80   printf ("1024x 64-byte Hash perf took %s\n",
81           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
82                                                   GNUNET_YES));
83
84   start = GNUNET_TIME_absolute_get ();
85   perfHash ();
86   printf ("1024x 64k Hash perf took %s\n",
87           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
88                                                   GNUNET_YES));
89   GAUGER ("UTIL", "Cryptographic hashing",
90           64 * 1024 / (1 +
91                        GNUNET_TIME_absolute_get_duration
92                        (start).rel_value_us / 1000LL), "kb/ms");
93   start = GNUNET_TIME_absolute_get ();
94   perfHKDF ();
95   printf ("HKDF perf took %s\n",
96           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
97                                                   GNUNET_YES));
98   GAUGER ("UTIL", "Cryptographic HKDF",
99           64 * 1024 / (1 +
100                        GNUNET_TIME_absolute_get_duration
101                        (start).rel_value_us / 1000LL), "kb/ms");
102   return 0;
103 }
104
105 /* end of perf_crypto_hash.c */