Skip to contentSkip to conversation

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

PATCH Superseded Anup Sharma 3 participants 3 messages May 17, 2023 – May 18, 2023 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

As a newcomer to this community, any feedback on the implementation is highly appreciated.

---
 .../shell/test_perf_data_converter_json.sh    | 64 +++++++++++++++++++
 1 file changed, 64 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 +64 -0
new file mode 100755
index 000000000000..88db96e38925
@@ -0,0 +1,64 @@
+#!/bin/sh
+# perf data json converter test
+# SPDX-License-Identifier: GPL-2.0
+
+set -e
+
+err=0
+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
+
+check()
+{
+	if [ `id -u` != 0 ]; then
+		echo "[Skip] No root permission"
+		err=2
+		exit
+	fi
+}
+
+test_json() 
+{
+	echo "Testing perf data convertion to JSON"
+	perf record -o $perfdata -F 99 -a -g -- sleep 1 > /dev/null 2>&1
+	perf data convert --to-json $result --force -i $perfdata >/dev/null 2>&1
+	echo "Perf Data Converter to JSON [SUCCESS]"
+}
+
+validate_json_format() 
+{
+	echo "Testing perf data converted to JSON format"
+	if [ -f "${result}" ]; then
+		if jq '.' "${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
+		fi
+	    else
+		echo "File not found [FAILED]"
+		err=2
+		exit
+	    fi
+}
+
+check
+
+test_json
+validate_json_format
+
+exit ${err}
Signature · 1 line
2.34.1
reviewer
#1 lore raw
Quoted 10 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.

As a newcomer to this community, any feedback on the implementation is highly appreciated.

Congratulations on sending your first patch! Anup is this summer's Google Summer-of-Code contributor for the Linux perf tool. I'm sure everyone on the lists is looking forward to your contributions!

Quoted 46 lines
---
 .../shell/test_perf_data_converter_json.sh    | 64 +++++++++++++++++++
 1 file changed, 64 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 +35 -0
new file mode 100755
index 000000000000..88db96e38925
@@ -0,0 +1,64 @@
+#!/bin/sh
+# perf data json converter test
+# SPDX-License-Identifier: GPL-2.0
+
+set -e
+
+err=0
+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
+
+check()
+{
+       if [ `id -u` != 0 ]; then
+               echo "[Skip] No root permission"
+               err=2
+               exit
+       fi
+}
+
+test_json()
+{

tools/perf/tests/shell/test_perf_data_converter_json.sh line 45

Quoted 10 lines
tools/perf/tests/shell/test_perf_data_converter_json.sh new file +10 -0
+       echo "Testing perf data convertion to JSON"
+       perf record -o $perfdata -F 99 -a -g -- sleep 1 > /dev/null 2>&1
+       perf data convert --to-json $result --force -i $perfdata >/dev/null 2>&1
+       echo "Perf Data Converter to JSON [SUCCESS]"
+}
+
+validate_json_format()
+{
+       echo "Testing perf data converted to JSON format"
+       if [ -f "${result}" ]; then

It can be a good idea to test for failure and early exit. It would avoid being as indented in the following if.

tools/perf/tests/shell/test_perf_data_converter_json.sh line 46

Quoted 1 line
tools/perf/tests/shell/test_perf_data_converter_json.sh new file +1 -0
+               if jq '.' "${result}" > /dev/null 2>&1; then

This assumes the jq program is available and it isn't by default on my distribution. We could test:
if ! which jq; then
but it may be better to depend on Python and json.loads - Python is already a dependency for a number of the tests.

Thanks,
Ian

Quoted 21 lines
tools/perf/tests/shell/test_perf_data_converter_json.sh new file +18 -1
+                   echo "The file contains valid JSON format [SUCCESS]"
+               else
+                   echo "The file does not contain valid JSON format [FAILED]"
+                   err=1
+               fi
+           else
+               echo "File not found [FAILED]"
+               err=2
+               exit
+           fi
+}
+
+check
+
+test_json
+validate_json_format
+
+exit ${err}
--

2.34.1

maintainer
#2 lore raw

Hello,

Quoted 8 lines · Ian Rogers
Quoted 6 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.

Quoted 8 lines
Quoted 4 lines

As a newcomer to this community, any feedback on the implementation is highly appreciated.

Congratulations on sending your first patch! Anup is this summer's Google Summer-of-Code contributor for the Linux perf tool. I'm sure everyone on the lists is looking forward to your contributions!

+1

Quoted 6 lines
Quoted 6 lines
---
 .../shell/test_perf_data_converter_json.sh    | 64 +++++++++++++++++++
 1 file changed, 64 insertions(+)
 create mode 100755 tools/perf/tests/shell/test_perf_data_converter_json.sh
Quoted 9 lines

tools/perf/tests/shell/test_perf_data_converter_json.sh line 3

Quoted 9 lines
tools/perf/tests/shell/test_perf_data_converter_json.sh new file +3 -0
new file mode 100755
index 000000000000..88db96e38925
@@ -0,0 +1,64 @@
+#!/bin/sh
+# perf data json converter test
+# SPDX-License-Identifier: GPL-2.0

You may want to run shellcheck when you write a shell script.

Quoted 17 lines
Quoted 17 lines
tools/perf/tests/shell/test_perf_data_converter_json.sh new file +17 -0
+
+set -e
+
+err=0
+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
Quoted 8 lines

tools/perf/tests/shell/test_perf_data_converter_json.sh line 28

Quoted 8 lines
tools/perf/tests/shell/test_perf_data_converter_json.sh new file +8 -0
+       exit ${err}
+}
+trap trap_cleanup exit term int
+
+check()
+{
+       if [ `id -u` != 0 ]; then
+               echo "[Skip] No root permission"

I don't think we actually need root permission for this test.

Quoted 9 lines

tools/perf/tests/shell/test_perf_data_converter_json.sh line 37

Quoted 9 lines
tools/perf/tests/shell/test_perf_data_converter_json.sh new file +9 -0
+               err=2
+               exit
+       fi
+}
+
+test_json()
+{
+       echo "Testing perf data convertion to JSON"
+       perf record -o $perfdata -F 99 -a -g -- sleep 1 > /dev/null 2>&1

The -a/--system-wide option requires root permission usually. We can run perf record in per-task mode instead. Please remove the -a option and run some other command instead of 'sleep 1'. The perf tool has a couple of test workloads and benchmark programs. So you can try something like

perf record -o $perfdata -F 99 -g -- perf test -w noploop

or

perf record -o $perfdata -F 99 -g -- perf bench sched messaging

Thanks,
Namhyung

Quoted 44 lines

tools/perf/tests/shell/test_perf_data_converter_json.sh line 45

Quoted 8 lines
tools/perf/tests/shell/test_perf_data_converter_json.sh new file +8 -0
+       perf data convert --to-json $result --force -i $perfdata >/dev/null 2>&1
+       echo "Perf Data Converter to JSON [SUCCESS]"
+}
+
+validate_json_format()
+{
+       echo "Testing perf data converted to JSON format"
+       if [ -f "${result}" ]; then

It can be a good idea to test for failure and early exit. It would avoid being as indented in the following if.

tools/perf/tests/shell/test_perf_data_converter_json.sh line 46

Quoted 1 line
tools/perf/tests/shell/test_perf_data_converter_json.sh new file +1 -0
+               if jq '.' "${result}" > /dev/null 2>&1; then

This assumes the jq program is available and it isn't by default on my distribution. We could test:
if ! which jq; then
but it may be better to depend on Python and json.loads - Python is already a dependency for a number of the tests.

Thanks,
Ian

Quoted 21 lines
tools/perf/tests/shell/test_perf_data_converter_json.sh new file +18 -1
+                   echo "The file contains valid JSON format [SUCCESS]"
+               else
+                   echo "The file does not contain valid JSON format [FAILED]"
+                   err=1
+               fi
+           else
+               echo "File not found [FAILED]"
+               err=2
+               exit
+           fi
+}
+
+check
+
+test_json
+validate_json_format
+
+exit ${err}
--

2.34.1