[PATCH v2 1/1] Add field checking tests for perf stat JSON output.

From: Claire Jensen
Date: Fri Aug 13 2021 - 18:09:58 EST


Counts number of fields to make sure expected fields are present.

Signed-off-by: Claire Jensen <cjense@xxxxxxxxxx>
---
.../tests/shell/lib/perf_json_output_lint.py | 48 ++++++++
tools/perf/tests/shell/stat+json_output.sh | 114 ++++++++++++++++++
2 files changed, 162 insertions(+)
create mode 100644 tools/perf/tests/shell/lib/perf_json_output_lint.py
create mode 100644 tools/perf/tests/shell/stat+json_output.sh

diff --git a/tools/perf/tests/shell/lib/perf_json_output_lint.py b/tools/perf/tests/shell/lib/perf_json_output_lint.py
new file mode 100644
index 000000000000..45d9163e7423
--- /dev/null
+++ b/tools/perf/tests/shell/lib/perf_json_output_lint.py
@@ -0,0 +1,48 @@
+#!/usr/bin/python
+
+from __future__ import print_function
+import argparse
+import sys
+
+# Basic sanity check of perf JSON output as specified in the man page.
+# Currently just checks the number of fields per line in output.
+
+ap = argparse.ArgumentParser()
+ap.add_argument('--no-args', action='store_true')
+ap.add_argument('--interval', action='store_true')
+ap.add_argument('--all-cpus-no-aggr', action='store_true')
+ap.add_argument('--all-cpus', action='store_true')
+ap.add_argument('--event', action='store_true')
+ap.add_argument('--per-core', action='store_true')
+ap.add_argument('--per-thread', action='store_true')
+ap.add_argument('--per-die', action='store_true')
+ap.add_argument('--per-node', action='store_true')
+ap.add_argument('--per-socket', action='store_true')
+args = ap.parse_args()
+
+Lines = sys.stdin.readlines()
+ch = ','
+
+
+def check_json_output(exp):
+ for line in Lines:
+ if 'failed' not in line:
+ count = 0
+ count = line.count(ch)
+ if count != exp:
+ sys.stdout.write(''.join(Lines))
+ raise RuntimeError('wrong number of fields. counted {0}'
+ ' expected {1} in {2}\n'.format(count, exp, line))
+
+
+try:
+ if args.no_args or args.all_cpus or args.event:
+ check_json_output(6)
+ if args.interval or args.per_thread:
+ check_json_output(7)
+ if args.per_core or args.per_socket or args.per_node or args.per_die:
+ check_json_output(8)
+
+except:
+ sys.stdout.write('Test failed for input:\n' + ''.join(Lines))
+ raise
diff --git a/tools/perf/tests/shell/stat+json_output.sh b/tools/perf/tests/shell/stat+json_output.sh
new file mode 100644
index 000000000000..8a772badae45
--- /dev/null
+++ b/tools/perf/tests/shell/stat+json_output.sh
@@ -0,0 +1,114 @@
+#!/bin/bash
+# perf stat JSON output linter
+# SPDX-License-Identifier: GPL-2.0
+# Checks various perf stat JSON output commands for the
+# correct number of fields.
+
+set -e
+set -x
+
+pythonchecker=$(dirname $0)/lib/perf_json_output_lint.py
+file="/proc/sys/kernel/perf_event_paranoid"
+paranoia=$(cat "$file" | grep -o -E '[0-9]+')
+
+check_no_args()
+{
+ perf stat -j sleep 1 2>&1 | \
+ python $pythonchecker --no-args
+}
+
+if [ $paranoia -gt 0 ];
+then
+ echo check_all_cpus test skipped because of paranoia level.
+else
+ check_all_cpus()
+ {
+ perf stat -j -a 2>&1 sleep 1 | \
+ python $pythonchecker --all-cpus
+ }
+ check_all_cpus
+fi
+
+check_interval()
+{
+ perf stat -j -I 1000 2>&1 sleep 1 | \
+ python $pythonchecker --interval
+}
+
+check_all_cpus_no_aggr()
+{
+ perf stat -j -A -a --no-merge 2>&1 sleep 1 | \
+ python $pythonchecker --all-cpus-no-aggr
+}
+
+check_event()
+{
+ perf stat -j -e cpu-clock 2>&1 sleep 1 | \
+ python $pythonchecker --event
+}
+
+if [ $paranoia -gt 0 ];
+then
+ echo check_all_cpus test skipped because of paranoia level.
+else
+ check_per_core()
+ {
+ perf stat -j --per-core -a 2>&1 sleep 1 | \
+ python $pythonchecker --per-core
+ }
+ check_per_core
+fi
+
+if [ $paranoia -gt 0 ];
+then
+ echo check_all_cpus test skipped because of paranoia level.
+else
+ check_per_thread()
+ {
+ perf stat -j --per-thread -a 2>&1 sleep 1 | \
+ python $pythonchecker --per-thread
+ }
+ check_per_thread
+fi
+
+if [ $paranoia -gt 0 ];
+then
+ echo check_per_die test skipped because of paranoia level.
+else
+ check_per_die()
+ {
+ perf stat -j --per-die -a 2>&1 sleep 1 | \
+ python $pythonchecker --per-die
+ }
+ check_per_die
+fi
+
+if [ $paranoia -gt 0 ];
+then
+ echo check_per_node test skipped because of paranoia level.
+else
+ check_per_node()
+ {
+ perf stat -j --per-node -a 2>&1 sleep 1 | \
+ python $pythonchecker --per-node
+ }
+ check_per_node
+fi
+
+if [ $paranoia -gt 0 ];
+then
+ echo check_per_socket test skipped because of paranoia level.
+else
+ check_per_socket()
+ {
+ perf stat -j --per-socket -a 2>&1 sleep 1 | \
+ python $pythonchecker --per-socket
+ }
+ check_per_socket
+fi
+
+check_no_args
+check_interval
+check_all_cpus_no_aggr
+check_event
+exit 0
--
2.33.0.rc1.237.g0d66db33f3-goog