2 * Copyright (C) 2008 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
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 and/or 3 of the License.
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.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #define ENSC_TESTSUITE
23 #include <lib_internal/crypto-wrapper.h>
24 #include <lib_internal/coreassert.h>
30 do_benchmark(char const *meth_name)
35 } const DATA_SIZES[] = {
47 ensc_hash_method const *m = ensc_crypto_hash_find(meth_name);
48 ensc_hash_context ctx;
49 size_t d_len = m ? ensc_crypto_hash_get_digestsize(m) : 0;
55 assert(ensc_crypto_hashctx_init(&ctx, m)==0);
57 for (i=0; i<sizeof(DATA_SIZES)/sizeof(DATA_SIZES[0]); ++i) {
58 size_t cnt = DATA_SIZES[i].blocks;
59 size_t const bs = DATA_SIZES[i].block_size;
60 struct timespec tm_start, tm_end, delta;
63 buf = malloc(bs+1); /* avoid malloc-0 confusions */
66 memset(buf, 0x11, bs);
68 ensc_crypto_hashctx_reset(&ctx);
70 /* benchmarked code starts here... */
71 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tm_start);
73 ensc_crypto_hashctx_update(&ctx, buf, bs);
75 ensc_crypto_hashctx_get_digest(&ctx, digest, NULL, d_len);
76 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tm_end);
77 /* ... and ends here */
79 delta.tv_sec = tm_end.tv_sec - tm_start.tv_sec;
80 if (tm_end.tv_nsec < tm_start.tv_nsec) {
82 tm_end.tv_nsec += 1000000000l;
84 delta.tv_nsec = tm_end.tv_nsec - tm_start.tv_nsec;
86 if (delta.tv_nsec==0 && delta.tv_sec==0)
89 bps = (uint64_t)(DATA_SIZES[i].blocks) * bs * 1000000000;
90 bps /= (uint64_t)(delta.tv_sec) * 1000000000 + delta.tv_nsec;
92 printf("%6s: %7zu x %-7zu -> %2lu.%09lus, %'15llu bytes/s\n",
93 meth_name, DATA_SIZES[i].blocks, bs,
94 delta.tv_sec, delta.tv_nsec, (unsigned long long)(bps));
97 ensc_crypto_hashctx_free(&ctx);
102 char const * const METHS[] = {
103 "md5", "sha1", "sha256", "sha512", NULL
105 char const * const * meth;
108 setlocale(LC_NUMERIC, ""); /* needed for the thousands grouping */
110 for (meth=METHS+0; *meth; ++meth)