Feat: Start to add benchmarking

This commit is contained in:
Dorian Zedler 2022-07-06 20:35:10 +02:00
parent f96fed1ebd
commit 047ad315da
Signed by: dorian
GPG key ID: 989DE36109AFA354
3 changed files with 40 additions and 7 deletions

View file

@ -1,11 +1,15 @@
#ifndef HELPER_H #ifndef HELPER_H
#define HELPER_H #define HELPER_H
#define _POSIX_C_SOURCE 200809L
#include <errno.h> #include <errno.h>
#include <getopt.h> #include <getopt.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
#include "md2.h"
enum argumentParseResult { enum argumentParseResult {
RESULT_OK = 0, RESULT_OK = 0,
@ -31,4 +35,16 @@ struct configuration {
enum argumentParseResult parseArguments(int argc, char** argv, enum argumentParseResult parseArguments(int argc, char** argv,
struct configuration* c); struct configuration* c);
/**
* @brief Run an md2_hash_func with benchmark timing
*
* @param func the function to run
* @param len the lenght of buf
* @param buf the data to hash
* @param out the target array for the hash
* @return double
*/
double run_benchmark(md2_hash_func func, size_t len, uint8_t* buf,
uint8_t* out);
#endif // HELPER_H #endif // HELPER_H

View file

@ -38,7 +38,7 @@ enum argumentParseResult parseArguments(int argc, char **argv,
while (1) { while (1) {
static struct option longOptions[] = {{"help", no_argument, NULL, 'h'}, static struct option longOptions[] = {{"help", no_argument, NULL, 'h'},
NULL}; {NULL}};
int longOptionIndex = 0; int longOptionIndex = 0;
opt = getopt_long(argc, argv, "V:B::h", longOptions, &longOptionIndex); opt = getopt_long(argc, argv, "V:B::h", longOptions, &longOptionIndex);
@ -85,4 +85,19 @@ enum argumentParseResult parseArguments(int argc, char **argv,
} }
return RESULT_OK; return RESULT_OK;
}
double current_time(void) {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return t.tv_sec + t.tv_nsec * 1e-9;
}
double run_benchmark(md2_hash_func func, size_t len, uint8_t *buf,
uint8_t *out) {
double start = current_time();
func(len, buf, out);
double end = current_time();
return end - start;
} }

View file

@ -7,14 +7,15 @@
// Returns true when val is approx. equal to exp. // Returns true when val is approx. equal to exp.
static bool runTest(const char* message, const char* expectedHash) { static bool runTest(const char* message, const char* expectedHash) {
uint8_t out[16]; uint8_t out[16];
md2_hash(strlen(message), message, out); double duration =
run_benchmark(md2_hash, strlen(message), (uint8_t*)message, out);
char hash[32]; char hash[32];
md2_encode_hash(out, hash); md2_encode_hash(out, hash);
bool ok = !strcmp(hash, expectedHash); bool ok = !strncmp(hash, expectedHash, 32);
printf("%s: md2(%s) %s == %s\n", "not ok" + (4 * ok), message, hash, printf("%s: md2(%s) %s == %s, took: %f\n", "not ok" + (4 * ok), message, hash,
expectedHash); expectedHash, duration);
return ok; return ok;
} }
@ -85,12 +86,13 @@ int main(int argc, char** argv) {
printf("\n"); printf("\n");
uint8_t out[16]; uint8_t out[16];
md2_hash(len, data, out); double duration = run_benchmark(md2_hash, len, data, out);
printf("Hash: "); printf("Hash: ");
char hash[32]; char hash[32];
md2_encode_hash(out, hash); md2_encode_hash(out, hash);
printf("%s\n", hash);
printf("%s, took: %f\n", hash, duration);
free(data); free(data);