diff --git a/tests/bench_sys_base64/Makefile b/tests/bench_sys_base64/Makefile new file mode 100644 index 0000000000..98993c7b3c --- /dev/null +++ b/tests/bench_sys_base64/Makefile @@ -0,0 +1,7 @@ +include ../Makefile.tests_common + +USEMODULE += base64 +USEMODULE += fmt +USEMODULE += xtimer + +include $(RIOTBASE)/Makefile.include diff --git a/tests/bench_sys_base64/main.c b/tests/bench_sys_base64/main.c new file mode 100644 index 0000000000..3c33a7301a --- /dev/null +++ b/tests/bench_sys_base64/main.c @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2020 Otto-von-Guericke-Universität Magdeburg + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief Benchmark for the base64 lib + * + * @author Marian Buschsieweke + * + * @} + */ + +#include +#include + +#include "base64.h" +#include "fmt.h" +#include "xtimer.h" + +#define MIN(a, b) (a < b) ? a : b + +static char buf[128]; + +static const char input[96] = "This is an extremely, enormously, greatly, " + "immensely, tremendously, remarkably lengthy " + "sentence!"; +static const char base64[128] = +"VGhpcyBpcyBhbiBleHRyZW1lbHksIGVub3Jtb3VzbHksIGdyZWF0bHksIGltbWVuc2VseSwgdHJl" +"bWVuZG91c2x5LCByZW1hcmthYmx5IGxlbmd0aHkgc2VudGVuY2Uh"; + +int main(void) { + uint32_t start, stop; + size_t size; + + /* We don't want check return value in the benchmark loop, so we just do + * a simple self test now. */ + print_str("Verifying that base64 encoding works for benchmark input: "); + size = sizeof(buf); + if ((BASE64_SUCCESS != base64_encode(input, sizeof(input), buf, &size)) || + (size != sizeof(buf)) || + (0 != memcmp(base64, buf, sizeof(base64)))) + { + print_str("FAIL\nGot: \""); + print(buf, MIN(size, sizeof(base64))); + print_str("\"\nExpected: \""); + print(base64, sizeof(base64)); + print_str("\"\n"); + } + else { + print_str("OK\n"); + } + + print_str("Verifying that base64 decoding works for benchmark input: "); + size = sizeof(buf); + if ((BASE64_SUCCESS != base64_decode(base64, sizeof(base64), buf, &size)) || + (size != sizeof(input)) || + (0 != memcmp(input, buf, sizeof(input)))) + { + print_str("FAIL\nGot: \""); + print(buf, MIN(size, sizeof(input))); + print_str("\"\nExpected: \""); + print(input, sizeof(input)); + print_str("\"\n"); + } + else { + print_str("OK\n"); + } + + start = xtimer_now_usec(); + for (unsigned i = 0; i < 10000; i++) { + size = sizeof(buf); + base64_encode(input, sizeof(input), buf, &size); + } + stop = xtimer_now_usec(); + + print_str("Encoding 10.000 x 96 bytes (128 bytes in base64): "); + print_u32_dec(stop - start); + print_str(" µs\n"); + + start = xtimer_now_usec(); + for (unsigned i = 0; i < 10000; i++) { + size = sizeof(buf); + base64_decode(base64, sizeof(base64), buf, &size); + } + stop = xtimer_now_usec(); + + print_str("Decoding 10.000 x 96 bytes (128 bytes in base64): "); + print_u32_dec(stop - start); + print_str(" µs\n"); + return 0; +} diff --git a/tests/bench_sys_base64/tests/01-run.py b/tests/bench_sys_base64/tests/01-run.py new file mode 100755 index 0000000000..7fa6c23f3c --- /dev/null +++ b/tests/bench_sys_base64/tests/01-run.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2020 Otto-von-Guericke-Universität Magdeburg +# +# This file is subject to the terms and conditions of the GNU Lesser +# General Public License v2.1. See the file LICENSE in the top level +# directory for more details. + +import sys +from testrunner import run + + +def testfunc(child): + child.expect_exact("Verifying that base64 encoding works for benchmark input: OK\r\n") + child.expect_exact("Verifying that base64 decoding works for benchmark input: OK\r\n") + child.expect(r"Encoding 10\.000 x 96 bytes \(128 bytes in base64\): [0-9]+ µs\r\n") + child.expect(r"Decoding 10\.000 x 96 bytes \(128 bytes in base64\): [0-9]+ µs\r\n") + + +if __name__ == "__main__": + sys.exit(run(testfunc))