linux/tools/perf/tests/shell/test_gecko_python.sh
Ian Rogers e1e4bd6f8b perf python: Port gecko to perf module
Port gecko.py to a standalone script in tools/perf/python/ that uses
the perf module directly to convert perf.data profiles into Firefox
Gecko profile format.

Improvements compared to the legacy script:
- Encapsulate profiler state in GeckoCLI and CategoryData classes with
  full type annotations, removing global variables.
- Harden the local HTTP server in _write_and_launch(): write the
  temporary profile to an isolated tempfile.TemporaryDirectory() with a
  randomized UUID filename instead of the current working directory,
  bind HTTPServer exclusively to 127.0.0.1 on an OS-assigned ephemeral
  port (0), restrict CORS Access-Control-Allow-Origin to
  'https://profiler.firefox.com' instead of '*', and restrict HTTP GET
  requests exclusively to the randomized profile filename (returning
  HTTP 403 for directory listings and any other path).
- Add -i/--input and -e/--event CLI options via argparse.

Add a shell test (test_gecko_python.sh) to verify the standalone script.

Assisted-by: Antigravity:gemini-3.1-pro
Signed-off-by: Ian Rogers <irogers@google.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2026-09-26 20:49:28 +02:00

96 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# gecko python test
set -e
shelldir=$(dirname "$0")
# shellcheck source=lib/setup_python.sh
. "${shelldir}"/lib/setup_python.sh
# If we don't have the perf python module, we can't test
if ! "$PYTHON" -c 'import perf' > /dev/null 2>&1; then
echo "Skipping test, perf python module not found"
exit 2
fi
script_dir="$(dirname "$0")/../../python"
script_path="${script_dir}/gecko.py"
if [ ! -f "$script_path" ]; then
echo "Skipping test, gecko.py not found at $script_path"
exit 2
fi
err=0
temp_data=""
temp_json=""
cleanup() {
rm -f "${temp_data}" "${temp_json}"
}
trap 'cleanup' EXIT TERM INT
temp_data=$(mktemp /tmp/perf.data.XXXXXX)
temp_json=$(mktemp /tmp/perf.gecko.json.XXXXXX)
test_file_mode() {
echo "Testing gecko.py..."
# Generate some events with callchains
if ! perf record -g -o "${temp_data}" -- perf test -w noploop >/dev/null 2>&1; then
echo "Skipping test, perf record -g failed (permissions or lack of support)"
exit 2
fi
# Run the script in save-only mode with custom product and category colors
if ! "$PYTHON" "$script_path" -i "${temp_data}" \
--product "perf-test-product" --user-color blue --kernel-color red \
--save-only "${temp_json}" >/dev/null; then
echo "File mode test failed."
err=1
else
# Validate JSON schema and custom CLI option values
if ! "$PYTHON" - "$script_path" "${temp_json}" << 'PYEOF'
import importlib.util
import json
import sys
with open(sys.argv[2], encoding="utf-8") as f:
data = json.load(f)
assert data["meta"]["product"] == "perf-test-product"
assert data["meta"]["version"] == 24
assert isinstance(data["threads"], list) and len(data["threads"]) > 0
spec = importlib.util.spec_from_file_location("gecko", sys.argv[1])
mod = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = mod
spec.loader.exec_module(mod)
class DummyHandler:
headers = []
error_code = None
def send_header(self, k, v):
self.headers.append((k, v))
def send_error(self, code, _msg):
self.error_code = code
dummy = DummyHandler()
mod.CORSRequestHandler.list_directory(dummy, "/tmp")
assert dummy.error_code == 403
PYEOF
then
echo "Gecko JSON and CORSRequestHandler validation failed."
err=1
else
echo "File mode JSON and CORSRequestHandler test passed."
fi
fi
}
test_file_mode
exit $err