perf test: Add test validating JSON generated by 'perf data convert --to-json'
authorAnup Sharma <anupnewsmail@gmail.com>
Fri, 19 May 2023 07:41:24 +0000 (13:11 +0530)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 22 May 2023 18:47:48 +0000 (15:47 -0300)
This commit adds support for testing the JSON output generated by the
'perf data' command's conversion to JSON functionality.

The test script now includes a step to ensure that the resulting JSON
file contains valid data.

Changes:
V1 -> V2:

Added a check for the existence of the result output file.
Replaced the usage of jq with json.load for validating the JSON format.
Checks using ShellCheck and checkpatch, addressing and resolving warnings.
Removed the unnecessary root permission check.
Modified the 'perf record' command to avoid requiring root permissions.

Committer testing:

  $ perf test to-json
  115: 'perf data convert --to-json' command test                      : Ok
  $ perf test -v to-json
  Couldn't bump rlimit(MEMLOCK), failures may take place when creating BPF maps, etc
  115: 'perf data convert --to-json' command test                      :
  --- start ---
  test child forked, pid 1746867
  Testing Perf Data Convertion Command to JSON
  Perf Data Converter Command to JSON [SUCCESS]
  Validating Perf Data Converted JSON file
  The file contains valid JSON format [SUCCESS]
  test child finished with 0
  ---- end ----
  'perf data convert --to-json' command test: Ok
  $

Signed-off-by: Anup Sharma <anupnewsmail@gmail.com>
Acked-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/ZGcoJBAGlknjsA/n@yoga
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Anup Sharma <anupnewsmail@gmail.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: linux-kernel@vger.kernel.org
Cc: linux-perf-users@vger.kernel.org
[ Fixup indentation to use consistently tabs, not a mixture of spaces and tabs, have 'if ... ; then'  on the same line ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/tests/shell/test_perf_data_converter_json.sh [new file with mode: 0755]

diff --git a/tools/perf/tests/shell/test_perf_data_converter_json.sh b/tools/perf/tests/shell/test_perf_data_converter_json.sh
new file mode 100755 (executable)
index 0000000..72ac6c8
--- /dev/null
@@ -0,0 +1,72 @@
+#!/bin/bash
+# 'perf data convert --to-json' command test
+# SPDX-License-Identifier: GPL-2.0
+
+set -e
+
+err=0
+
+if [ "$PYTHON" = "" ] ; then
+       if which python3 > /dev/null ; then
+               PYTHON=python3
+       elif which python > /dev/null ; then
+               PYTHON=python
+       else
+               echo Skipping test, python not detected please set environment variable PYTHON.
+               exit 2
+       fi
+fi
+
+perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
+result=$(mktemp /tmp/__perf_test.output.json.XXXXX)
+
+cleanup()
+{
+       rm -f "${perfdata}"
+       rm -f "${result}"
+       trap - exit term int
+}
+
+trap_cleanup()
+{
+       cleanup
+       exit ${err}
+}
+trap trap_cleanup exit term int
+
+test_json_converter_command()
+{
+       echo "Testing Perf Data Convertion Command to JSON"
+       perf record -o "$perfdata" -F 99 -g -- perf test -w noploop > /dev/null 2>&1
+       perf data convert --to-json "$result" --force -i "$perfdata" >/dev/null 2>&1
+       if [ $(cat "${result}" | wc -l) -gt "0" ] ; then
+               echo "Perf Data Converter Command to JSON [SUCCESS]"
+       else
+               echo "Perf Data Converter Command to JSON [FAILED]"
+               err=1
+               exit
+       fi
+}
+
+validate_json_format()
+{
+       echo "Validating Perf Data Converted JSON file"
+       if [ -f "$result" ] ; then
+               if $PYTHON -c  "import json; json.load(open('$result'))" >/dev/null 2>&1 ; then
+                       echo "The file contains valid JSON format [SUCCESS]"
+               else
+                       echo "The file does not contain valid JSON format [FAILED]"
+                       err=1
+                       exit
+               fi
+       else
+               echo "File not found [FAILED]"
+               err=2
+               exit
+       fi
+}
+
+test_json_converter_command
+validate_json_format
+
+exit ${err}