From 1dd6c91e41bd8e293d62671964c44cfd85048426 Mon Sep 17 00:00:00 2001 From: Brando Date: Sun, 4 May 2025 10:33:41 -0700 Subject: [PATCH 1/5] can provide nanosecond precision --- bftest/src/bftest.c | 9 +++++++++ bftest/src/bftest.h | 15 ++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/bftest/src/bftest.c b/bftest/src/bftest.c index 182dbf13..c863d32a 100644 --- a/bftest/src/bftest.c +++ b/bftest/src/bftest.c @@ -159,3 +159,12 @@ void _BFTestLogFlush() { } } +long long __BFTestGetCurrentTimeNS__() { + struct timespec ts; + if (clock_gettime(CLOCK_REALTIME, &ts) == -1) { + return 0; + } + + return (long long) ts.tv_sec * 1000000000LL + ts.tv_nsec; +} + diff --git a/bftest/src/bftest.h b/bftest/src/bftest.h index 61e6bb45..ba99e659 100644 --- a/bftest/src/bftest.h +++ b/bftest/src/bftest.h @@ -11,6 +11,7 @@ #include #include #include +#include /** TEST SUITE **/ // a test suite is the high level function that will call @@ -56,6 +57,8 @@ /** UNIT TEST **/ +long long __BFTestGetCurrentTimeNS__(); + #define BFTEST_UNIT_FUNC(name, repeat, ...) \ int name (void) {\ BFTEST_UNIT_START;\ @@ -84,13 +87,15 @@ #define BFTEST_UNIT_START \ printf("%s - ", __func__);fflush(stdout);\ fflush(stdout);\ - int result = 0; + int result = 0;\ + time_t startTime = __BFTestGetCurrentTimeNS__(); #define BFTEST_UNIT_END \ - if (result == 0) { printf("PASS\n");fflush(stdout); }\ - else {\ - printf("FAIL\n");fflush(stdout);\ - }\ + time_t endTime = __BFTestGetCurrentTimeNS__();\ + if (result == 0) { printf("PASS"); }\ + else { printf("FAIL"); }\ + printf(" ... %ldns\n", (endTime - startTime) / __repeat__);\ + fflush(stdout);\ _BFTestLogFlush();\ return result; From ed4264286845460098d64595f6f0276f48bc3e7f Mon Sep 17 00:00:00 2001 From: Brando Date: Sun, 4 May 2025 11:55:23 -0700 Subject: [PATCH 2/5] good func --- bftest/src/bftest.c | 6 ++++++ bftest/src/bftest.h | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/bftest/src/bftest.c b/bftest/src/bftest.c index c863d32a..718245e7 100644 --- a/bftest/src/bftest.c +++ b/bftest/src/bftest.c @@ -168,3 +168,9 @@ long long __BFTestGetCurrentTimeNS__() { return (long long) ts.tv_sec * 1000000000LL + ts.tv_nsec; } +void __BFTestFormatElapsedTime__(long long elapsedTimeNS, char * buf, size_t bufsize) { + if (!buf) return; + + snprintf(buf, bufsize, "%lld ns", elapsedTimeNS); +} + diff --git a/bftest/src/bftest.h b/bftest/src/bftest.h index ba99e659..e77586b5 100644 --- a/bftest/src/bftest.h +++ b/bftest/src/bftest.h @@ -58,6 +58,7 @@ /** UNIT TEST **/ long long __BFTestGetCurrentTimeNS__(); +void __BFTestFormatElapsedTime__(long long elapsedTimeNS, char * buf, size_t bufsize); #define BFTEST_UNIT_FUNC(name, repeat, ...) \ int name (void) {\ @@ -94,7 +95,9 @@ long long __BFTestGetCurrentTimeNS__(); time_t endTime = __BFTestGetCurrentTimeNS__();\ if (result == 0) { printf("PASS"); }\ else { printf("FAIL"); }\ - printf(" ... %ldns\n", (endTime - startTime) / __repeat__);\ + char buf[64];\ + __BFTestFormatElapsedTime__((endTime - startTime) / __repeat__, buf, 64);\ + printf(" (%s)\n", buf);\ fflush(stdout);\ _BFTestLogFlush();\ return result; From bb0feff1733124556ba8df9994e98b128255ac29 Mon Sep 17 00:00:00 2001 From: Brando Date: Sun, 4 May 2025 12:01:52 -0700 Subject: [PATCH 3/5] fixing makefile --- bflibcpp/makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bflibcpp/makefile b/bflibcpp/makefile index 42911649..fe5f1626 100644 --- a/bflibcpp/makefile +++ b/bflibcpp/makefile @@ -51,8 +51,8 @@ MAIN_FILE = testbench/tests.cpp endif # ($(CONFIG),...) dependencies: - cd ../bflibc/ && make clean && make build && make build CONFIG=debug cd ../bftest/ && make clean && make build && make build CONFIG=debug + cd ../bflibc/ && make clean && make build && make build CONFIG=debug include makefiles/lib.mk LIBS_MAKEFILES_PATH:=$(shell dirname $(CURDIR))/makefiles From 0db78a49b0c039c4a19168184e1bf3180f89aaad Mon Sep 17 00:00:00 2001 From: Brando Date: Sun, 4 May 2025 12:09:22 -0700 Subject: [PATCH 4/5] good units --- bftest/src/bftest.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/bftest/src/bftest.c b/bftest/src/bftest.c index 718245e7..0fbd25c5 100644 --- a/bftest/src/bftest.c +++ b/bftest/src/bftest.c @@ -171,6 +171,33 @@ long long __BFTestGetCurrentTimeNS__() { void __BFTestFormatElapsedTime__(long long elapsedTimeNS, char * buf, size_t bufsize) { if (!buf) return; - snprintf(buf, bufsize, "%lld ns", elapsedTimeNS); + // 0: nano, 1: micro, 2: milli, 3: seconds + int level = 0; + const int maxLevel = 3; + float ns = elapsedTimeNS; + + while (ns > 1000 && level <= maxLevel) { + ns /= 1000; + level++; + } + + char * unit = NULL; + switch (level) { + case 0: + unit = "ns"; + break; + case 1: + unit = "us"; + break; + case 2: + unit = "ms"; + break; + case 3: + default: + unit = "s"; + break; + } + + snprintf(buf, bufsize, "%.2f %s", ns, unit); } From 23977683b225d17f7508c4a022d574e44c2d4f01 Mon Sep 17 00:00:00 2001 From: Brando Date: Mon, 5 May 2025 09:04:15 -0700 Subject: [PATCH 5/5] timing entire test suite and using minute rounding --- bftest/src/bftest.c | 10 ++++++++-- bftest/src/bftest.h | 14 +++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/bftest/src/bftest.c b/bftest/src/bftest.c index 0fbd25c5..77b18947 100644 --- a/bftest/src/bftest.c +++ b/bftest/src/bftest.c @@ -171,7 +171,7 @@ long long __BFTestGetCurrentTimeNS__() { void __BFTestFormatElapsedTime__(long long elapsedTimeNS, char * buf, size_t bufsize) { if (!buf) return; - // 0: nano, 1: micro, 2: milli, 3: seconds + // 0: nano, 1: micro, 2: milli, 3: seconds, 4: minutes int level = 0; const int maxLevel = 3; float ns = elapsedTimeNS; @@ -198,6 +198,12 @@ void __BFTestFormatElapsedTime__(long long elapsedTimeNS, char * buf, size_t buf break; } - snprintf(buf, bufsize, "%.2f %s", ns, unit); + if (ns >= 60 && level == 3) { + int min = ns / 60; + int sec = (int) ns % 60; + snprintf(buf, bufsize, "%d min %d s", min, sec); + } else { + snprintf(buf, bufsize, "%.2f %s", ns, unit); + } } diff --git a/bftest/src/bftest.h b/bftest/src/bftest.h index e77586b5..eba2f627 100644 --- a/bftest/src/bftest.h +++ b/bftest/src/bftest.h @@ -13,6 +13,9 @@ #include #include +long long __BFTestGetCurrentTimeNS__(); +void __BFTestFormatElapsedTime__(long long elapsedTimeNS, char * buf, size_t bufsize); + /** TEST SUITE **/ // a test suite is the high level function that will call // different sets of test coverage @@ -21,8 +24,12 @@ int main () {\ int pass = 0, fail = 0;\ float tp = 0, tf = 0;\ + time_t startTime = __BFTestGetCurrentTimeNS__();\ __VA_ARGS__ \ - printf("Grade - %.2f%% (%d/%d)\n", (float) ((tp/(tp+tf)) * 100), (int) tp, (int) (tp+tf));\ + time_t endTime = __BFTestGetCurrentTimeNS__();\ + char buf[64];\ + __BFTestFormatElapsedTime__(endTime - startTime, buf, 64);\ + printf("Grade - %.2f%% (%d/%d) %s\n", (float) ((tp/(tp+tf)) * 100), (int) tp, (int) (tp+tf), buf);\ return 0;\ } @@ -57,9 +64,6 @@ /** UNIT TEST **/ -long long __BFTestGetCurrentTimeNS__(); -void __BFTestFormatElapsedTime__(long long elapsedTimeNS, char * buf, size_t bufsize); - #define BFTEST_UNIT_FUNC(name, repeat, ...) \ int name (void) {\ BFTEST_UNIT_START;\ @@ -96,7 +100,7 @@ void __BFTestFormatElapsedTime__(long long elapsedTimeNS, char * buf, size_t buf if (result == 0) { printf("PASS"); }\ else { printf("FAIL"); }\ char buf[64];\ - __BFTestFormatElapsedTime__((endTime - startTime) / __repeat__, buf, 64);\ + __BFTestFormatElapsedTime__(endTime - startTime, buf, 64);\ printf(" (%s)\n", buf);\ fflush(stdout);\ _BFTestLogFlush();\