Reputation: 101
I am trying to get some information about the bandwidth and the latency using java. All what um doing is sending to a php server the number of bytes and the php server replies back with a string of these bytes. The strange thing that for packets ranging from 4 bytes to 4096 bytes the response time is almost constant but at larger packet sizes (larger than 4KB)the response time increase linearly.
PS : I tried to do traffic shaping in order to decrease the bandwidth for measurment but the same issue occurs.
Here is the java code
public void measure() throws IOException {
timeArray = new double[sizeArray.length];
for (int i = 0; i < sizeArray.length; i++) {
URL url = new URL("http://10.10.10.101/test.php?bytes=" +sizeArray[i]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
DataInputStream in = new DataInputStream (connection.getInputStream ());
long start = System.nanoTime();
byte [] temp = new byte[sizeArray[i]];
in.readFully(temp);
long end = System.nanoTime();
System.out.println("Size = " + sizeArray[i] + " , time = "+(end - start) +" ");
in.close();
//linear.addPoint(k[i] * Math.pow(10, -6) , (end - start) * Math.pow(10, -9));
// simple.addData(k[i] * Math.pow(10, -6) , (end - start) * Math.pow(10, -9));
timeArray[i] = (end - start);
} finally {
connection.disconnect();
}
}
}
and here is the code for the php echo server
<?php
$bytes = $_GET["bytes"];
$temp = intval ($bytes);
$result = str_pad("",$temp,"*");
echo($result);
?>
Can any body explain this odd behaviour ?
Upvotes: 1
Views: 366
Reputation: 2841
The strange thing that for packets ranging from 4 bytes to 4096 bytes the response time is almost constant but at larger packet sizes (larger than 4KB)the response time increase linearly.
This is likely due to output buffering. If you look at your PHP configuration at the output-buffering section you'll probably see that the 4096 bytes is the default buffer size for output. This basically means that PHP will not serve your request until is has accumulated 4096 bytes of information to send.
If you want to see a linear increase over all request, you'll want to lower this buffer. If you want a higher consistency you'll raise it. Really though you probably shouldn't be messing with it to much. But I assume you're just having fun and experimenting with the language. I hope this answered your question of why the behavior
Edit: After looking through java source code and how the HttpUrlConnection is processing some of the code, I wonder if adding a content length header would also help you.
<?php
$bytes = $_GET["bytes"];
$temp = intval ($bytes);
$result = str_pad("",$temp,"*");
header('Content-Length: ' . $temp);
echo($result);
?>
Apache or whatever you're using to serve the requests should be adding it automatically, but it wouldn't hurt to try setting the header explicitly. And on that note, have you considered that the TTFB (time to first byte) issue you're experiencing might be from the overhead on the server itself and not the php code? How are you serving the requests? Consider looking into the sendbuffersize configuration on apache if you're using that or whatever the equivalent is on your server.
Upvotes: 1