Last active
February 27, 2023 13:13
-
-
Save thotypous/15370bad23689c0c8db2f4874e01f4b2 to your computer and use it in GitHub Desktop.
Medição de latência e velocidade de download e upload para diversos servidores
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import math | |
import itertools | |
import matplotlib.pyplot as plt | |
import matplotlib.dates as mdates | |
import numpy as np | |
from datetime import datetime | |
from collections import defaultdict | |
markers = 'ov^<>1234spP*hHXD' | |
def do_plot(col, ylabel, scale=8/1024/1024, additional_lines=[]): | |
series = defaultdict(lambda: ([], [])) | |
with open('log') as f: | |
for line in f: | |
line = line.strip().split('\t') | |
if len(line) < col + 1: | |
continue | |
value = line[col] | |
if value in {'-', 'FAIL', 'null'}: | |
continue | |
ts, domain = line[:2] | |
domain = re.sub(r'^(ipv[46])-[^-]+-[^-]+-(.*?\.nflxvideo\.net)$', r'\1-*-*-\2', domain) | |
series[domain][0].append(datetime.fromtimestamp(int(ts))) | |
series[domain][1].append(scale*float(value)) | |
fig = plt.figure(figsize=(14, 10)) | |
ax = fig.add_subplot() | |
min_ts = min(itertools.chain(*[x for _, (x, _) in series.items()])) | |
max_ts = max(itertools.chain(*[x for _, (x, _) in series.items()])) | |
for value, spec, label in additional_lines: | |
ax.plot([min_ts, max_ts], 2*[value], spec, alpha=.5, lw=2, label=label) | |
order = sorted(((np.array(y).mean(), domain) | |
for domain, (_, y) in series.items()), reverse=True) | |
for (_, domain), marker in zip(order, itertools.cycle(markers)): | |
x, y = series[domain] | |
ax.plot(x, y, f'{marker}-', label=domain) | |
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %e %H:%M')) | |
fig.autofmt_xdate(rotation=45) | |
ax.set_position([0.05, 0.1, 0.7, 0.85]) | |
ax.set_ylabel(ylabel) | |
ax.legend(loc='center left', fontsize=7, bbox_to_anchor=(1, 0.5)) | |
return ax, fig | |
ax, fig = do_plot(2, 'Latência - RTT (ms)', scale=1) | |
ax.set_ylim([0, 50]) | |
fig.savefig('latencia.svg') | |
ax, fig = do_plot(3, 'Taxa de download (Mibit/s)', | |
additional_lines=[(0.80*600, 'g--', 'média exigida anatel (80%)'), | |
(0.40*600, 'r--', 'mínimo exigido anatel (40%)')]) | |
fig.savefig('download.svg') | |
ax, fig = do_plot(4, 'Taxa de upload (Mibit/s)', | |
additional_lines=[(0.80*300, 'g--', 'média exigida anatel (80%)'), | |
(0.40*300, 'r--', 'mínimo exigido anatel (40%)')]) | |
fig.savefig('upload.svg') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# The fast.com routines were based on https://github.com/hectorm/fast-cli, by Héctor Molinero Fernández | |
# License: MIT, https://opensource.org/licenses/MIT | |
set -eu | |
export LC_ALL=C | |
CHUNK_MiB=100 | |
CHUNK_B=$(($CHUNK_MiB*1024*1024)) | |
getFastToken() { | |
local fastMainURL='https://fast.com' | |
local fastJSPath=$(curl -sf "${fastMainURL:?}" | awk ' | |
match($0, /\/app-[A-Za-z0-9]+\.js/) { | |
print(substr($0, RSTART, RLENGTH)); | |
exit; | |
} | |
') | |
local fastToken=$(curl -sf "${fastMainURL:?}${fastJSPath:?}" | awk ' | |
match($0, /token:"[A-Za-z0-9]+"/) { | |
print(substr($0, RSTART+7, RLENGTH-8)); | |
exit; | |
} | |
') | |
printf -- '%s' "${fastToken:?}" | |
} | |
getFastURLs() { | |
local fastToken=${1:?}; local fastUseHTTPS=${2:?}; local fastURLCount=${3:?} | |
local fastAPIURL='https://api.fast.com/netflix/speedtest/v2' | |
local fastAPIParams="token=${fastToken:?}&https=${fastUseHTTPS:?}&urlCount=${fastURLCount:?}" | |
curl -sf "${fastAPIURL:?}?${fastAPIParams:?}" | awk ' | |
{while(match($0, /"url":"[^"]+"/)) { | |
print(substr($0, RSTART+7, RLENGTH-8)); | |
$0=substr($0, RSTART+RLENGTH); | |
}} | |
' | |
} | |
setFastURLRange() { | |
printf -- '%s' "${1:?}" | awk -v s="${2:?}" -v e="${3:?}" 'sub("/speedtest", "/speedtest/range/"s"-"e)' | |
} | |
domainFromURL() { | |
printf -- '%s' "${1:?}" | awk '{if(match($0,/\/\/([^\/]*)/,a))print a[1]}' | |
} | |
measureAvgRTT() { | |
# https://unix.stackexchange.com/a/409214 | |
ping -qc5 "$@" 2>&1 | awk -F'/' 'END{print(/^rtt/?$5:"FAIL")}' | |
} | |
measureHTTP() { | |
local downloadURL="${1:?}"; local uploadURL="${2:?}" | |
local ts="$(date +%s)" | |
local domain="$(domainFromURL "${downloadURL:?}")" | |
local rtt="$(measureAvgRTT "${domain:?}")" | |
local downloadSpeed="$(curl -ksfw '%{speed_download}' -o /dev/null "${downloadURL:?}")" | |
local uploadSpeed="$(dd if=/dev/urandom bs=1M count=${CHUNK_MiB:?} status=none | curl -ksfw '%{speed_upload}' -X POST -d @- -o /dev/null "${uploadURL:?}")" | |
printf -- '%s\t%s\t%s\t%s\t%s\n' "${ts:?}" "${domain:?}" "${rtt:?}" "${downloadSpeed:?}" "${uploadSpeed:?}" | |
} | |
main() { | |
# speedtest | |
local server="velocidade.clig.net.br.prod.hosts.ooklaserver.net:8080" | |
local ts="$(date +%s)" | |
speedtest++ --output json --test-server "${server:?}" | jq -r '["'$ts'", .server.host, .ping, (.download|tonumber)/8, (.upload|tonumber)/8] | @sh' | xargs printf '%s\t%s\t%s\t%s\t%s\n' | |
sleep 2 | |
# fast.com | |
local fastToken=$(getFastToken); local fastUseHTTPS=true; local fastURLCount=4 | |
local fastURLs=$(getFastURLs "${fastToken:?}" "${fastUseHTTPS:?}" "${fastURLCount:?}") | |
local url | |
printf -- '%s' "${fastURLs:?}" | while read url; do | |
local downloadURL=$(setFastURLRange "${url:?}" "0" "${CHUNK_B:?}") | |
local uploadURL=$(setFastURLRange "${url:?}" "0" "0") | |
measureHTTP "${downloadURL:?}" "${uploadURL:?}" | |
sleep 2 | |
done | |
# EAQ (NIC.br) | |
local domain | |
for domain in saopaulo-axpot.eaqbr.com.br saopaulo-axpot2.eaqbr.com.br saopaulo-axpot3.eaqbr.com.br; do | |
local downloadURL="https://${domain:?}/download/${CHUNK_MiB}?r=$RANDOM" | |
local uploadURL="https://${domain:?}/upload?r=$RANDOM" | |
measureHTTP "${downloadURL:?}" "${uploadURL:?}" | |
sleep 2 | |
done | |
# RNP | |
local urlbase | |
for urlbase in https://mirror.ufscar.br/ubuntu-releases http://ubuntu.c3sl.ufpr.br/releases https://mirror.uepg.br/ubuntu-releases; do | |
local proto | |
for proto in 4 6; do | |
local downloadURL="${urlbase:?}/jammy/ubuntu-22.04.2-desktop-amd64.iso?r=$RANDOM" | |
local ts="$(date +%s)" | |
local domain="$(domainFromURL "${downloadURL:?}")" | |
local rtt="$(measureAvgRTT -${proto:?} "${domain:?}")" | |
local downloadSpeed="$(curl -${proto:?} -ksfw '%{speed_download}' -H "range: bytes=1-${CHUNK_B:?}" -o /dev/null "${downloadURL:?}")" | |
printf -- '%s\t%s(v%s)\t%s\t%s\t%s\n' "${ts:?}" "${domain:?}" "${proto:?}" "${rtt:?}" "${downloadSpeed:?}" "-" | |
sleep 2 | |
done | |
done | |
} | |
main "$@" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment