#!/bin/bash
# omarchy:summary=Benchmark Arch mirrors for speed, latency, and sync freshness
# omarchy:examples=omarchycn mirror benchmark

set -euo pipefail

source "$OMARCHY_PATH/cn/lib/mirror.sh"

printf '%-11s %-22s %12s %8s %10s\n' "ID" "NAME" "SPEED" "TTFB" "SYNC"

for id in $(cn_mirror_ids); do
  url=$(cn_mirror_url "$id")
  read -r speed ttfb age < <(cn_mirror_probe "$url")

  if (( speed > 0 )); then
    speed_h="$((speed / 1024)) KB/s"
  else
    speed_h="FAIL"
  fi
  case "$age" in
  stale) sync_h="STALE" ;;
  unknown) sync_h="?" ;;
  *) sync_h="$((age / 60))m ago" ;;
  esac

  printf '%-11s %-22s %12s %7ss %10s\n' "$id" "$(cn_mirror_name "$id")" "$speed_h" "${ttfb:0:5}" "$sync_h"
done
