From c50874c07b84feb80da6554fcee33f5493a96c94 Mon Sep 17 00:00:00 2001 From: Alfred Klomp Date: Mon, 8 Jan 2024 22:59:57 +0100 Subject: [PATCH 1/3] Benchmark: Windows: get precise timestamp --- test/benchmark.c | 55 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/test/benchmark.c b/test/benchmark.c index 80d21a38..7b26b7de 100644 --- a/test/benchmark.c +++ b/test/benchmark.c @@ -8,17 +8,24 @@ #define _XOPEN_SOURCE 600 #endif +// Standard cross-platform includes. #include -#include -#include -#include -#include #include #include -#include -#ifdef __MACH__ -#include +// Platform-specific includes. +#if defined(_WIN32) || defined(_WIN64) +# include +#else +# include +# include +# include +# include +# include +#endif + +#if defined(__MACH__) +# include #endif #include "../include/libbase64.h" @@ -84,12 +91,13 @@ get_random_data (struct buffers *b, char **errmsg) return true; } -#ifdef __MACH__ +#if defined(__MACH__) typedef uint64_t base64_timespec; + static void -base64_gettime (base64_timespec * o_time) +base64_gettime (base64_timespec *t) { - *o_time = mach_absolute_time(); + *t = mach_absolute_time(); } static float @@ -101,18 +109,39 @@ timediff_sec (base64_timespec *start, base64_timespec *end) return (float)((diff * tb.numer) / tb.denom) / 1e9f; } +#elif defined(_WIN32) || defined(_WIN64) +typedef ULARGE_INTEGER base64_timespec; + +static void +base64_gettime (base64_timespec *t) +{ + FILETIME current_time_ft; + + GetSystemTimePreciseAsFileTime(¤t_time_ft); + + t->LowPart = current_time_ft.dwLowDateTime; + t->HighPart = current_time_ft.dwHighDateTime; +} + +static float +timediff_sec (base64_timespec *start, base64_timespec *end) +{ + // Timer resolution is 100 nanoseconds (10^-7 sec). + return (end->QuadPart - start->QuadPart) / 1e7f; +} #else typedef struct timespec base64_timespec; + static void -base64_gettime (base64_timespec * o_time) +base64_gettime (base64_timespec *t) { - clock_gettime(CLOCK_REALTIME, o_time); + clock_gettime(CLOCK_REALTIME, t); } static float timediff_sec (base64_timespec *start, base64_timespec *end) { - return (end->tv_sec - start->tv_sec) + ((float)(end->tv_nsec - start->tv_nsec)) / 1e9f; + return (end->tv_sec - start->tv_sec) + (end->tv_nsec - start->tv_nsec) / 1e9f; } #endif From ecebc7c25cfb3b48ae9eac0ded5b28a885a6f55f Mon Sep 17 00:00:00 2001 From: Alfred Klomp Date: Mon, 8 Jan 2024 23:06:39 +0100 Subject: [PATCH 2/3] Benchmark: Windows: collect random data --- test/benchmark.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/benchmark.c b/test/benchmark.c index 7b26b7de..e78b696b 100644 --- a/test/benchmark.c +++ b/test/benchmark.c @@ -16,6 +16,7 @@ // Platform-specific includes. #if defined(_WIN32) || defined(_WIN64) # include +# include #else # include # include @@ -67,6 +68,27 @@ bytes_to_mb (size_t bytes) static bool get_random_data (struct buffers *b, char **errmsg) { +#if defined(_WIN32) || defined(_WIN64) + HCRYPTPROV hProvider = 0; + + if (!CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) { + *errmsg = "Error: CryptAcquireContext"; + return false; + } + + if (!CryptGenRandom(hProvider, b->regsz, b->reg)) { + CryptReleaseContext(hProvider, 0); + *errmsg = "Error: CryptGenRandom"; + return false; + } + + if (!CryptReleaseContext(hProvider, 0)) { + *errmsg = "Error: CryptReleaseContext"; + return false; + } + + return true; +#else int fd; ssize_t nread; size_t total_read = 0; @@ -87,8 +109,10 @@ get_random_data (struct buffers *b, char **errmsg) } total_read += nread; } + close(fd); return true; +#endif } #if defined(__MACH__) From 54e3cbc81611907c749cf78316446ea18b851116 Mon Sep 17 00:00:00 2001 From: Alfred Klomp Date: Mon, 8 Jan 2024 23:32:40 +0100 Subject: [PATCH 3/3] CI: benchmark: run on Windows targets through CTest The benchmark program now compiles on Windows, so run it when CTest is invoked, just like on other platforms. --- test/CMakeLists.txt | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ef878704..f07b65a0 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -32,12 +32,10 @@ add_base64_test(test_base64 test_base64.c ) -if (NOT WIN32) - add_base64_test(benchmark - codec_supported.c - benchmark.c - ) -endif() +add_base64_test(benchmark + codec_supported.c + benchmark.c +) if(CMAKE_SYSTEM_NAME STREQUAL "Linux") target_link_libraries(benchmark PRIVATE rt)