Skip to content

Instantly share code, notes, and snippets.

@pansila
Last active May 23, 2024 02:14
Show Gist options
  • Save pansila/809889227e973a81f46cb1808d0349e3 to your computer and use it in GitHub Desktop.
Save pansila/809889227e973a81f46cb1808d0349e3 to your computer and use it in GitHub Desktop.
Calculation the TX OP win chance among different WiFi AC traffics.
import random
from collections import defaultdict
SIFS_2G = 10
SIFS_5G = 16
SLOT = 9
LABELS = ['DIFS', 'BE', 'BK', 'VI', 'VO']
AIFS = [2, 3, 7, 2, 2]
CW_min = [15, 15, 15, 7, 3]
CW_max = [1023, 1023, 1023, 15, 7]
SHORT_RETRY_LIMIT = 7
def aifs_2g(ac):
return AIFS[ac] * SLOT + SIFS_2G
def aifs_5g(ac):
return AIFS[ac] * SLOT + SIFS_5G
def aifs(ac, band):
if ac == 0: # DIFS
return aifs_5g(ac)
if band == 0:
return aifs_2g(ac)
if band == 1:
return aifs_5g(ac)
def backoff_cnt(ac, cnt):
cw = (1 << cnt) * (CW_min[ac] + 1) - 1
cw = min(cw, CW_max[ac])
t = random.randint(0, cw + 1)
return t
stats = []
for cnt in range(SHORT_RETRY_LIMIT):
stats.append(defaultdict(int))
def run(streams, band):
for r in range(100000):
for cnt in range(SHORT_RETRY_LIMIT):
try_times = [backoff_cnt(ac, cnt) * SLOT + aifs(ac, band) for ac in streams]
win_i = -1
win_t = 1000000
for i, t in enumerate(try_times):
if t < win_t:
win_t = t
win_i = i
stats[cnt][win_i] += 1
for cnt, v in enumerate(stats):
print(f'retry={cnt}')
tot = 0
for ac, win in v.items():
tot += win
for ac, win in v.items():
print(f'\tac={LABELS[streams[ac]]}, {win=}, win_prob={win/tot:%}')
run([1,2,3,4], 0)
#run([0,1,2], 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment