Speed Test library for Java / Android :
- speed test download with transfer rate output
- speed test upload with transfer rate output
- download and upload progress monitoring
- speed test server / port / uri can be configured easily
- For download process, library will download file from given speed test server parameters and calculate transfer rate
- For upload process, library will generate a random file with a given size and will upload this file to a server calculating transfer rate
No external file are required and no file are stored in Hard Disk.
- with Gradle, from jcenter :
compile 'com.github.akinaru:speedtest:1.09'
SpeedTestSocket speedTestSocket = new SpeedTestSocket();
- download process result with
onDownloadPacketsReceived
callback - upload process result with
onUploadPacketsReceived
callback - download progress with
onDownloadProgress
callback - upload progress with
onUploadProgress
callback - download error catch with
onDownloadError
- upload error catch with
onUploadError
speedTestSocket.addSpeedTestListener(new ISpeedTestListener() {
@Override
public void onDownloadPacketsReceived(int packetSize, float transferRateBitPerSeconds, float transferRateOctetPerSeconds) {
System.out.println("download transfer rate : " + transferRateBitPerSeconds + " bps");
System.out.println("download transfer rate : " + transferRateOctetPerSeconds + "Bps");
}
@Override
public void onDownloadError(SpeedTestError errorCode, String message) {
System.out.println("Download error " + errorCode + " occured with message : " + message);
}
@Override
public void onUploadPacketsReceived(int packetSize, float transferRateBitPerSeconds, float transferRateOctetPerSeconds) {
System.out.println("download transfer rate : " + transferRateBitPerSeconds + " bps");
System.out.println("download transfer rate : " + transferRateOctetPerSeconds + "Bps");
}
@Override
public void onUploadError(SpeedTestError errorCode, String message) {
System.out.println("Upload error " + errorCode + " occured with message : " + message);
}
@Override
public void onDownloadProgress(float percent, SpeedTestReport downloadReport) {
}
@Override
public void onUploadProgress(float percent, SpeedTestReport uploadReport) {
}
});
Download a single file from a server :
void startDownload(String hostname, int port, String uri)
params | type | description |
---|---|---|
hostname |
String | server hostname |
port |
int | server port |
uri |
String | uri to fetch your file from server |
speedTestSocket.startDownload("ipv4.intuxication.testdebit.info", 80,"/fichiers/10Mo.dat");
You can wait for test completion with closeSocketJoinRead()
which is prefered to closeSocket()
since it join reading thread before resuming application.
speedTestSocket.closeSocketJoinRead();
Upload a single file with specified size to a server :
void startUpload(String hostname, int port, String uri, int fileSizeOctet)
params | type | description |
---|---|---|
hostname |
String | server hostname |
port |
int | server port |
uri |
String | uri to fetch your file from server |
fileSizeOctet |
int | the file size to be uploaded to server (file will be generated randomly and sent to speed test server) |
Here is an example for a file of 10Moctet :
speedTestSocket.startUpload("1.testdebit.info", 80, "/", 10000000);
If you want to download/upload during a fix value, you can begin download/upload and then invoke :
speedTestSocket.forceStopTask();
The following will start downloading a file of 100Mo but will stop downloading 15 seconds later :
final Timer timer = new Timer();
/* instanciate speed test */
final SpeedTestSocket speedTestSocket = new SpeedTestSocket();
/* add a listener to wait for speed test completion and progress */
speedTestSocket.addSpeedTestListener(new ISpeedTestListener() {
@Override
public void onDownloadPacketsReceived(long packetSize, float transferRateBitPerSeconds, float transferRateOctetPerSeconds) {
}
@Override
public void onDownloadError(SpeedTestError speedTestError, String errorMessage) {
if (speedTestError != SpeedTestError.FORCE_CLOSE_SOCKET) {
if (timer != null) {
timer.purge();
timer.cancel();
}
}
}
@Override
public void onUploadPacketsReceived(long packetSize, float transferRateBitPerSeconds, float transferRateOctetPerSeconds) {
}
@Override
public void onUploadError(SpeedTestError speedTestError, String errorMessage) {
if (speedTestError != SpeedTestError.FORCE_CLOSE_SOCKET) {
if (timer != null) {
timer.purge();
timer.cancel();
}
}
}
@Override
public void onDownloadProgress(float percent, SpeedTestReport downloadReport) {
}
@Override
public void onUploadProgress(float percent, SpeedTestReport uploadReport) {
}
});
TimerTask stopTask = new TimerTask() {
@Override
public void run() {
System.out.println("--------------- FINISH REPORT -----------------------------");
SpeedTestReport downloadReport = speedTestSocket.getLiveDownloadReport();
System.out.println("---------------current download report--------------------");
System.out.println("progress : " + downloadReport.getProgressPercent() + "%");
System.out.println("transfer rate bit : " + downloadReport.getTransferRateBit() + "b/s");
System.out.println("transfer rate octet : " + downloadReport.getTransferRateOctet() + "B/s");
System.out.println("downloaded for now : " + downloadReport.getTemporaryPacketSize() + "/" + downloadReport.getTotalPacketSize());
if (downloadReport.getStartTime() > 0) {
System.out.println("amount of time : " + ((downloadReport.getReportTime() - downloadReport.getStartTime()) / 1000) + "s");
}
speedTestSocket.forceStopTask();
if (timer != null) {
timer.cancel();
timer.purge();
}
}
};
timer.schedule(stopTask, 15000);
speedTestSocket.startDownload("1.testdebit.info", 80, "/fichiers/100Mo.dat");
By default, progress report is achieved as following :
-
for upload, a progress report is sent and trigger
onUploadProgress
each timeuploadChunkSize
number of packet is sent. The default value foruploadChunkSize
is 65535 but you can change withspeedTestSocket.setUploadChunkSize(int chunkSize)
-
for download, a progress report is sent and trigger
onDownloadProgress
each time a chunk of data is read from the downlink socket
If you want to set a custom report interval, you can use a task scheduled at fixed rate to retrieve report with speedTestSocket.getLiveDownloadReport()
or speedTestSocket.getLiveUploadReport()
depending if you want download or upload report.
The following will start uploading a file of 10Mo and request reports every 400ms :
final Timer timer = new Timer();
/* instanciate speed test */
final SpeedTestSocket speedTestSocket = new SpeedTestSocket();
/* add a listener to wait for speed test completion and progress */
speedTestSocket.addSpeedTestListener(new ISpeedTestListener() {
@Override
public void onDownloadPacketsReceived(long packetSize, float transferRateBitPerSeconds, float transferRateOctetPerSeconds) {
}
@Override
public void onDownloadError(SpeedTestError speedTestError, String errorMessage) {
if (speedTestError != SpeedTestError.FORCE_CLOSE_SOCKET) {
if (timer != null) {
timer.purge();
timer.cancel();
}
}
}
@Override
public void onUploadPacketsReceived(long packetSize, float transferRateBitPerSeconds, float transferRateOctetPerSeconds) {
}
@Override
public void onUploadError(SpeedTestError speedTestError, String errorMessage) {
if (speedTestError != SpeedTestError.FORCE_CLOSE_SOCKET) {
if (timer != null) {
timer.purge();
timer.cancel();
}
}
}
@Override
public void onDownloadProgress(float percent, SpeedTestReport downloadReport) {
}
@Override
public void onUploadProgress(float percent, SpeedTestReport uploadReport) {
}
});
TimerTask task = new TimerTask() {
@Override
public void run() {
if (speedTestSocket.getSpeedTestMode() == SpeedTestMode.UPLOAD) {
SpeedTestReport uploadReport = speedTestSocket.getLiveUploadReport();
System.out.println("---------------current upload report--------------------");
System.out.println("progress : " + uploadReport.getProgressPercent() + "%");
System.out.println("transfer rate bit : " + uploadReport.getTransferRateBit() + "b/s");
System.out.println("transfer rate octet : " + uploadReport.getTransferRateOctet() + "B/s");
System.out.println("uploaded for now : " + uploadReport.getTemporaryPacketSize()
+ "/" + uploadReport.getTotalPacketSize());
System.out.println("amount of time : " +
((uploadReport.getReportTime() - uploadReport.getStartTime()) / 1000) + "s");
System.out.println("--------------------------------------------------------");
} else if (speedTestSocket.getSpeedTestMode() == SpeedTestMode.DOWNLOAD) {
SpeedTestReport downloadReport = speedTestSocket.getLiveDownloadReport();
System.out.println("---------------current download report--------------------");
System.out.println("progress : " + downloadReport.getProgressPercent() + "%");
System.out.println("transfer rate bit : " + downloadReport.getTransferRateBit() + "b/s");
System.out.println("transfer rate octet : " + downloadReport.getTransferRateOctet() + "B/s");
System.out.println("downloaded for now : " + downloadReport.getTemporaryPacketSize()
+ "/" + downloadReport.getTotalPacketSize());
System.out.println("amount of time : "
+ ((downloadReport.getReportTime() - downloadReport.getStartTime()) / 1000) + "s");
}
}
};
// scheduling the task at interval
timer.scheduleAtFixedRate(task, 0, 400);
speedTestSocket.startUpload("1.testdebit.info", 80, "/", 10000000);
- retrieve current download report :
SpeedTestReport getLiveDownloadReport()
- retrieve current upload report :
SpeedTestReport getLiveUploadReport()
speedTestSocket.setUploadChunkSize(65535);
You can set download socket timeout in milliseconds :
speedTestSocket.setSocketTimeout(5000);
- add Internet permission to manifest :
<uses-permission android:name="android.permission.INTERNET" />
- use an
AsyncTask
to run your speed test :
public class SpeedTestTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
SpeedTestSocket speedTestSocket = new SpeedTestSocket();
speedTestSocket.addSpeedTestListener(new ISpeedTestListener() {
@Override
public void onDownloadPacketsReceived(int packetSize,
float transferRateBitPerSeconds,
float transferRateOctetPerSeconds) {
Log.i("speed-test-app","download transfer rate : " + transferRateOctetPerSeconds + "Bps");
}
@Override
public void onDownloadError(SpeedTestError errorCode, String message) {
Log.i("speed-test-app","Download error " + errorCode + " occured with message : " + message);
}
@Override
public void onUploadPacketsReceived(int packetSize,
float transferRateBitPerSeconds,
float transferRateOctetPerSeconds) {
Log.i("speed-test-app","download transfer rate : " + transferRateOctetPerSeconds + "Bps");
}
@Override
public void onUploadError(SpeedTestError errorCode, String message) {
Log.i("speed-test-app","Upload error " + errorCode + " occured with message : " + message);
}
@Override
public void onDownloadProgress(float percent,SpeedTestReport downloadReport) {
}
@Override
public void onUploadProgress(float percent,SpeedTestReport uploadReport) {
}
});
speedTestSocket.startUpload("1.testdebit.info",
80, "/", 10000000); //will block until upload is finished
return null;
}
}
Execute it with : new SpeedTestTask().execute();
http://akinaru.github.io/speed-test-lib
./gradlew quickTest
JRE 1.7 compliant
Gradle using IntelliJ IDEA or Eclipse
The MIT License (MIT) Copyright (c) 2016 Bertrand Martel