Skip to content

Commit a99f450

Browse files
committed
Slightly improve timing demo.
* Add the option to only run for a subset of algos. * Improve `hash` to show something meaningful. Signed-off-by: Steffen Jaeckel <[email protected]>
1 parent e3460ad commit a99f450

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

demos/timing.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ static prng_state yarrow_prng;
1414
#define KTIMES 25
1515
#define TIMES 100000
1616

17+
static const char *filter_arg;
18+
1719
static struct list {
1820
int id;
1921
ulong64 spd1, spd2, avg;
@@ -56,7 +58,7 @@ static void tally_results(int type)
5658
}
5759

5860
/* RDTSC from Scott Duplichan */
59-
static ulong64 rdtsc (void)
61+
static LTC_INLINE ulong64 rdtsc (void)
6062
{
6163
#if defined __GNUC__ && !defined(LTC_NO_ASM)
6264
#if defined(__i386__) || defined(__x86_64__)
@@ -111,12 +113,12 @@ static ulong64 rdtsc (void)
111113

112114
static ulong64 timer, skew = 0;
113115

114-
static void t_start(void)
116+
static LTC_INLINE void t_start(void)
115117
{
116118
timer = rdtsc();
117119
}
118120

119-
static ulong64 t_read(void)
121+
static LTC_INLINE ulong64 t_read(void)
120122
{
121123
return rdtsc() - timer;
122124
}
@@ -470,17 +472,23 @@ static void time_cipher_lrw(void) { fprintf(stderr, "NO LRW\n"); }
470472

471473
static void time_hash(void)
472474
{
473-
unsigned long x, y1, len;
475+
unsigned long x, y1, len = 1024;
474476
ulong64 t1, t2, c1, c2;
475477
hash_state md;
476478
int (*func)(hash_state *, const unsigned char *, unsigned long), err;
477-
unsigned char pt[MAXBLOCKSIZE] = { 0 };
478-
479+
unsigned char *pt = XMALLOC(len);
480+
if (pt == NULL) {
481+
fprintf(stderr, "\n\nout of heap yo\n\n");
482+
exit(EXIT_FAILURE);
483+
}
479484

480485
fprintf(stderr, "\n\nHASH Time Trials for:\n");
481486
no_results = 0;
482487
for (x = 0; hash_descriptor[x].name != NULL; x++) {
483488

489+
if (filter_arg && strstr(hash_descriptor[x].name, filter_arg) == NULL)
490+
continue;
491+
484492
/* sanity check on hash */
485493
if ((err = hash_descriptor[x].test()) != CRYPT_OK) {
486494
fprintf(stderr, "\n\nERROR: Hash %s failed self-test %s\n", hash_descriptor[x].name, error_to_string(err));
@@ -493,7 +501,6 @@ static void time_hash(void)
493501
#define DO2 DO1 DO1
494502

495503
func = hash_descriptor[x].process;
496-
len = hash_descriptor[x].blocksize;
497504

498505
c1 = c2 = (ulong64)-1;
499506
for (y1 = 0; y1 < TIMES; y1++) {
@@ -1368,12 +1375,15 @@ static void LTC_NORETURN die(int status)
13681375
{
13691376
FILE* o = status == EXIT_SUCCESS ? stdout : stderr;
13701377
fprintf(o,
1371-
"Usage: timing [<-h|-l|alg>] [mpi]\n\n"
1378+
"Usage: timing [<-h|-l|alg>] [mpi] [filter]\n\n"
13721379
"Run timing tests of all built-in algorithms, or only the one given in <alg>.\n\n"
1373-
"\talg\tThe algorithm to test. Use the '-l' option to check for valid values.\n"
1380+
"\talg\tThe algorithms to test. Use the '-l' option to check for valid values.\n"
13741381
"\tmpi\tThe MPI provider to use.\n"
1382+
"\tfilter\tFilter within the algorithm class (currently only for 'hash'es).\n"
13751383
"\t-l\tList all built-in algorithms that can be timed.\n"
1376-
"\t-h\tThe help you're looking at.\n"
1384+
"\t-h\tThe help you're looking at.\n\n"
1385+
"Examples:\n"
1386+
"\ttiming hash sha\t\tWill run the timing demo for all hashes containing 'sha' in their name\n"
13771387
);
13781388
exit(status);
13791389
}
@@ -1440,6 +1450,9 @@ register_all_prngs();
14401450

14411451
if (crypt_mp_init(mpi_provider) != CRYPT_OK) {
14421452
fprintf(stderr, "Init of MPI provider \"%s\" failed\n", mpi_provider ? mpi_provider : "(null)");
1453+
filter_arg = mpi_provider;
1454+
} else if (argc > 3){
1455+
filter_arg = argv[3];
14431456
}
14441457

14451458
if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) {

0 commit comments

Comments
 (0)