Created
December 26, 2020 00:55
-
-
Save maxandersen/77ae2a4e44d1391aa0b034ccd1f5ab64 to your computer and use it in GitHub Desktop.
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
///usr/bin/env jbang "$0" "$@" ; exit $? | |
//DEPS info.picocli:picocli:4.5.0 | |
//DEPS fr.bmartel:jspeedtest:1.32.1 | |
//DEPS me.tongfei:progressbar:0.9.0 | |
//DEPS commons-io:commons-io:2.6 | |
import static java.lang.System.out; | |
import java.net.URL; | |
import java.util.concurrent.Callable; | |
import org.apache.commons.io.FileUtils; | |
import fr.bmartel.speedtest.SpeedTestReport; | |
import fr.bmartel.speedtest.SpeedTestSocket; | |
import fr.bmartel.speedtest.inter.ISpeedTestListener; | |
import fr.bmartel.speedtest.model.SpeedTestError; | |
import me.tongfei.progressbar.ProgressBar; | |
import picocli.CommandLine; | |
import picocli.CommandLine.Parameters; | |
class speedtest implements Callable<Integer> { | |
@Parameters(index = "0", description = "The url to use for testing", defaultValue = "http://ipv4.ikoula.testdebit.info/100M.iso") | |
private URL toDownload; | |
private final static int SOCKET_TIMEOUT = 5000; | |
public static void main(String... args) throws Exception { | |
int exitCode = new CommandLine(new speedtest()).execute(args); | |
System.exit(exitCode); | |
} | |
@Override | |
public Integer call() throws Exception { // your business logic goes here... | |
// instantiate speed examples | |
final SpeedTestSocket speedTestSocket = new SpeedTestSocket(); | |
// set timeout for download | |
speedTestSocket.setSocketTimeout(SOCKET_TIMEOUT); | |
try (ProgressBar pb = new ProgressBar("Download", 100)) { | |
// add a listener to wait for speed examples completion and progress | |
speedTestSocket.addSpeedTestListener(new ISpeedTestListener() { | |
@Override | |
public void onCompletion(final SpeedTestReport report) { | |
onProgress(report.getProgressPercent(), report); | |
} | |
@Override | |
public void onProgress(final float percent, final SpeedTestReport downloadReport) { | |
pb.stepTo((long) downloadReport.getProgressPercent()); | |
pb.setExtraMessage( | |
FileUtils.byteCountToDisplaySize(downloadReport.getTransferRateBit().longValue()) + "/s"); | |
} | |
@Override | |
public void onError(final SpeedTestError speedTestError, final String errorMessage) { | |
out.println("error " + speedTestError + " : " + errorMessage); | |
System.exit(-1); | |
} | |
}); | |
speedTestSocket.startFixedDownload(toDownload.toString(), 500); | |
while (speedTestSocket.getLiveReport().getProgressPercent() < 100.0) { | |
Thread.sleep(10); | |
} | |
; | |
} | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment