2

I am building an application in Java where I need to know the download/upload speed as we get in https://speedtest.net kind of sites. Are there any APIs/java inbuilt methods using which I can get the speeds. Thanks!

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36

2 Answers2

2

You can use JSpeedTest:

SpeedTestSocket speedTestSocket = new SpeedTestSocket();

// add a listener to wait for speedtest completion and progress
speedTestSocket.addSpeedTestListener(new ISpeedTestListener() {

    @Override
    public void onCompletion(SpeedTestReport report) {
        // called when download/upload is complete
        System.out.println("[COMPLETED] rate in bit/s   : " + report.getTransferRateBit());
    }

    @Override
    public void onError(SpeedTestError speedTestError, String errorMessage) {
         // called when a download/upload error occur
    }

    @Override
    public void onProgress(float percent, SpeedTestReport report) {
        // called to notify download/upload progress
        System.out.println("[PROGRESS] progress : " + percent + "%");
        System.out.println("[PROGRESS] rate in bit/s   : " + report.getTransferRateBit());
    }
});
  • HTTP download:
speedTestSocket.startDownload("http://ipv4.ikoula.testdebit.info/1M.iso");
  • HTTP upload:
speedTestSocket.startUpload("http://ipv4.ikoula.testdebit.info/", 1000000);
  • HTTP fixed duration download:
speedTestSocket.startFixedDownload("http://ipv4.ikoula.testdebit.info/100M.iso", 10000);
  • HTTP fixed duration upload:
speedTestSocket.startFixedUpload("http://ipv4.ikoula.testdebit.info/", 10000000, 10000);

Sample output:

[PROGRESS] progress : 0.1223%
[PROGRESS] rate in bit/s   : 4253913.0432
[PROGRESS] progress : 1.2903%
[PROGRESS] rate in bit/s   : 18768000.0000
[PROGRESS] progress : 1.4363%
[PROGRESS] rate in bit/s   : 19150666.6664
.
.
.
[PROGRESS] progress : 98.6723%
[PROGRESS] rate in bit/s   : 11584655.1216
[PROGRESS] progress : 99.2563%
[PROGRESS] rate in bit/s   : 11651509.9048
[PROGRESS] progress : 100.0%
[PROGRESS] rate in bit/s   : 11694196.7552
[COMPLETED] rate in bit/s   : 11690778.8984

Maven dependency:

<!-- https://mvnrepository.com/artifact/fr.bmartel/jspeedtest -->
<dependency>
    <groupId>fr.bmartel</groupId>
    <artifactId>jspeedtest</artifactId>
    <version>1.32.1</version>
</dependency>
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
0

I don't think there is ready made API for this.

The setup for this solution is a client-server design

  1. Start Test (Client requests the server for a file URI and its size)
  2. Server responds to the Client with size of file to download and its URI
  3. Client requests the file using URI (starts timer)
  4. Client publishes periodic events on how much download is complete
  5. When file download finishes Client checks the download for completion (stops timer)
  6. Calculates time taken to download file of said size (Stop - Start) - Lets say this is time T
  7. Client now calculates speed based on size of file downloaded and time taken to download the file.

You could have the client do this calculation periodically too and show the speed experienced by the client on each update event.

Hope this helps. :)

A G
  • 297
  • 1
  • 7