Files
testcan/scripts/make_report.sh
2026-08-20 14:56:23 +07:00

194 lines
7.9 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# Build a DETAILED markdown comparison table from master+slave benchmark logs.
#
# Two modes:
# make_report.sh <bitrate> [log_dir] [out_dir]
# append one detailed row for the newest
# master+slave pair (used by run_test.sh).
# log_dir defaults to ../results; out_dir
# defaults to log_dir.
# make_report.sh --dir <dir> rebuild the whole detailed table in <dir>
# from every master/slave pair it contains.
#
# The table is written to <dir>/results_table.md (regenerated in --dir mode,
# appended in single-row mode). The file is never deleted.
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
# ---------- helpers -------------------------------------------------------
# Pull a key=value out of a log's SUMMARY line.
val() { grep '^SUMMARY' "$1" 2>/dev/null | tr ' ' '\n' | grep "^$2=" | cut -d= -f2- || true; }
# Friendly device name for an interface.
#
# Ask the kernel which driver is actually bound before falling back to guessing
# from the interface name. The old name-only mapping claimed can0 was a PEAK,
# which mislabelled every FINTEK F81604 run in the results table.
device_label() {
local ifc="$1" drv="" dpath="/sys/class/net/$1/device/driver"
if [ -e "$dpath" ]; then drv="$(basename "$(readlink -f "$dpath")" 2>/dev/null || true)"; fi
case "$drv" in
f81604) echo "FINTEK F81604 USB"; return ;;
peak_usb) echo "PEAK USB"; return ;;
gs_usb) echo "gs_usb (CANable)"; return ;;
kvaser_usb) echo "Kvaser USB"; return ;;
esd_usb|ucan|mcba_usb|ems_usb) echo "$drv"; return ;;
"") ;; # interface gone (old log) -> fall through to name heuristics
*) echo "$drv"; return ;;
esac
case "$ifc" in
can_com*) echo "IPC module" ;;
can_peak*|pcan*) echo "PEAK USB" ;;
*) echo "$ifc" ;;
esac
}
bitrate_label() {
case "$1" in
1000000) echo "1 Mbps" ;;
500000) echo "500 kbps" ;;
*) echo "$(awk -v b="$1" 'BEGIN{printf "%.0f kbps", b/1000}')" ;;
esac
}
# yyyyMMdd-HHmmss -> epoch seconds
ts_epoch() {
local d="${1%%-*}" t="${1##*-}"
date -d "${d:0:4}-${d:4:2}-${d:6:2} ${t:0:2}:${t:2:2}:${t:4:2}" +%s 2>/dev/null || echo 0
}
# filename -> the trailing timestamp token (yyyyMMdd-HHmmss)
ts_of() { local b; b="$(basename "$1" .log)"; echo "${b##*_}"; }
# filename -> ratetag (500k / 1m ...): token before the timestamp
tag_of() { local b; b="$(basename "$1" .log)"; b="${b%_*}"; echo "${b##*_}"; }
# Detailed table header (also the column contract for emit_row).
print_header() {
echo "| Device | Interfaces (M→S) | Bitrate | Dur (s) | TX f/s | RX f/s | Frames sent | Valid rx | Invalid | Lost | Loss % | Payload Mbps | TX err | CAN err | Status |"
echo "|--------|------------------|--------:|--------:|-------:|-------:|------------:|---------:|--------:|-----:|-------:|-------------:|-------:|-------:|:------:|"
}
# emit_row <master_log> <slave_log> -> one markdown table row
emit_row() {
local M="$1" S="$2"
local m_if b m_dur tx_fps sent tx_err
m_if=$(val "$M" interface); b=$(val "$M" bitrate); m_dur=$(val "$M" duration_s)
tx_fps=$(val "$M" tx_frames_per_s); sent=$(val "$M" frames_sent); tx_err=$(val "$M" tx_errors)
local s_if valid invalid can_err
s_if=$(val "$S" interface); valid=$(val "$S" valid_frames)
invalid=$(val "$S" invalid_frames); can_err=$(val "$S" can_errors)
: "${sent:=0}" "${valid:=0}" "${invalid:=0}" "${tx_err:=0}" "${can_err:=0}" "${m_dur:=0}"
# RX f/s and payload Mbps rated over the master's actual send window (fair TX/RX compare).
local rx_fps mbps lost losspct status
rx_fps=$(awk -v v="$valid" -v d="$m_dur" 'BEGIN{ if(d>0) printf "%.2f", v/d; else print "0" }')
mbps=$(awk -v v="$valid" -v d="$m_dur" 'BEGIN{ if(d>0) printf "%.4f", v*8*8/d/1000000; else print "0" }')
lost=$(awk -v s="$sent" -v v="$valid" 'BEGIN{printf "%d", s - v}')
losspct=$(awk -v s="$sent" -v l="$lost" 'BEGIN{ if(s>0) printf "%.4f", l*100.0/s; else print "n/a" }')
if [ "$lost" -le 0 ] && [ "${tx_err:-0}" -eq 0 ] && [ "${can_err:-0}" -eq 0 ] && [ "${invalid:-0}" -eq 0 ]; then
status="✅ OK"
else
status="❌ FAIL"
fi
printf "| %s | %s→%s | %s | %s | %s | %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |\n" \
"$(device_label "$m_if")" "$m_if" "$s_if" "$(bitrate_label "$b")" \
"$(printf '%.1f' "$m_dur")" "$tx_fps" "$rx_fps" "$sent" "$valid" "$invalid" \
"$lost" "$losspct" "$mbps" "$tx_err" "$can_err" "$status"
}
# ---------- mode: rebuild whole table from a directory ---------------------
if [ "${1:-}" = "--dir" ]; then
DIR="${2:?usage: make_report.sh --dir <dir>}"
[ -d "$DIR" ] || { echo "!! not a directory: $DIR" >&2; exit 1; }
TABLE="$DIR/results_table.md"
# Pair each master log with the slave log of the same ratetag whose timestamp
# is closest (a master+slave run starts within ~1-2 s of each other).
rows_file="$(mktemp)"
shopt -s nullglob
for M in "$DIR"/can_master_*.log; do
mtag=$(tag_of "$M"); mts=$(ts_epoch "$(ts_of "$M")")
best=""; bestdiff=999999999
for S in "$DIR"/can_slave_*.log; do
[ "$(tag_of "$S")" = "$mtag" ] || continue
sts=$(ts_epoch "$(ts_of "$S")")
diff=$(( mts>sts ? mts-sts : sts-mts ))
if [ "$diff" -lt "$bestdiff" ]; then bestdiff=$diff; best="$S"; fi
done
[ -n "$best" ] || { echo "!! no slave match for $(basename "$M")" >&2; continue; }
# sort key: device then bitrate then interface
b=$(val "$M" bitrate); m_if=$(val "$M" interface)
printf "%s\t%010d\t%s\t%s\t%s\n" "$(device_label "$m_if")" "$b" "$m_if" "$M" "$best" >> "$rows_file"
done
{
echo "# CAN Max-Bandwidth — Detailed Results"
echo
echo "_Generated: $(date '+%Y-%m-%d %H:%M:%S') from \`$DIR\`_"
echo
print_header
# sort by device, bitrate, interface; then emit rows
sort -t$'\t' -k1,1 -k2,2n -k3,3 "$rows_file" | while IFS=$'\t' read -r _dev _b _if M S; do
emit_row "$M" "$S"
done
echo
echo "**RX f/s** and **Payload Mbps** are rated over the master's actual send window."
echo "**Lost** = master \`frames_sent\` − slave \`valid_frames\`; **Loss %** = Lost / frames_sent × 100."
echo "**Status** is ✅ OK only when Lost = 0 **and** TX err = 0 **and** CAN err = 0 **and** Invalid = 0."
echo
echo "## Per-run log files"
echo
sort -t$'\t' -k1,1 -k2,2n -k3,3 "$rows_file" | while IFS=$'\t' read -r dev _b _if M S; do
printf -- "- **%s @ %s**: \`%s\` + \`%s\`\n" "$dev" "$(bitrate_label "$((10#$_b))")" "$(basename "$M")" "$(basename "$S")"
done
} > "$TABLE"
rm -f "$rows_file"
echo ">> rebuilt detailed table: $TABLE"
echo
cat "$TABLE"
exit 0
fi
# ---------- mode: append newest pair (single bitrate) ----------------------
BITRATE="${1:?usage: make_report.sh <bitrate> [log_dir] [out_dir] | make_report.sh --dir <dir>}"
DIR="${2:-$ROOT/results}"
# Where the table is written. run_test.sh keeps logs in log/ and reports in
# report/, so it passes the two separately; standalone use writes beside the logs.
OUT_DIR="${3:-$DIR}"
mkdir -p "$OUT_DIR"
case "$BITRATE" in
1000000) TAG="1m" ;;
500000) TAG="500k" ;;
*) TAG="$(awk -v b="$BITRATE" 'BEGIN{printf "%.0fk", b/1000}')" ;;
esac
newest() { ls -t "$DIR"/can_${1}_*_${TAG}_*.log 2>/dev/null | head -1; }
MLOG="$(newest master)"; SLOG="$(newest slave)"
if [ -z "$MLOG" ] || [ -z "$SLOG" ]; then
echo "!! could not find master/slave logs for tag '$TAG' in $DIR" >&2
exit 1
fi
TABLE="$OUT_DIR/results_table.md"
[ -f "$TABLE" ] || print_header > "$TABLE"
ROW="$(emit_row "$MLOG" "$SLOG")"
echo
echo "================= RESULT ($(bitrate_label "$BITRATE")) ================="
echo "master log : $MLOG"
echo "slave log : $SLOG"
echo
print_header
echo "$ROW"
echo "$ROW" >> "$TABLE"
echo
echo ">> row appended to $TABLE"
echo ">> tip: run 'make_report.sh --dir $DIR' to rebuild the full sorted table."