From cce1ec6f42eb9d1d075df9e7dc58a8da955f9744 Mon Sep 17 00:00:00 2001 From: Ming Shao Date: Tue, 2 Aug 2022 11:23:28 +0800 Subject: [PATCH] ztest: Refine when to collect unit test duration Previously, unit test duration is collected within the TC_START and Z_TC_END_RESULT macros. With existing tests, the TC_START macro can be invoked by both the ztest fx and the tests themselves. And the TC_START macro definition went lengths to avoid the interference when it is invoked within a unit test. This commit decouple the time collection and the TC_STRAT/Z_TC_END_RESULT macros to fix this issue. Now only the (old) ztest framework is responsible for the test duration measure. The test duration stats of new ztest fx is different from this btw. Signed-off-by: Ming Shao --- subsys/testsuite/include/zephyr/tc_util.h | 12 ++---------- subsys/testsuite/ztest/src/ztest.c | 4 ++++ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/subsys/testsuite/include/zephyr/tc_util.h b/subsys/testsuite/include/zephyr/tc_util.h index 95bec0becc5..edcf8c02d38 100644 --- a/subsys/testsuite/include/zephyr/tc_util.h +++ b/subsys/testsuite/include/zephyr/tc_util.h @@ -97,16 +97,10 @@ static uint32_t tc_spend_time; static inline void get_start_time_cyc(void) { - /* Besides the ztest framework, some testcase will also call - * TC_START() in their code. But the caller thread cannot be - * in userspace. - */ - if (!k_is_user_context()) { - tc_start_time = k_cycle_get_32(); - } + tc_start_time = k_cycle_get_32(); } -static inline void test_time_ms(void) +static inline void get_test_duration_ms(void) { uint32_t spend_cycle = k_cycle_get_32() - tc_start_time; @@ -161,7 +155,6 @@ static inline void print_nothing(const char *fmt, ...) #define TC_START(name) \ do { \ TC_START_PRINT(name); \ - get_start_time_cyc(); \ } while (0) #endif @@ -185,7 +178,6 @@ static inline void print_nothing(const char *fmt, ...) #ifndef Z_TC_END_RESULT #define Z_TC_END_RESULT(result, func) \ do { \ - test_time_ms(); \ TC_END_PRINT(result, " %s - %s in %u.%u seconds\n", \ TC_RESULT_TO_STR(result), func, tc_spend_time/1000, \ tc_spend_time%1000); \ diff --git a/subsys/testsuite/ztest/src/ztest.c b/subsys/testsuite/ztest/src/ztest.c index 15de1b7dd9a..b41cb5e6888 100644 --- a/subsys/testsuite/ztest/src/ztest.c +++ b/subsys/testsuite/ztest/src/ztest.c @@ -273,6 +273,7 @@ static int run_test(struct unit_test *test) int skip = 0; TC_START(test->name); + get_start_time_cyc(); if (setjmp(test_fail)) { ret = TC_FAIL; @@ -292,6 +293,7 @@ static int run_test(struct unit_test *test) run_test_functions(test); out: ret |= cleanup_test(test); + get_test_duration_ms(); if (skip) { Z_TC_END_RESULT(TC_SKIP, test->name); @@ -365,6 +367,7 @@ static int run_test(struct unit_test *test) int ret = TC_PASS; TC_START(test->name); + get_start_time_cyc(); if (IS_ENABLED(CONFIG_MULTITHREADING)) { k_thread_create(&ztest_thread, ztest_thread_stack, @@ -405,6 +408,7 @@ static int run_test(struct unit_test *test) if (!test_result || !FAIL_FAST) { ret |= cleanup_test(test); } + get_test_duration_ms(); if (test_result == -2) { Z_TC_END_RESULT(TC_SKIP, test->name);