"""Discrete-event toy: inference batching trade-off (CPU only, synthetic).
Service model: a batch of b requests costs t(b)=BASE+PER*b ms (GPU amortizes BASE).
Poisson arrivals. Not a benchmark; numbers illustrate shape only.
Every printed number is simulated. No GPU and no inference server is called.
"""
import random, statistics as st
BASE, PER = 20.0, 2.0      # ms: fixed per-forward cost, marginal per request
RATE = 0.25                # requests per ms (250 req/s)
N = 20000
def arrivals(seed=1):
    r=random.Random(seed); t=0; out=[]
    for _ in range(N): t+=r.expovariate(RATE); out.append(t)
    return out
def pct(xs,p): xs=sorted(xs); return xs[int(p*(len(xs)-1))]
def dynamic(max_batch, max_wait):
    a=arrivals(); i=0; now=0.0; lat=[]; batches=[]
    while i<N:
        now=max(now,a[i]); deadline=a[i]+max_wait
        j=i
        while j<N and j-i<max_batch and a[j]<=max(now,deadline): j+=1
        start=max(now, a[j-1]) if j-i==max_batch else max(now, min(deadline, a[j-1] if j==N else deadline))
        if j-i<max_batch: start=max(now,deadline) if j<N else max(now,a[j-1])
        b=j-i; done=start+BASE+PER*b
        lat+= [done-a[k] for k in range(i,j)]; batches.append(b); now=done; i=j
    thr=N/(now-a[0])*1000
    return thr,pct(lat,.5),pct(lat,.99),st.mean(batches)
print(f"arrival={RATE*1000:.0f} req/s, t(b)={BASE}+{PER}*b ms")
print(f"{'max_batch':>9} {'max_wait':>8} {'thr/s':>7} {'p50ms':>7} {'p99ms':>7} {'avg_b':>6}")
for mb,mw in [(1,0),(4,0),(8,5),(16,10),(32,20),(64,50)]:
    thr,p50,p99,ab=dynamic(mb,mw)
    print(f"{mb:>9} {mw:>8} {thr:>7.0f} {p50:>7.1f} {p99:>7.1f} {ab:>6.1f}")
# static vs continuous (iteration-level, Orca-style) for variable-length generation
def gen(seed=2,n=400):
    r=random.Random(seed); return [r.choice([8,16,32,64,256]) for _ in range(n)]  # output tokens
STEP=1.0  # ms per decode iteration, batch-size-independent (toy)
def static(lens,B=16):
    t=0; lat=[]
    for k in range(0,len(lens),B):
        grp=lens[k:k+B]; t+=max(grp)*STEP; lat+=[t]*len(grp)  # all wait for longest
    return t,lat
def continuous(lens,B=16):
    q=list(lens); slots=[]; t=0; lat=[]
    while q or slots:
        while q and len(slots)<B: slots.append(q.pop(0))
        t+=STEP; slots=[s-1 for s in slots]
        lat+=[t for s in slots if s==0]; slots=[s for s in slots if s>0]
    return t,lat
L=gen()
for name,f in [("static",static),("continuous",continuous)]:
    T,lat=f(L); print(f"{name:>10}: total {T:.0f} ms, tokens/s {sum(L)/T*1000:.0f}, mean completion {st.mean(lat):.0f} ms")
