From 5821ca70e0209cf1aae51c713377f1a9dfdb6c98 Mon Sep 17 00:00:00 2001 From: Loup Vaillant Date: Tue, 2 Jan 2018 23:59:05 +0100 Subject: [PATCH] Factored speed tests --- makefile | 16 ++++++----- tests/speed-sodium.c | 67 +------------------------------------------- tests/speed.c | 67 +------------------------------------------- tests/speed.h | 65 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 139 deletions(-) create mode 100644 tests/speed.h diff --git a/makefile b/makefile index 3b2b1a1..7b4000d 100644 --- a/makefile +++ b/makefile @@ -80,19 +80,21 @@ lib/monocypher.o lib/sha512.o: # Test & speed libraries $TEST_COMMON=tests/utils.h src/monocypher.h src/optional/sha512.h -lib/utils.o: tests/utils.c tests/utils.h -lib/test.o : tests/test.c $(TEST_COMMON) tests/vectors.h -lib/speed.o: tests/speed.c $(TEST_COMMON) -lib/utils.o lib/test.o lib/speed.o: +lib/utils.o : tests/utils.c tests/utils.h +lib/test.o : tests/test.c $(TEST_COMMON) tests/vectors.h +lib/speed.o : tests/speed.c $(TEST_COMMON) tests/speed.h +lib/speed-sodium.o: tests/speed-sodium.c $(TEST_COMMON) tests/speed.h +lib/utils.o lib/test.o lib/speed.o lib/speed-sodium.o: @mkdir -p $(@D) $(CC) $(CFLAGS) -I src -I src/optional -fPIC -c -o $@ $< # test & speed executables -test.out : lib/test.o lib/monocypher.o lib/sha512.o lib/utils.o -speed.out: lib/speed.o lib/monocypher.o lib/sha512.o lib/utils.o +test.out : lib/test.o lib/utils.o lib/monocypher.o lib/sha512.o +speed.out : lib/speed.o lib/utils.o lib/monocypher.o lib/sha512.o +speed-sodium.out: lib/speed-sodium.o lib/utils.o test.out speed.out: $(CC) $(CFLAGS) -I src -I src/optional -o $@ $^ -speed-sodium.out: tests/speed-sodium.c lib/utils.o +speed-sodium.out: lib/speed-sodium.o lib/utils.o $(CC) $(CFLAGS) -I src -I src/optional -o $@ $^ \ $$(pkg-config --cflags libsodium) \ $$(pkg-config --libs libsodium) diff --git a/tests/speed-sodium.c b/tests/speed-sodium.c index 97e1747..4e188cf 100644 --- a/tests/speed-sodium.c +++ b/tests/speed-sodium.c @@ -1,70 +1,5 @@ -#include -#include -#include -#include +#include "speed.h" #include "sodium.h" -#include "utils.h" - -typedef struct timespec timespec; - -// TODO: provide a user defined buffer size -#define KILOBYTE 1024 -#define MEGABYTE (1024 * KILOBYTE) -#define SIZE (50 * MEGABYTE) -#define MULT (SIZE / MEGABYTE) - -timespec diff(timespec start, timespec end) -{ - timespec duration; - duration.tv_sec = end.tv_sec - start.tv_sec; - duration.tv_nsec = end.tv_nsec - start.tv_nsec; - if (duration.tv_nsec < 0) { - duration.tv_nsec += 1000000000; - duration.tv_sec -= 1; - } - return duration; -} - -timespec min(timespec a, timespec b) -{ - if (a.tv_sec < b.tv_sec || - (a.tv_sec == b.tv_sec && a.tv_nsec < b.tv_nsec)) { - return a; - } - return b; -} - -u64 speed(timespec duration) -{ -#define DIV 1000 // avoid round errors - static const u64 giga = 1000000000; - return DIV * giga / (duration.tv_nsec + duration.tv_sec * giga); -} - -static void print(const char *name, u64 speed, const char *unit) -{ - printf("%s: %5" PRIu64 " %s\n", name, speed, unit); -} - -// TODO: adjust this crap -#define TIMESTAMP(t) \ - timespec t; \ - clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t) - -#define TIMING_START \ - timespec duration; \ - duration.tv_sec = -1; \ - duration.tv_nsec = -1; \ - duration.tv_sec = 3600 * 24; \ - duration.tv_nsec = 0; \ - FOR (i, 0, 10) { \ - TIMESTAMP(start); - -#define TIMING_END \ - TIMESTAMP(end); \ - duration = min(duration, diff(start, end)); \ - } /* end FOR*/ \ - return speed(duration) static u64 chacha20(void) { diff --git a/tests/speed.c b/tests/speed.c index 787f8e8..cd9eb5b 100644 --- a/tests/speed.c +++ b/tests/speed.c @@ -1,73 +1,8 @@ -#include -#include -#include -#include +#include "speed.h" #include "monocypher.h" #include "sha512.h" #include "utils.h" -typedef struct timespec timespec; - -// TODO: provide a user defined buffer size -#define KILOBYTE 1024 -#define MEGABYTE (1024 * KILOBYTE) -#define SIZE (50 * MEGABYTE) -#define MULT (SIZE / MEGABYTE) - -timespec diff(timespec start, timespec end) -{ - timespec duration; - duration.tv_sec = end.tv_sec - start.tv_sec; - duration.tv_nsec = end.tv_nsec - start.tv_nsec; - if (duration.tv_nsec < 0) { - duration.tv_nsec += 1000000000; - duration.tv_sec -= 1; - } - return duration; -} - -timespec min(timespec a, timespec b) -{ - if (a.tv_sec < b.tv_sec || - (a.tv_sec == b.tv_sec && a.tv_nsec < b.tv_nsec)) { - return a; - } - return b; -} - -u64 speed(timespec duration) -{ -#define DIV 1000 // avoid round errors - static const u64 giga = 1000000000; - return DIV * giga / (duration.tv_nsec + duration.tv_sec * giga); -} - -static void print(const char *name, u64 speed, const char *unit) -{ - printf("%s: %4" PRIu64 " %s\n", name, speed, unit); -} - -// TODO: adjust this crap -#define TIMESTAMP(t) \ - timespec t; \ - clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t) - -#define TIMING_START \ - timespec duration; \ - duration.tv_sec = -1; \ - duration.tv_nsec = -1; \ - duration.tv_sec = 3600 * 24; \ - duration.tv_nsec = 0; \ - FOR (i, 0, 10) { \ - TIMESTAMP(start); - -#define TIMING_END \ - TIMESTAMP(end); \ - duration = min(duration, diff(start, end)); \ - } /* end FOR*/ \ - return speed(duration) - - static u64 chacha20(void) { static u8 in [SIZE]; p_random(in , SIZE); diff --git a/tests/speed.h b/tests/speed.h new file mode 100644 index 0000000..d91c2bf --- /dev/null +++ b/tests/speed.h @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include "utils.h" + +typedef struct timespec timespec; + +// TODO: provide a user defined buffer size +#define KILOBYTE 1024 +#define MEGABYTE (1024 * KILOBYTE) +#define SIZE (50 * MEGABYTE) +#define MULT (SIZE / MEGABYTE) + +static timespec diff(timespec start, timespec end) +{ + timespec duration; + duration.tv_sec = end.tv_sec - start.tv_sec; + duration.tv_nsec = end.tv_nsec - start.tv_nsec; + if (duration.tv_nsec < 0) { + duration.tv_nsec += 1000000000; + duration.tv_sec -= 1; + } + return duration; +} + +static timespec min(timespec a, timespec b) +{ + if (a.tv_sec < b.tv_sec || + (a.tv_sec == b.tv_sec && a.tv_nsec < b.tv_nsec)) { + return a; + } + return b; +} + +static u64 speed(timespec duration) +{ +#define DIV 1000 // avoid round errors + static const u64 giga = 1000000000; + return DIV * giga / (duration.tv_nsec + duration.tv_sec * giga); +} + +static void print(const char *name, u64 speed, const char *unit) +{ + printf("%s: %5" PRIu64 " %s\n", name, speed, unit); +} + +#define TIMESTAMP(t) \ + timespec t; \ + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t) + +#define TIMING_START \ + timespec duration; \ + duration.tv_sec = -1; \ + duration.tv_nsec = -1; \ + duration.tv_sec = 3600 * 24; \ + duration.tv_nsec = 0; \ + FOR (i, 0, 10) { \ + TIMESTAMP(start); + +#define TIMING_END \ + TIMESTAMP(end); \ + duration = min(duration, diff(start, end)); \ + } /* end FOR*/ \ + return speed(duration) -- 2.47.3