linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Anup Sharma <anupnewsmail@gmail.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>,
	Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Anup Sharma <anupnewsmail@gmail.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2] perf: test: Add support for testing JSON generated by perf data command
Date: Fri, 19 May 2023 13:11:24 +0530	[thread overview]
Message-ID: <ZGcoJBAGlknjsA/n@yoga> (raw)

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 validation step to ensure that
the resulting JSON file is contain valid data.

Signed-off-by: Anup Sharma <anupnewsmail@gmail.com>

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.
---
 .../shell/test_perf_data_converter_json.sh    | 78 +++++++++++++++++++
 1 file changed, 78 insertions(+)
 create mode 100755 tools/perf/tests/shell/test_perf_data_converter_json.sh

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
index 000000000000..54b7a19962fa
--- /dev/null
+++ b/tools/perf/tests/shell/test_perf_data_converter_json.sh
@@ -0,0 +1,78 @@
+#!/bin/bash
+# perf data json converter 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}
-- 
2.34.1


             reply	other threads:[~2023-05-19  7:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-19  7:41 Anup Sharma [this message]
2023-05-22 18:10 ` [PATCH v2] perf: test: Add support for testing JSON generated by perf data command Ian Rogers
2023-05-22 18:28   ` Arnaldo Carvalho de Melo
2023-05-22 18:31     ` Arnaldo Carvalho de Melo
2023-05-22 18:43       ` Arnaldo Carvalho de Melo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZGcoJBAGlknjsA/n@yoga \
    --to=anupnewsmail@gmail.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).