xref: /linux/tools/perf/tests/shell/test_perf_data_converter_json.sh (revision c7decec2f2d2ab0366567f9e30c0e1418cece43f)
1#!/bin/bash
2# 'perf data convert --to-json' command test
3# SPDX-License-Identifier: GPL-2.0
4
5set -e
6
7err=0
8
9shelldir=$(dirname "$0")
10# shellcheck source=lib/setup_python.sh
11. "${shelldir}"/lib/setup_python.sh
12
13perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
14result=$(mktemp /tmp/__perf_test.output.json.XXXXX)
15
16cleanup()
17{
18	rm -f "${perfdata}*"
19	rm -f "${result}"
20	trap - exit term int
21}
22
23trap_cleanup()
24{
25	echo "Unexpected signal in ${FUNCNAME[1]}"
26	cleanup
27	exit 1
28}
29trap trap_cleanup exit term int
30
31test_json_converter_command()
32{
33	echo "Testing Perf Data Conversion Command to JSON"
34	perf record -o "$perfdata" -F 99 -g -- perf test -w noploop
35	perf data convert --to-json "$result" --force -i "$perfdata"
36	if [ "$(cat ${result} | wc -l)" -gt "0" ] ; then
37		echo "Perf Data Converter Command to JSON [SUCCESS]"
38	else
39		echo "Perf Data Converter Command to JSON [FAILED]"
40		err=1
41	fi
42}
43
44test_json_converter_pipe()
45{
46	echo "Testing Perf Data Conversion Command to JSON (Pipe mode)"
47	perf record -o - -F 99 -g -- perf test -w noploop > "$perfdata"
48	cat "$perfdata" | perf data convert --to-json "$result" --force -i -
49	if [ "$(cat ${result} | wc -l)" -gt "0" ] ; then
50		echo "Perf Data Converter Command to JSON (Pipe mode) [SUCCESS]"
51	else
52		echo "Perf Data Converter Command to JSON (Pipe mode) [FAILED]"
53		err=1
54	fi
55}
56
57validate_json_format()
58{
59	echo "Validating Perf Data Converted JSON file"
60	if [ -f "$result" ] ; then
61		if $PYTHON -c  "import json; json.load(open('$result'))" >/dev/null 2>&1 ; then
62			echo "The file contains valid JSON format [SUCCESS]"
63		else
64			echo "The file does not contain valid JSON format [FAILED]"
65			err=1
66		fi
67	else
68		echo "File not found [FAILED]"
69		err=1
70	fi
71}
72
73test_json_converter_command
74validate_json_format
75
76test_json_converter_pipe
77validate_json_format
78
79cleanup
80exit ${err}
81