I'm trying to implement a micro-benchmark for tcp socket communication though a loop of a send and receive messages. However, I've got a high latency compared to the standards latency of TCP socket in some diagrams. How can I improve my code ?
Server Side
DataOutputStream output = new DataOutputStream(this.clientSocket.getOutputStream());
DataInputStream input = new DataInputStream(this.clientSocket.getInputStream());
int j = 0;
while (j < loop) {
output.write(bytes);
input.read(new byte[input.readInt()] );
j++;
}
Client Side
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
DataInputStream input = new DataInputStream(socket.getInputStream());
final long startTime = System.nanoTime();
int j = 0;
while (j < loop) {
input.read(new byte[input.readInt()]);
output.write(bytes);
j++;
}
final long endTime = System.nanoTime();
final double difference = (endTime - startTime) / 1e6;
LOGGER.info("Latency:"+ difference/loop);