Commit f8f2847f authored by Maxime Ripard's avatar Maxime Ripard Committed by Shuah Khan

kunit: Warn if tests are slow

Kunit recently gained support to setup attributes, the first one being
the speed of a given test, then allowing to filter out slow tests.

A slow test is defined in the documentation as taking more than one
second. There's an another speed attribute called "super slow" but whose
definition is less clear.

Add support to the test runner to check the test execution time, and
report tests that should be marked as slow but aren't.
Signed-off-by: default avatarMaxime Ripard <mripard@kernel.org>
Reviewed-by: default avatarDavid Gow <davidgow@google.com>
Signed-off-by: default avatarShuah Khan <skhan@linuxfoundation.org>
parent b85ea95d
...@@ -338,6 +338,36 @@ void kunit_init_test(struct kunit *test, const char *name, struct string_stream ...@@ -338,6 +338,36 @@ void kunit_init_test(struct kunit *test, const char *name, struct string_stream
} }
EXPORT_SYMBOL_GPL(kunit_init_test); EXPORT_SYMBOL_GPL(kunit_init_test);
/* Only warn when a test takes more than twice the threshold */
#define KUNIT_SPEED_WARNING_MULTIPLIER 2
/* Slow tests are defined as taking more than 1s */
#define KUNIT_SPEED_SLOW_THRESHOLD_S 1
#define KUNIT_SPEED_SLOW_WARNING_THRESHOLD_S \
(KUNIT_SPEED_WARNING_MULTIPLIER * KUNIT_SPEED_SLOW_THRESHOLD_S)
#define s_to_timespec64(s) ns_to_timespec64((s) * NSEC_PER_SEC)
static void kunit_run_case_check_speed(struct kunit *test,
struct kunit_case *test_case,
struct timespec64 duration)
{
struct timespec64 slow_thr =
s_to_timespec64(KUNIT_SPEED_SLOW_WARNING_THRESHOLD_S);
enum kunit_speed speed = test_case->attr.speed;
if (timespec64_compare(&duration, &slow_thr) < 0)
return;
if (speed == KUNIT_SPEED_VERY_SLOW || speed == KUNIT_SPEED_SLOW)
return;
kunit_warn(test,
"Test should be marked slow (runtime: %lld.%09lds)",
duration.tv_sec, duration.tv_nsec);
}
/* /*
* Initializes and runs test case. Does not clean up or do post validations. * Initializes and runs test case. Does not clean up or do post validations.
*/ */
...@@ -345,6 +375,8 @@ static void kunit_run_case_internal(struct kunit *test, ...@@ -345,6 +375,8 @@ static void kunit_run_case_internal(struct kunit *test,
struct kunit_suite *suite, struct kunit_suite *suite,
struct kunit_case *test_case) struct kunit_case *test_case)
{ {
struct timespec64 start, end;
if (suite->init) { if (suite->init) {
int ret; int ret;
...@@ -356,7 +388,13 @@ static void kunit_run_case_internal(struct kunit *test, ...@@ -356,7 +388,13 @@ static void kunit_run_case_internal(struct kunit *test,
} }
} }
ktime_get_ts64(&start);
test_case->run_case(test); test_case->run_case(test);
ktime_get_ts64(&end);
kunit_run_case_check_speed(test, test_case, timespec64_sub(end, start));
} }
static void kunit_case_internal_cleanup(struct kunit *test) static void kunit_case_internal_cleanup(struct kunit *test)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment