Add ksft_metric() to report a numeric measurement of the current test case as JSON, right before the test result. Each metric states its goal: higher or lower for a performance metric, depending on which way is better, or zero for an error metric, so a consumer can tell wins from regressions. Values are plain numbers without a timestamp. A helper for other kinds of values, or a timestamp for values sampled over time, can be added when a test needs one. Signed-off-by: Stanislav Fomichev --- v2: https://lore.kernel.org/netdev/20261005201115.2690830-1-sdf@fomichev.me/ v1: https://lore.kernel.org/netdev/20260916190409.1222272-1-sdf@fomichev.me/ --- .../testing/selftests/drivers/net/README.rst | 17 +++++++++++ .../testing/selftests/net/lib/py/__init__.py | 9 +++--- tools/testing/selftests/net/lib/py/ksft.py | 28 +++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/drivers/net/README.rst b/tools/testing/selftests/drivers/net/README.rst index 3fe49bce4f3a..ab61bbc322fd 100644 --- a/tools/testing/selftests/drivers/net/README.rst +++ b/tools/testing/selftests/drivers/net/README.rst @@ -259,6 +259,23 @@ ksft_pr() Use ``ksft_pr()`` instead of ``print()`` to avoid breaking TAP format. +ksft_metric() +~~~~~~~~~~~~~ + +Use ``ksft_metric()`` to report a numeric measurement of the current test +case. Metrics are printed as compact JSON right before the test result. For +example:: + + ksft_metric("throughput", 100.5, goal="higher") + +prints:: + + # ktap-metric-json: {"goal":"higher","name":"throughput","value":100.5} + +``goal`` tells a performance metric, which is better ``higher`` or ``lower``, +from an error metric, which must be ``zero``. Metrics do not affect the +result of the test. + ksft_disruptive ~~~~~~~~~~~~~~~ diff --git a/tools/testing/selftests/net/lib/py/__init__.py b/tools/testing/selftests/net/lib/py/__init__.py index e269a260d58e..b1696f432c7c 100644 --- a/tools/testing/selftests/net/lib/py/__init__.py +++ b/tools/testing/selftests/net/lib/py/__init__.py @@ -8,8 +8,8 @@ from .consts import KSRC from .ksft import KsftFailEx, KsftSkipEx, KsftXfailEx, ksft_pr, ksft_eq, \ ksft_ne, ksft_true, ksft_not_none, ksft_in, ksft_not_in, ksft_is, \ ksft_ge, ksft_gt, ksft_lt, ksft_raises, ksft_busy_wait, \ - ktap_result, ksft_disruptive, ksft_setup, ksft_run, ksft_exit, \ - ksft_variants, KsftNamedVariant + ktap_result, ksft_disruptive, ksft_metric, ksft_setup, ksft_run, \ + ksft_exit, ksft_variants, KsftNamedVariant from .netns import NetNS, NetNSEnter, UserNetNS from .nsim import NetdevSim, NetdevSimDev from .utils import CmdExitFailure, fd_read_timeout, cmd, bkg, defer, \ @@ -24,8 +24,9 @@ __all__ = ["KSRC", "KsftFailEx", "KsftSkipEx", "KsftXfailEx", "ksft_pr", "ksft_eq", "ksft_ne", "ksft_true", "ksft_not_none", "ksft_in", "ksft_not_in", "ksft_is", "ksft_ge", "ksft_gt", "ksft_lt", "ksft_raises", - "ksft_busy_wait", "ktap_result", "ksft_disruptive", "ksft_setup", - "ksft_run", "ksft_exit", "ksft_variants", "KsftNamedVariant", + "ksft_busy_wait", "ktap_result", "ksft_disruptive", "ksft_metric", + "ksft_setup", "ksft_run", "ksft_exit", "ksft_variants", + "KsftNamedVariant", "NetNS", "NetNSEnter", "UserNetNS", "CmdExitFailure", "fd_read_timeout", "cmd", "bkg", "defer", "bpftool", "ip", "ethtool", "bpftrace", "rand_port", "rand_ports", diff --git a/tools/testing/selftests/net/lib/py/ksft.py b/tools/testing/selftests/net/lib/py/ksft.py index 81287c2daff0..3b97bdff4cbb 100644 --- a/tools/testing/selftests/net/lib/py/ksft.py +++ b/tools/testing/selftests/net/lib/py/ksft.py @@ -4,6 +4,7 @@ import fnmatch import functools import getopt import inspect +import json import os import signal import sys @@ -16,6 +17,7 @@ from . import utils KSFT_RESULT = None KSFT_RESULT_ALL = True KSFT_DISRUPTIVE = True +KSFT_METRICS = [] class KsftFailEx(Exception): @@ -93,6 +95,31 @@ KSFT_DISRUPTIVE = True print(pfx, prefixed, **kwargs) +def ksft_metric(name, value, *, goal): + """ + Report a numeric measurement of the current test case. + + Metrics are printed right before the result of the test case. They do + not affect the result. + + Args: + name - name of the metric, e.g. "throughput" + value - the measured number + goal - "higher" or "lower" for a performance metric, depending on + which way is better, "zero" for an error metric, which must + be zero + """ + KSFT_METRICS.append({"name": name, "value": value, "goal": goal}) + + +def _ksft_flush_metrics(): + for metric in KSFT_METRICS: + payload = json.dumps(metric, allow_nan=False, separators=(",", ":"), + sort_keys=True) + ksft_pr(f"ktap-metric-json: {payload}") + KSFT_METRICS.clear() + + def _fail(*args): global KSFT_RESULT KSFT_RESULT = False @@ -450,6 +477,7 @@ KsftCaseFunction = namedtuple("KsftCaseFunction", if not cnt_key: cnt_key = 'pass' if KSFT_RESULT else 'fail' + _ksft_flush_metrics() ktap_result(KSFT_RESULT, cnt, name, comment=comment) totals[cnt_key] += 1 -- 2.53.0-Meta