#!/bin/bash
# --- 1. Input Validation and Argument Parsing ---
CSV_FILE="${1:-./logs/profiler_combined_report.csv}"
OUTPUT_FILE="${2:-./logs/performance_summary.txt}"
THRESHOLD="${3:-100.0}"
# Verify file presence and verify it is not empty
if [ ! -f "$CSV_FILE" ] || [ ! -s "$CSV_FILE" ]; then
echo "Error: Accumulated metrics CSV file not found or empty at: $CSV_FILE" >&2
exit 1
fi
# Ensure output directory exists
mkdir -p "$(dirname "$OUTPUT_FILE")"
# --- 2. Extract Timing Metadata ---
START_TIME=$(awk -F, 'NR>2 && $1!="" {print $1; exit}' "$CSV_FILE")
END_TIME=$(tail -n 1 "$CSV_FILE" | awk -F, '{print $1}')
DURATION_STR="0m 0s"
if [ -n "$START_TIME" ] && [ -n "$END_TIME" ]; then
if [ "$START_TIME" == "$END_TIME" ]; then
DURATION_STR="0m 0s (Single Snapshot)"
else
START_SEC=$(date -d "$START_TIME" +%s 2>/dev/null || echo 0)
END_SEC=$(date -d "$END_TIME" +%s 2>/dev/null || echo 0)
DIFF_SEC=$((END_SEC - START_SEC))
if [ $DIFF_SEC -gt 0 ]; then
DURATION_STR="$((DIFF_SEC / 60))m $((DIFF_SEC % 60))s"
fi
fi
fi
# --- 3. Process Data Aggregations using AWK ---
# Dynamically aggregates statistics based ONLY on entries present inside the CSV file
REPORT_DATA=$(awk -F, -v thresh="$THRESHOLD" '
# Parse incoming CSV metrics rows (skipping header blocks)
NR > 2 && NF >= 5 {
method = $2
gsub(/[ \t\r\n]+/, "", method)
c = $3 + 0
ms = $5 + 0.0
calls[method] += c
total_ms[method] += ms
}
END {
total_targets = 0
executed_targets = 0
for (m in calls) {
total_targets++
c = calls[m]
t_ms = total_ms[m]
avg = (c > 0) ? (t_ms / c) : 0.0
flag = ""
if (c > 0) {
executed_targets++
if (avg > thresh) flag = "⚠️ SLOW"
}
# Format and output data row for sorting
printf "%-86s | %8d | %10.2f | %10.4f | %s\n", m, c, t_ms, avg, flag
}
# Coverage metrics match execution count 1:1 since we are not scanning a whitelist file
print "SUMMARY_METRICS:" total_targets "," executed_targets ",100.00"
}
' "$CSV_FILE")
# --- 4. Assemble the Final ASCII Manifest ---
THICK_DIV=$(printf '=%.0s' {1..136})
THIN_DIV=$(printf '-%.0s' {1..136})
CURRENT_DATE=$(date +"%Y-%m-%d %H:%M:%S")
# Filter out data rows and sort numerically descending by total_ms column (Field 3)
SORTED_ROWS=$(echo "$REPORT_DATA" | grep -v "^SUMMARY_METRICS:" | sort -t'|' -k3 -nr)
# Extract summary metrics values
METRICS_LINE=$(echo "$REPORT_DATA" | grep "^SUMMARY_METRICS:")
TOTAL_T=$(echo "$METRICS_LINE" | cut -d: -f2 | cut -d, -f1)
EXEC_T=$(echo "$METRICS_LINE" | cut -d, -f2)
PCT_T=$(echo "$METRICS_LINE" | cut -d, -f3)
{
echo "$THICK_DIV"
echo "PROFILER FULL REPORT: $CURRENT_DATE"
echo "Time Range: [${START_TIME:-N/A}] to [${END_TIME:-N/A}] (Duration: $DURATION_STR)"
echo "Threshold: > ${THRESHOLD}ms | Source: $(basename "$CSV_FILE")"
echo "$THICK_DIV"
printf "%-86s | %-8s | %-10s | %-10s | %s\n" "Method Name" "Calls" "Total MS" "Avg MS" "Flag"
echo "$THIN_DIV"
echo "$SORTED_ROWS"
echo "$THIN_DIV"
printf "COVERAGE SUMMARY: Total Methods Tracked: %d Methods Executed: %d Coverage Percentage: %s%%\n" "$TOTAL_T" "$EXEC_T" "$PCT_T"
echo "$THICK_DIV"
} > "$OUTPUT_FILE"
echo -e "\n[REPORT]: Saved to $OUTPUT_FILE"
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)