[PATCH 3/3] selftests: add option to print the time spent per test

From: Jori Koolstra (CIQ)

Date: Wed Nov 18 2026 - 08:40:59 EST


There is currently no way to see how long each test took when running
kselftests through run_kselftest.sh. This information is useful for
CI/CD systems.

Add a -T/--timed option to run_kselftest.sh. When it is set, run_one()
prints a KTAP diagnostic line with the elapsed wall-clock time just
before the result line of each test that is actually run, e.g.:

# selftests: timers: nanosleep
# time: 12.34s
ok 3 selftests: timers: nanosleep

Signed-off-by: Jori Koolstra (CIQ) <jkoolstra@xxxxxxxxx>
---
tools/testing/selftests/kselftest/runner.sh | 22 +++++++++++++++++++++-
tools/testing/selftests/run_kselftest.sh | 8 ++++++++
2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/kselftest/runner.sh b/tools/testing/selftests/kselftest/runner.sh
index 06748af6fcf7..60e3bba518ed 100644
--- a/tools/testing/selftests/kselftest/runner.sh
+++ b/tools/testing/selftests/kselftest/runner.sh
@@ -16,6 +16,7 @@ export timeout_rc=124
export logfile=/dev/stdout
export per_test_logging=
export per_test_log_dir=/tmp
+export per_test_timing=
export RUN_IN_NETNS=

# Defaults for "settings" file fields:
@@ -56,10 +57,20 @@ tap_timeout()
fi
}

+# Print the system uptime in hundredths of a second.
+uptime_cs()
+{
+ local up idle
+
+ read up idle < /proc/uptime
+ # Prefix the fraction with 1 so that a leading 0 isn't parsed as octal.
+ echo $((${up%.*} * 100 + 1${up#*.} - 100))
+}
+
run_one()
{
local DIR TEST BASENAME_TEST BASENAME_SANITIZED SETTINGS TEST_HDR_MSG
- local line field value interpreter stdbuf cmd rc
+ local line field value interpreter stdbuf cmd rc start elapsed

DIR="$1"
TEST="$2"
@@ -145,10 +156,18 @@ run_one()
fi
fi
cd `dirname $TEST` > /dev/null
+ if [ -n "$per_test_timing" ]; then
+ start=$(uptime_cs)
+ fi
(((( tap_timeout "$cmd" 2>&1; echo $? >&3) |
tap_prefix >&4) 3>&1) |
(read xs; exit $xs)) 4>>"$logfile"
rc=$?
+ if [ -n "$per_test_timing" ]; then
+ elapsed=$(($(uptime_cs) - start))
+ ktap_print_msg "$(printf 'time: %d.%02ds' \
+ $((elapsed / 100)) $((elapsed % 100)))"
+ fi
case "$rc" in
"$KSFT_PASS")
ktap_test_pass "$TEST_HDR_MSG";;
@@ -175,6 +194,7 @@ in_netns()
source $BASE_DIR/kselftest/runner.sh
logfile=$logfile
kselftest_override_timeout=$kselftest_override_timeout
+ per_test_timing=$per_test_timing
run_one $DIR $TEST
EOF
}
diff --git a/tools/testing/selftests/run_kselftest.sh b/tools/testing/selftests/run_kselftest.sh
index e11b0e93ba41..0f2a8c619a9c 100755
--- a/tools/testing/selftests/run_kselftest.sh
+++ b/tools/testing/selftests/run_kselftest.sh
@@ -32,6 +32,7 @@ Usage: $0 [OPTIONS]
-n | --netns Run each test in namespace
-h | --help Show this usage info
-o | --override-timeout Number of seconds after which we timeout
+ -T | --timed Print the time spent on each test
EOF
exit $1
}
@@ -101,6 +102,13 @@ while true; do
-o | --override-timeout)
kselftest_override_timeout="$2"
shift 2 ;;
+ -T | --timed)
+ if [ ! -r /proc/uptime ]; then
+ echo "Timing tests requires /proc/uptime" >&2
+ exit 1
+ fi
+ per_test_timing=1
+ shift ;;
-h | --help)
usage 0 ;;
"")

--
2.56.1