From 047ad315da93e8af655f849b5a3c5a5254eac8d4 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Wed, 6 Jul 2022 20:35:10 +0200 Subject: [PATCH] Feat: Start to add benchmarking --- Implementierung/lib/helper.h | 16 ++++++++++++++++ Implementierung/src/helper.c | 17 ++++++++++++++++- Implementierung/src/main.c | 14 ++++++++------ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/Implementierung/lib/helper.h b/Implementierung/lib/helper.h index 90ddbc4..6d5ecfb 100644 --- a/Implementierung/lib/helper.h +++ b/Implementierung/lib/helper.h @@ -1,11 +1,15 @@ #ifndef HELPER_H #define HELPER_H +#define _POSIX_C_SOURCE 200809L #include #include #include #include #include +#include + +#include "md2.h" enum argumentParseResult { RESULT_OK = 0, @@ -31,4 +35,16 @@ struct configuration { enum argumentParseResult parseArguments(int argc, char** argv, 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 \ No newline at end of file diff --git a/Implementierung/src/helper.c b/Implementierung/src/helper.c index f809aa5..a32625a 100644 --- a/Implementierung/src/helper.c +++ b/Implementierung/src/helper.c @@ -38,7 +38,7 @@ enum argumentParseResult parseArguments(int argc, char **argv, while (1) { static struct option longOptions[] = {{"help", no_argument, NULL, 'h'}, - NULL}; + {NULL}}; int longOptionIndex = 0; opt = getopt_long(argc, argv, "V:B::h", longOptions, &longOptionIndex); @@ -85,4 +85,19 @@ enum argumentParseResult parseArguments(int argc, char **argv, } 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; } \ No newline at end of file diff --git a/Implementierung/src/main.c b/Implementierung/src/main.c index c1afb63..8ab1411 100644 --- a/Implementierung/src/main.c +++ b/Implementierung/src/main.c @@ -7,14 +7,15 @@ // Returns true when val is approx. equal to exp. static bool runTest(const char* message, const char* expectedHash) { 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]; md2_encode_hash(out, hash); - bool ok = !strcmp(hash, expectedHash); - printf("%s: md2(%s) %s == %s\n", "not ok" + (4 * ok), message, hash, - expectedHash); + bool ok = !strncmp(hash, expectedHash, 32); + printf("%s: md2(%s) %s == %s, took: %f\n", "not ok" + (4 * ok), message, hash, + expectedHash, duration); return ok; } @@ -85,12 +86,13 @@ int main(int argc, char** argv) { printf("\n"); uint8_t out[16]; - md2_hash(len, data, out); + double duration = run_benchmark(md2_hash, len, data, out); printf("Hash: "); char hash[32]; md2_encode_hash(out, hash); - printf("%s\n", hash); + + printf("%s, took: %f\n", hash, duration); free(data);