xref: /linux/tools/perf/tests/shell/test_perf_data_converter_json.sh (revision 2b0cfa6e49566c8fa6759734cf821aa6e8271a9e)
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	cleanup
26	exit ${err}
27}
28trap trap_cleanup exit term int
29
30test_json_converter_command()
31{
32	echo "Testing Perf Data Convertion Command to JSON"
33	perf record -o "$perfdata" -F 99 -g -- perf test -w noploop > /dev/null 2>&1
34	perf data convert --to-json "$result" --force -i "$perfdata" >/dev/null 2>&1
35	if [ "$(cat ${result} | wc -l)" -gt "0" ] ; then
36		echo "Perf Data Converter Command to JSON [SUCCESS]"
37	else
38		echo "Perf Data Converter Command to JSON [FAILED]"
39		err=1
40		exit
41	fi
42}
43
44validate_json_format()
45{
46	echo "Validating Perf Data Converted JSON file"
47	if [ -f "$result" ] ; then
48		if $PYTHON -c  "import json; json.load(open('$result'))" >/dev/null 2>&1 ; then
49			echo "The file contains valid JSON format [SUCCESS]"
50		else
51			echo "The file does not contain valid JSON format [FAILED]"
52			err=1
53			exit
54		fi
55	else
56		echo "File not found [FAILED]"
57		err=2
58		exit
59	fi
60}
61
62test_json_converter_command
63validate_json_format
64
65exit ${err}
66