How to measure network response time using Java?

We have a client and a server. I want to measure the response time of the network between them. When I send a request to the server, it should immediately respond to my request, it should look like a ping request so that there is no time for processing on the server.

How to do it in Java?

+4
source share
4 answers

I did this by sending a time-stamped packet from the client to the server, and then returned the server again. When the server receives a time stamp, it can be compared with the current time stamp to measure the travel time in both directions.

There seems to be a way to generate ICMP ping in Java 5 and later. If you want to measure network speed, just take System.nanoTime() before and after and compare. Please note that if your program does not have sufficient privileges to perform ICMP ping, then Java will use the TCP echo request for port 7, which will still meet your needs.

+2
source

To provide a server response as quickly as possible, you must either

  • have a second connection to another port - this is useful if you want to test the route to the server, i.e. wires - or
  • a common thread for all incoming packets that will determine if the incoming packet is a ping request and respond immediately - which is useful if you want to test the current connection (which takes into account other packets on this connection).

In any case, the listening part (server) must have its own stream, or request processing may fade while other packets are being processed.

Please note that no matter what you do to measure the connection speed, you will not have a reliable value. However, you should have an acceptable value if you accept on average a few of these emails.

+1
source

I think you need to distinguish between the time spent by the server (machine) and the time for processing on the server side. For example, ping will simply measure ICMP response time, which is a return of the low-level network protocol. If you take into account the TCP stack, and then the subsequent processing by the server, this period of time will be longer.

From your question, it looks like ping (ICMP) is what you want. In JDK 5 and above, you can:

 String host = "192.168.0.1" int timeOut = 5000; boolean status = InetAddress.getByName(host).isReachable(timeOut) 

and you will need time using System.nano() or the like.

+1
source

using ping should be enough or wget that tracks the baud rate. You can use justniffer to reset the response time of "working" working servers.

0
source

All Articles