Skip to content

Commit f8f2847

Browse files
mripardshuahkh
authored andcommitted
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: Maxime Ripard <[email protected]> Reviewed-by: David Gow <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent b85ea95 commit f8f2847

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

lib/kunit/test.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,45 @@ void kunit_init_test(struct kunit *test, const char *name, struct string_stream
338338
}
339339
EXPORT_SYMBOL_GPL(kunit_init_test);
340340

341+
/* Only warn when a test takes more than twice the threshold */
342+
#define KUNIT_SPEED_WARNING_MULTIPLIER 2
343+
344+
/* Slow tests are defined as taking more than 1s */
345+
#define KUNIT_SPEED_SLOW_THRESHOLD_S 1
346+
347+
#define KUNIT_SPEED_SLOW_WARNING_THRESHOLD_S \
348+
(KUNIT_SPEED_WARNING_MULTIPLIER * KUNIT_SPEED_SLOW_THRESHOLD_S)
349+
350+
#define s_to_timespec64(s) ns_to_timespec64((s) * NSEC_PER_SEC)
351+
352+
static void kunit_run_case_check_speed(struct kunit *test,
353+
struct kunit_case *test_case,
354+
struct timespec64 duration)
355+
{
356+
struct timespec64 slow_thr =
357+
s_to_timespec64(KUNIT_SPEED_SLOW_WARNING_THRESHOLD_S);
358+
enum kunit_speed speed = test_case->attr.speed;
359+
360+
if (timespec64_compare(&duration, &slow_thr) < 0)
361+
return;
362+
363+
if (speed == KUNIT_SPEED_VERY_SLOW || speed == KUNIT_SPEED_SLOW)
364+
return;
365+
366+
kunit_warn(test,
367+
"Test should be marked slow (runtime: %lld.%09lds)",
368+
duration.tv_sec, duration.tv_nsec);
369+
}
370+
341371
/*
342372
* Initializes and runs test case. Does not clean up or do post validations.
343373
*/
344374
static void kunit_run_case_internal(struct kunit *test,
345375
struct kunit_suite *suite,
346376
struct kunit_case *test_case)
347377
{
378+
struct timespec64 start, end;
379+
348380
if (suite->init) {
349381
int ret;
350382

@@ -356,7 +388,13 @@ static void kunit_run_case_internal(struct kunit *test,
356388
}
357389
}
358390

391+
ktime_get_ts64(&start);
392+
359393
test_case->run_case(test);
394+
395+
ktime_get_ts64(&end);
396+
397+
kunit_run_case_check_speed(test, test_case, timespec64_sub(end, start));
360398
}
361399

362400
static void kunit_case_internal_cleanup(struct kunit *test)

0 commit comments

Comments
 (0)