Skip to content

Commit

Permalink
#6: benchmark: compile on Windows
Browse files Browse the repository at this point in the history
Add compatibility shims to be able to compile the benchmark program on
Windows.

Resolves #6.
  • Loading branch information
aklomp committed Jan 9, 2024
2 parents f5ed26f + 54e3cbc commit ce31662
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 19 deletions.
10 changes: 4 additions & 6 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
79 changes: 66 additions & 13 deletions test/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@
#define _XOPEN_SOURCE 600
#endif

// Standard cross-platform includes.
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#ifdef __MACH__
#include <mach/mach_time.h>
// Platform-specific includes.
#if defined(_WIN32) || defined(_WIN64)
# include <windows.h>
# include <wincrypt.h>
#else
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <unistd.h>
# include <time.h>
#endif

#if defined(__MACH__)
# include <mach/mach_time.h>
#endif

#include "../include/libbase64.h"
Expand Down Expand Up @@ -60,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;
Expand All @@ -80,16 +109,19 @@ get_random_data (struct buffers *b, char **errmsg)
}
total_read += nread;
}

close(fd);
return true;
#endif
}

#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
Expand All @@ -101,18 +133,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(&current_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

Expand Down

0 comments on commit ce31662

Please sign in to comment.