perf: show_peers() — replace bash while-read loops with awk

- cumulative_data: single awk pass for per-country bytes + grand totals
- tracker_snapshot: single awk pass for bytes, unique IP counts, and totals
- Remove unused cumulative_ips parsing (dead code — populated but never displayed)
- Fix awk dedup bug: ft++/tt++ were outside if-block, counting total lines instead of unique IPs
This commit is contained in:
SamNet-dev
2026-02-10 22:24:52 -06:00
parent 8bf2f8c640
commit 92267d037d

View File

@@ -3469,31 +3469,22 @@ show_peers() {
echo -e "${CYAN}╚══════════════════════════════════════════════════════════════════════╝${NC}${EL}" echo -e "${CYAN}╚══════════════════════════════════════════════════════════════════════╝${NC}${EL}"
echo -e "${EL}" echo -e "${EL}"
# Load tracker data # Load tracker data (awk for speed — bash while-read is too slow on large files)
unset cumul_from cumul_to total_ips_count 2>/dev/null unset cumul_from cumul_to 2>/dev/null
declare -A cumul_from cumul_to total_ips_count declare -A cumul_from cumul_to
local grand_in=0 grand_out=0 local grand_in=0 grand_out=0
if [ -s "$persist_dir/cumulative_data" ]; then if [ -s "$persist_dir/cumulative_data" ]; then
local _cd_parsed
_cd_parsed=$(awk -F'|' '$1!="" && $1!~/can.t|error/ {f=int($2+0); t=int($3+0); print $1"|"f"|"t; gi+=f; go+=t} END{print "_GI|"gi+0"|"go+0}' "$persist_dir/cumulative_data" 2>/dev/null)
while IFS='|' read -r c f t; do while IFS='|' read -r c f t; do
[ -z "$c" ] && continue if [ "$c" = "_GI" ]; then
[[ "$c" == *"can't"* || "$c" == *"error"* ]] && continue grand_in=$f; grand_out=$t
f=$(printf '%.0f' "${f:-0}" 2>/dev/null) || f=0 else
t=$(printf '%.0f' "${t:-0}" 2>/dev/null) || t=0 cumul_from["$c"]=$f; cumul_to["$c"]=$t
cumul_from["$c"]=$f fi
cumul_to["$c"]=$t done <<< "$_cd_parsed"
grand_in=$((grand_in + f))
grand_out=$((grand_out + t))
done < "$persist_dir/cumulative_data"
fi
if [ -s "$persist_dir/cumulative_ips" ]; then
while IFS='|' read -r c ip; do
[ -z "$c" ] && continue
[[ "$c" == *"can't"* || "$c" == *"error"* ]] && continue
total_ips_count["$c"]=$((${total_ips_count["$c"]:-0} + 1))
done < "$persist_dir/cumulative_ips"
fi fi
# Get actual connected clients from docker logs (parallel) # Get actual connected clients from docker logs (parallel)
@@ -3519,39 +3510,36 @@ show_peers() {
echo -e "${EL}" echo -e "${EL}"
# Parse snapshot for speed and country distribution # Parse snapshot for speed and country distribution (single awk pass)
unset snap_from_bytes snap_to_bytes snap_from_ips snap_to_ips 2>/dev/null unset snap_from_bytes snap_to_bytes snap_from_ip_cnt snap_to_ip_cnt 2>/dev/null
declare -A snap_from_bytes snap_to_bytes snap_from_ips snap_to_ips declare -A snap_from_bytes snap_to_bytes snap_from_ip_cnt snap_to_ip_cnt
local snap_total_from_ips=0 snap_total_to_ips=0 local snap_total_from_ips=0 snap_total_to_ips=0
if [ -s "$persist_dir/tracker_snapshot" ]; then if [ -s "$persist_dir/tracker_snapshot" ]; then
while IFS='|' read -r dir c bytes ip; do local _snap_parsed
[ -z "$c" ] && continue _snap_parsed=$(awk -F'|' '
[[ "$c" == *"can't"* || "$c" == *"error"* ]] && continue $2!="" && $2!~/can.t|error/ {
bytes=$(printf '%.0f' "${bytes:-0}" 2>/dev/null) || bytes=0 if($1=="FROM") {
if [ "$dir" = "FROM" ]; then fb[$2]+=$3+0
snap_from_bytes["$c"]=$(( ${snap_from_bytes["$c"]:-0} + bytes )) if(!fs[$2"|"$4]++) { fc[$2]++; ft++ }
snap_from_ips["$c|$ip"]=1 } else if($1=="TO") {
elif [ "$dir" = "TO" ]; then tb[$2]+=$3+0
snap_to_bytes["$c"]=$(( ${snap_to_bytes["$c"]:-0} + bytes )) if(!ts[$2"|"$4]++) { tc[$2]++; tt++ }
snap_to_ips["$c|$ip"]=1 }
fi }
done < "$persist_dir/tracker_snapshot" END {
for(c in fb) print "F|"c"|"int(fb[c])"|"fc[c]+0
for(c in tb) print "T|"c"|"int(tb[c])"|"tc[c]+0
print "_T|"ft+0"|"tt+0"|0"
}' "$persist_dir/tracker_snapshot" 2>/dev/null)
while IFS='|' read -r type c val1 val2; do
case "$type" in
F) snap_from_bytes["$c"]=$val1; snap_from_ip_cnt["$c"]=$val2 ;;
T) snap_to_bytes["$c"]=$val1; snap_to_ip_cnt["$c"]=$val2 ;;
_T) snap_total_from_ips=$c; snap_total_to_ips=$val1 ;;
esac
done <<< "$_snap_parsed"
fi fi
# Count unique snapshot IPs per country + totals
unset snap_from_ip_cnt snap_to_ip_cnt 2>/dev/null
declare -A snap_from_ip_cnt snap_to_ip_cnt
for k in "${!snap_from_ips[@]}"; do
local sc="${k%%|*}"
snap_from_ip_cnt["$sc"]=$(( ${snap_from_ip_cnt["$sc"]:-0} + 1 ))
snap_total_from_ips=$((snap_total_from_ips + 1))
done
for k in "${!snap_to_ips[@]}"; do
local sc="${k%%|*}"
snap_to_ip_cnt["$sc"]=$(( ${snap_to_ip_cnt["$sc"]:-0} + 1 ))
snap_total_to_ips=$((snap_total_to_ips + 1))
done
# TOP 10 TRAFFIC FROM (peers connecting to you) # TOP 10 TRAFFIC FROM (peers connecting to you)
echo -e "${GREEN}${BOLD} 📥 TOP 10 TRAFFIC FROM ${NC}${DIM}(peers connecting to you)${NC}${EL}" echo -e "${GREEN}${BOLD} 📥 TOP 10 TRAFFIC FROM ${NC}${DIM}(peers connecting to you)${NC}${EL}"
echo -e "${EL}" echo -e "${EL}"
@@ -3563,7 +3551,6 @@ show_peers() {
local snap_b=${snap_from_bytes[$country]:-0} local snap_b=${snap_from_bytes[$country]:-0}
local speed_val=$((snap_b / 15)) local speed_val=$((snap_b / 15))
local speed_str=$(format_bytes $speed_val) local speed_str=$(format_bytes $speed_val)
local ips_all=${total_ips_count[$country]:-0}
# Estimate clients per country using snapshot distribution # Estimate clients per country using snapshot distribution
local snap_cnt=${snap_from_ip_cnt[$country]:-0} local snap_cnt=${snap_from_ip_cnt[$country]:-0}
local est_clients=0 local est_clients=0
@@ -3589,7 +3576,6 @@ show_peers() {
local snap_b=${snap_to_bytes[$country]:-0} local snap_b=${snap_to_bytes[$country]:-0}
local speed_val=$((snap_b / 15)) local speed_val=$((snap_b / 15))
local speed_str=$(format_bytes $speed_val) local speed_str=$(format_bytes $speed_val)
local ips_all=${total_ips_count[$country]:-0}
local snap_cnt=${snap_to_ip_cnt[$country]:-0} local snap_cnt=${snap_to_ip_cnt[$country]:-0}
local est_clients=0 local est_clients=0
if [ "$snap_total_to_ips" -gt 0 ] && [ "$snap_cnt" -gt 0 ]; then if [ "$snap_total_to_ips" -gt 0 ] && [ "$snap_cnt" -gt 0 ]; then