| 1 |
#!/usr/bin/env python3 |
| 2 |
import os |
| 3 |
import queue |
| 4 |
import random |
| 5 |
import re |
| 6 |
import socket |
| 7 |
import threading |
| 8 |
import time |
| 9 |
import urllib.error |
| 10 |
import urllib.parse |
| 11 |
import urllib.request |
| 12 |
|
| 13 |
import aptsources.distro |
| 14 |
import aptsources.sourceslist |
| 15 |
|
| 16 |
socket.setdefaulttimeout(2) |
| 17 |
|
| 18 |
|
| 19 |
class MirrorTest: |
| 20 |
class PingWorker(threading.Thread): |
| 21 |
def __init__(self, jobs, results, id): |
| 22 |
self.id = id |
| 23 |
self.jobs = jobs |
| 24 |
self.results = results |
| 25 |
self.match_result = re.compile(r"^rtt .* = [\.\d]+/([\.\d]+)/.*") |
| 26 |
threading.Thread.__init__(self) |
| 27 |
|
| 28 |
def run(self): |
| 29 |
result = None |
| 30 |
while MirrorTest.completed_pings < MirrorTest.todo: |
| 31 |
try: |
| 32 |
mirror = self.jobs.get(True, 1) |
| 33 |
host = mirror.hostname |
| 34 |
except: |
| 35 |
continue |
| 36 |
print( |
| 37 |
"Pinging (Worker %s) %s (%s) ..." |
| 38 |
% (self.id, host, MirrorTest.completed_pings) |
| 39 |
) |
| 40 |
commando = os.popen("ping -q -c 4 -W 2 -i 0.3 %s" % host, "r") |
| 41 |
while True: |
| 42 |
line = commando.readline() |
| 43 |
if not line: |
| 44 |
break |
| 45 |
result = re.findall(self.match_result, line) |
| 46 |
MirrorTest.completed_pings_lock.acquire() |
| 47 |
MirrorTest.completed_pings += 1 |
| 48 |
if result: |
| 49 |
self.results.append([float(result[0]), host, mirror]) |
| 50 |
MirrorTest.completed_pings_lock.release() |
| 51 |
|
| 52 |
def speed_test(self, mirror): |
| 53 |
url = f"{mirror.get_repo_urls()[0]}/{self.test_file}" |
| 54 |
print("Downloading %s ..." % url) |
| 55 |
start = time.time() |
| 56 |
try: |
| 57 |
urllib.request.urlopen(url).read(51200) |
| 58 |
return 50 / (time.time() - start) |
| 59 |
except: |
| 60 |
return 0 |
| 61 |
|
| 62 |
def __init__(self, hosts, test_file): |
| 63 |
self.test_file = test_file |
| 64 |
jobs = queue.Queue() |
| 65 |
results = [] |
| 66 |
for h in hosts: |
| 67 |
jobs.put(h) |
| 68 |
self.threads = [] |
| 69 |
MirrorTest.completed_pings = 0 |
| 70 |
MirrorTest.completed_pings_lock = threading.Lock() |
| 71 |
MirrorTest.todo = len(hosts) |
| 72 |
|
| 73 |
for i in range(10): |
| 74 |
t = MirrorTest.PingWorker(jobs, results, i) |
| 75 |
self.threads.append(t) |
| 76 |
t.start() |
| 77 |
|
| 78 |
for t in self.threads: |
| 79 |
t.join() |
| 80 |
|
| 81 |
results.sort() |
| 82 |
print("\n\nTop ten RTTs:") |
| 83 |
for r in results[0:10]: |
| 84 |
print(f"{r[1]}: {r[0]}") |
| 85 |
print("\n\n") |
| 86 |
|
| 87 |
results.insert(0, [0, "rand", hosts[random.randint(1, len(hosts))]]) |
| 88 |
results.insert(0, [0, "rand", hosts[random.randint(1, len(hosts))]]) |
| 89 |
|
| 90 |
final = [(self.speed_test(r[2]), r[2]) for r in results[0:12]] |
| 91 |
final.sort() |
| 92 |
final.reverse() |
| 93 |
print("\n\nBest mirrors:") |
| 94 |
for f in final: |
| 95 |
print(f"{f[1].hostname}: {int(f[0])} KByte/s") |
| 96 |
|
| 97 |
|
| 98 |
if __name__ == "__main__": |
| 99 |
distro = aptsources.distro.get_distro() |
| 100 |
distro.get_sources(aptsources.sourceslist.SourcesList()) |
| 101 |
pipe = os.popen("dpkg --print-architecture") |
| 102 |
arch = pipe.read().strip() |
| 103 |
test_file = "dists/{}/{}/binary-{}/Packages.gz".format( |
| 104 |
distro.source_template.name, |
| 105 |
distro.source_template.components[0].name, |
| 106 |
arch, |
| 107 |
) |
| 108 |
app = MirrorTest(distro.source_template.mirror_set.values(), test_file) |