#!/usr/bin/env python3 -I # usage: timing.py import csv, random, re, subprocess, sys, time, tempfile, os # Set VERCEL_URL to the address of your own Vercel deployment of the site (the one tested is not published). HOSTS = {"vercel": os.environ["VERCEL_URL"], "cloudflare": "https://kaikhalid.com"} out, N = sys.argv[1], int(sys.argv[2]) FMT = "%{http_code},%{http_version},%{remote_ip},%{time_namelookup},%{time_connect},%{time_appconnect},%{time_starttransfer},%{time_total},%{size_download}" def get(url): return subprocess.run(["curl","-s","-m","30","-H","Accept-Encoding: br, gzip","-o","/dev/null","-D","-","-w","\n@@"+FMT,url],capture_output=True,text=True).stdout def asset(host, pat): html = subprocess.run(["curl","-s",HOSTS[host]+"/"],capture_output=True,text=True).stdout m = re.findall(pat, html) return m[0] if m else None targets = {} for h in HOSTS: avif = asset(h, r'/_next/static/[^"\']*desktop-1920[^"\']*\.avif') or asset(h, r'/_next/static/[^"\']*\.avif') targets[h] = {"home": HOSTS[h]+"/", "post": HOSTS[h]+"/blog/start-the-answer-early", "avif": HOSTS[h]+avif if avif else None} print(targets, file=sys.stderr) kinds = [k for k in ("home","post","avif") if all(targets[h][k] for h in HOSTS)] with open(out,"w",newline="") as f: w = csv.writer(f); w.writerow(["iter","kind","host","order","code","http","ip","dns","connect","tls","ttfb","total","bytes","cache"]) for i in range(N): for k in kinds: order = random.sample(list(HOSTS), 2) for pos,h in enumerate(order): raw = get(targets[h][k]) head, _, tail = raw.rpartition("@@") cache = ",".join(re.findall(r"(?im)^(?:x-vercel-cache|cf-cache-status): *(\S+)", head)) w.writerow([i,k,h,pos]+tail.strip().split(",")+[cache]); time.sleep(0.25) f.flush()