/ de-omarchy / bin / omarchy-network-speedtest
swadhinbiswas Initial de-omarchy: Omarchy UI layer for Arch-based distros · 8/22/2026
9f393b2
131 lines · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash

# omarchy:summary=Measure live internet speed for one direction
# omarchy:group=network
# omarchy:args=[down|up]

set -e

direction="${1:-}"
probe=1.1.1.1
parallel=8
url_count=3

case "$direction" in
  down | up)
    ;;
  *)
    echo "Usage: omarchy-network-speedtest [down|up]" >&2
    exit 2
    ;;
esac

format_mbps() {
  awk -v value="$1" 'BEGIN {
    if (value <= 0) print "0.0"
    else if (value < 10) printf "%.1f\n", value
    else printf "%.0f\n", value
  }'
}

iface=$(ip route get "$probe" 2>/dev/null | awk '{ for (i = 1; i <= NF; i++) if ($i == "dev") { print $(i + 1); exit } }')

if [[ -z $iface || ! -r /sys/class/net/$iface/statistics/rx_bytes || ! -r /sys/class/net/$iface/statistics/tx_bytes ]]; then
  echo "No active network interface" >&2
  exit 1
fi

if ! omarchy-cmd-present curl; then
  echo "curl is required" >&2
  exit 1
fi

fast_token="YXNkZmFzZGxmbnNkYWZoYXNkZmhrYWxm"
fast_api_url="https://api.fast.com/netflix/speedtest/v2?https=true&token=$fast_token&urlCount=$url_count"

fast_urls=$(curl -fsS "$fast_api_url" 2>/dev/null | jq -r '.targets[]?.url // empty')

if [[ -z $fast_urls ]]; then
  echo "Failed to fetch speed test endpoints" >&2
  exit 1
fi

traffic_pids=()

cleanup() {
  local pid
  for pid in "${traffic_pids[@]}"; do
    [[ -n $pid ]] || continue
    pkill -TERM -P "$pid" 2>/dev/null || true
    kill "$pid" 2>/dev/null || true
  done
  for pid in "${traffic_pids[@]}"; do
    [[ -n $pid ]] || continue
    wait "$pid" 2>/dev/null || true
  done
}
trap cleanup EXIT

# Round-robin across the returned URLs so we spread load across Netflix OCA nodes.
traffic_worker() {
  local urls=("$@")
  local url_count=${#urls[@]}
  local idx=$RANDOM
  if [[ $direction == "down" ]]; then
    while true; do
      url=${urls[$((idx % url_count))]}
      curl -fsS -o /dev/null "$url" 2>/dev/null || return
      idx=$((idx + 1))
    done
  else
    while true; do
      url=${urls[$((idx % url_count))]}
      dd if=/dev/zero bs=1M count=64 2>/dev/null | curl -fsS -o /dev/null -X POST --data-binary @- "$url" 2>/dev/null || return
      idx=$((idx + 1))
    done
  fi
}

for (( i = 0; i < parallel; i++ )); do
  traffic_worker $fast_urls &
  traffic_pids+=("$!")
done

rx_before=$(cat "/sys/class/net/$iface/statistics/rx_bytes")
tx_before=$(cat "/sys/class/net/$iface/statistics/tx_bytes")

any_alive() {
  local pid
  for pid in "${traffic_pids[@]}"; do
    [[ -n $pid ]] || continue
    if kill -0 "$pid" 2>/dev/null; then
      return 0
    fi
  done
  return 1
}

while any_alive; do
  sleep 1
  rx_after=$(cat "/sys/class/net/$iface/statistics/rx_bytes")
  tx_after=$(cat "/sys/class/net/$iface/statistics/tx_bytes")

  if [[ $direction == "down" ]]; then
    rate=$(awk -v before="$rx_before" -v after="$rx_after" 'BEGIN {
      if (after < before) print 0
      else print (after - before) * 8 / 1000000
    }')
  else
    rate=$(awk -v before="$tx_before" -v after="$tx_after" 'BEGIN {
      if (after < before) print 0
      else print (after - before) * 8 / 1000000
    }')
  fi

  format_mbps "$rate"
  rx_before=$rx_after
  tx_before=$tx_after
done

wait 2>/dev/null || true