Skip to contentSkip to conversation

perf: test: Add support for testing JSON generated by perf data command

PATCH In review Anup Sharma 3 participants 5 messages May 19, 2023 – May 22, 2023 perf events

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 perf: test: Add support for testing JSON generated by perf data command

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.

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
tools/perf/tests/shell/test_perf_data_converter_json.sh new file +78 -0
new file mode 100755
index 000000000000..54b7a19962fa
@@ -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}
Signature · 1 line
2.34.1
reviewer Acked-by on v2 1/1
#1 lore raw
Quoted 7 lines · Anup Sharma

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.

Thanks,
Ian

Quoted 98 lines

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
tools/perf/tests/shell/test_perf_data_converter_json.sh new file +78 -1
new file mode 100755
index 000000000000..54b7a19962fa
@@ -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

#2 lore raw

Em Mon, May 22, 2023 at 11:10:43AM -0700, Ian Rogers escreveu:

Quoted 10 lines · Anup Sharma
Quoted 7 lines

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.

I'm fixing these and some other identation minor issues:

[acme@quaco perf-tools-next]$ b4 am -ctsl --cc-trailers CAP-5=fXyqWYgR0M0rqG8a2j0sL1WThNX8r49T7EfvkOG32-UqA@mail.gmail.com Grabbing thread from lore.kernel.org/all/CAP-5%3DfXyqWYgR0M0rqG8a2j0sL1WThNX8r49T7EfvkOG32-UqA%40mail.gmail.com/t.mbox.gz Checking for newer revisions
Grabbing search results from lore.kernel.org
Analyzing 2 messages in the thread
Checking attestation on all messages, may take a moment...
---
[PATCH v2] perf: test: Add support for testing JSON generated by perf data command
+ Acked-by: Ian Rogers <[email protected]>
+ Link: https://lore.kernel.org/r/ZGcoJBAGlknjsA/n@yoga
+ Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
NOTE: install dkimpy for DKIM signature verification
---
Total patches: 1
---

Base: not specified
git am ./v2_20230519_anupnewsmail_perf_test_add_support_for_testing_json_generated_by_perf_data_command.mbx
[acme@quaco perf-tools-next]$ git am ./v2_20230519_anupnewsmail_perf_test_add_support_for_testing_json_generated_by_perf_data_command.mbx
Applying: perf: test: Add support for testing JSON generated by perf data command .git/rebase-apply/patch:59: trailing whitespace.
else
warning: 1 line adds whitespace errors. tools/perf/tests/shell/test_perf_data_converter_json.sh:48: trailing whitespace.
+ else
[acme@quaco perf-tools-next]$

#3 lore raw

Em Mon, May 22, 2023 at 03:28:20PM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 8 lines

Em Mon, May 22, 2023 at 11:10:43AM -0700, Ian Rogers escreveu:

Quoted 7 lines · Anup Sharma
Quoted 6 lines

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.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

I'm fixing these and some other identation minor issues:

The ones below, using 'set list' on vim, look at the mix of tabs with spaces:

+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$
+^Ithen$
+ echo "The file contains valid JSON format [SUCCESS]"$
+ else$
+ echo "The file does not contain valid JSON format [FAILED]"$
+ err=1$
+^I exit$
+ fi$
+ else$

+        echo "File not found [FAILED]"$
+        err=2$
+        exit$

+ fi$
+}$
+$
+test_json_converter_command$

Quoted 28 lines

[acme@quaco perf-tools-next]$ b4 am -ctsl --cc-trailers CAP-5=fXyqWYgR0M0rqG8a2j0sL1WThNX8r49T7EfvkOG32-UqA@mail.gmail.com Grabbing thread from lore.kernel.org/all/CAP-5%3DfXyqWYgR0M0rqG8a2j0sL1WThNX8r49T7EfvkOG32-UqA%40mail.gmail.com/t.mbox.gz Checking for newer revisions
Grabbing search results from lore.kernel.org
Analyzing 2 messages in the thread
Checking attestation on all messages, may take a moment...
---
[PATCH v2] perf: test: Add support for testing JSON generated by perf data command
+ Acked-by: Ian Rogers <[email protected]>
+ Link: https://lore.kernel.org/r/ZGcoJBAGlknjsA/n@yoga
+ Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
NOTE: install dkimpy for DKIM signature verification
---
Total patches: 1
---

Base: not specified
git am ./v2_20230519_anupnewsmail_perf_test_add_support_for_testing_json_generated_by_perf_data_command.mbx
[acme@quaco perf-tools-next]$ git am ./v2_20230519_anupnewsmail_perf_test_add_support_for_testing_json_generated_by_perf_data_command.mbx
Applying: perf: test: Add support for testing JSON generated by perf data command .git/rebase-apply/patch:59: trailing whitespace.
else
warning: 1 line adds whitespace errors. tools/perf/tests/shell/test_perf_data_converter_json.sh:48: trailing whitespace.

(quoted diff) +1 -0
+	else

[acme@quaco perf-tools-next]$

Signature · 1 line
- Arnaldo
#4 lore raw

Em Mon, May 22, 2023 at 03:31:06PM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 15 lines

Em Mon, May 22, 2023 at 03:28:20PM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 13 lines

Em Mon, May 22, 2023 at 11:10:43AM -0700, Ian Rogers escreveu:

Quoted 10 lines · Anup Sharma
Quoted 7 lines

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.

I'm fixing these and some other identation minor issues:

I have this on top, making the indentation to be consistent, using tabs, as the other .sh files in that directory and also having the 'then' be on the 'if' line, separated by ;

tools/perf/tests/shell/test_perf_data_converter_json.sh +19 -25
index 8f4d6575f34fa82e..72ac6c83231c03d5 100755
@@ -1,18 +1,15 @@
 #!/bin/bash
-# perf data json converter command test
+# '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
+if [ "$PYTHON" = "" ] ; then
+	if which python3 > /dev/null ; then
 		PYTHON=python3
-	elif which python > /dev/null
-	then
+	elif which python > /dev/null ; then
 		PYTHON=python
 	else
 		echo Skipping test, python not detected please set environment variable PYTHON.
@@ -42,8 +39,7 @@ 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
+	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]"
@@ -54,22 +50,20 @@ test_json_converter_command()
 
 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
+	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