Skip to content

Instantly share code, notes, and snippets.

@julian-klode
Created November 2, 2020 08:19
Show Gist options
  • Save julian-klode/f80617568353dc2f04394e04067d7276 to your computer and use it in GitHub Desktop.
Save julian-klode/f80617568353dc2f04394e04067d7276 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import urllib.request
import time
import apt_pkg
import subprocess
import sys
def get_ping(cdn):
"""Return average over 5 RTT measurements"""
with subprocess.Popen(
["ping", "-q", "-c", "5", f"cdn-{cdn}.deb.debian.org"],
stdout=subprocess.PIPE,
encoding="utf-8",
) as proc:
for line in proc.stdout:
if line.startswith("rtt"):
return float(line.split("=")[1].split("/")[1])
def get_speed(cdn):
"""Return the speed of the CDN, in Mbit/s"""
start = time.time()
fetched = 0
with urllib.request.urlopen(
f"http://cdn-{cdn}.deb.debian.org/debian/pool/main/m/mysql-8.0/mysql-testsuite-8.0_8.0.21-1_amd64.deb"
) as fobj:
while True:
block = fobj.read(10 * 1024 * 1024)
if not block:
break
fetched += len(block)
elapsed = time.time() - start
if "-q" not in sys.argv:
print(80 * " ", end="\r", file=sys.stderr)
print(
f"{cdn}: {apt_pkg.size_to_str(fetched/elapsed * 8) } bit/s",
end="\r",
file=sys.stderr,
)
return start, fetched / elapsed / (1000 * 1000) * 8
def main():
"""Main entry point."""
cdn_names = ["aws", "fastly"]
results = {}
first = time.time()
for cdn in cdn_names:
ts0, speed0 = get_speed(cdn)
if "-q" not in sys.argv:
print(file=sys.stderr)
ts1, speed1 = get_speed(cdn)
if "-q" not in sys.argv:
print(file=sys.stderr)
print(ts0, cdn, speed0, sep=",")
past_speeds = [speed0]
while (
all(speed1 > 1.1 * s or speed1 < 0.9 * s for s in past_speeds)
and len(past_speeds) < 5
):
past_speeds.append(speed1)
print(ts1, cdn, speed1, sep=",")
ts1, speed1 = get_speed(cdn)
if len(past_speeds) > 1:
print(ts1, cdn, speed1, sep=",")
if "-q" not in sys.argv:
print(file=sys.stderr)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment