-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInternetSpeedTest.java
More file actions
36 lines (29 loc) · 1.23 KB
/
InternetSpeedTest.java
File metadata and controls
36 lines (29 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class InternetSpeedTest {
private static final String TEST_FILE_URL = "http://ipv4.ikoula.testdebit.info/1G.rem";
private static final int FILE_SIZE = 1000 * 1000 * 1000; // 1 GB
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
try {
URL url = new URL(TEST_FILE_URL);
URLConnection connection = url.openConnection();
BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
byte[] data = new byte[1024];
int bytesRead;
long totalBytesRead = 0;
while ((bytesRead = in.read(data, 0, 1024)) != -1) {
totalBytesRead += bytesRead;
}
in.close();
long endTime = System.currentTimeMillis();
long downloadDuration = endTime - startTime;
long downloadSpeed = (FILE_SIZE / (downloadDuration / 1000)) / 1000; // Kilobits per second
System.out.println("Download Speed: " + downloadSpeed + " Kbps");
} catch (IOException e) {
e.printStackTrace();
}
}
}