[PATCH v3 3/3] perf-test: add a test for perf-stat --bpf-counters option

From: Song Liu
Date: Tue Mar 16 2021 - 03:19:53 EST


Add a test to compare the output of perf-stat with and without option
--bpf-counters. If the difference is more than 10%, the test is considered
as failed.

Signed-off-by: Song Liu <songliubraving@xxxxxx>
---
tools/perf/tests/shell/stat_bpf_counters.sh | 31 +++++++++++++++++++++
1 file changed, 31 insertions(+)
create mode 100755 tools/perf/tests/shell/stat_bpf_counters.sh

diff --git a/tools/perf/tests/shell/stat_bpf_counters.sh b/tools/perf/tests=
/shell/stat_bpf_counters.sh
new file mode 100755
index 0000000000000..7aabf177ce8d1
--- /dev/null
+++ b/tools/perf/tests/shell/stat_bpf_counters.sh
@@ -0,0 +1,31 @@
+#!/bin/sh
+# perf stat --bpf-counters test
+# SPDX-License-Identifier: GPL-2.0
+
+set -e
+
+# check whether $2 is within +/- 10% of $1
+compare_number()
+{
+ first_num=3D$1
+ second_num=3D$2
+
+ # upper bound is first_num * 110%
+ upper=3D$(( $first_num + $first_num / 10 ))
+ # lower bound is first_num * 90%
+ lower=3D$(( $first_num - $first_num / 10 ))
+
+ if [ $second_num -gt $upper ] || [ $second_num -lt $lower ]; then
+ echo "The difference between $first_num and $second_num are=
greater than 10%."
+ exit 1
+ fi
+}
+
+# skip if --bpf-counters is not supported
+perf stat --bpf-counters true > /dev/null 2>&1 || exit 2
+
+base_cycles=3D$(perf stat --no-big-num -e cycles -- perf bench sched messa=
ging -g 1 -l 100 -t 2>&1 | awk '/cycles/ {print $1}')
+bpf_cycles=3D$(perf stat --no-big-num --bpf-counters -e cycles -- perf ben=
ch sched messaging -g 1 -l 100 -t 2>&1 | awk '/cycles/ {print $1}')
+
+compare_number $base_cycles $bpf_cycles
+exit 0
--
2.30.2