Skip to content

Instantly share code, notes, and snippets.

@CnrLwlss
Created November 13, 2019 12:06
Show Gist options
  • Save CnrLwlss/89ebab3307ce382eec51cbcf94c68ad7 to your computer and use it in GitHub Desktop.
Save CnrLwlss/89ebab3307ce382eec51cbcf94c68ad7 to your computer and use it in GitHub Desktop.
Generate broadband speed test reports
#https://github.com/hrbrmstr/speedtest
library(speedtest)
fname = "speedtest_results2.txt"
makeplots = FALSE
if(!makeplots){
config = spd_config()
servers = spd_servers(config = config)
servers = spd_closest_servers(servers, config)
best = spd_best_servers(servers, config, max = 3)
print(paste(best$name,best$country)[1])
if(!file.exists(fname)){
fileConn = file(fname)
writeLines(paste(c("startdown","stopdown","startup","stopup","downspeed","upspeed"),collapse="\t"),fileConn)
close(fileConn)
}
while(TRUE){
startdown = Sys.time()
down = tryCatch(max(spd_download_test(best, config, FALSE, timeout = 5,.progress = "none")$bw),error=function(cond){message(cond); return(NA);})
stopdown = Sys.time()
up = tryCatch(max(spd_upload_test(best, config, FALSE, timeout = 10,.progress = "none")$bw),error=function(cond){message(cond); return(NA);})
stopup = Sys.time()
fileConn = file(fname,open="a")
print(paste(startdown,down,up,paste(best$name,best$country)[1]))
writeLines(paste(c(as.character(startdown),as.character(stopdown),as.character(stopdown),as.character(stopup),as.character(down),as.character(up)),collapse="\t"),fileConn)
close(fileConn)
Sys.sleep(60*5)
}
}else{
dat = read.delim(fname,sep="\t",stringsAsFactors=FALSE)
dat$downmean = as.POSIXlt(dat$startdown)
dat$upmean = as.POSIXlt(dat$startup)
for(i in seq_along(dat$downmean)){
dat$downmean[i] = mean(c(strptime(dat$startdown[i],format = "%Y-%m-%d %H:%M:%S"),strptime(dat$stopdown[i],format = "%Y-%m-%d %H:%M:%S")))
dat$upmean[i] = mean(c(strptime(dat$startup[i],format = "%Y-%m-%d %H:%M:%S"),strptime(dat$stopup[i],format = "%Y-%m-%d %H:%M:%S")))
}
dat$downspeed[is.na(dat$downspeed)]=0
dat$upspeed[is.na(dat$upspeed)]=0
dat$date = as.Date(dat$downmean)
dates = sort(unique(dat$date))
pdf("Report.pdf")
op = par(mfrow=c(2,2),mar=c(4,4,1,0))
for(i in seq_along(dates)){
print(dates[i])
print(summary(dat$downspeed[dat$date==dates[i]]))
xlim = as.POSIXct(paste(dates[i],c("00:00:00", "24:00:00")), format = "%Y-%m-%d %H:%M:%S")
plot(dat$downmean[dat$date==dates[i]],dat$downspeed[dat$date==dates[i]],type="l",main=dates[i],axes=FALSE,xlab="Time of day",ylab="Download speed (Mb/s)",xlim=xlim,ylim=range(dat$downspeed))
axis(2)
axis.POSIXct(1,at=seq(xlim[1],xlim[2],by="hour"),las=2)
}
par(op)
dev.off()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment