Solved

System.currentTimeMillis() not giving correct results when measuring network latency

Posted on 2006-07-19
10
572 Views
Last Modified: 2013-11-15
Here is a fragment of my code:


// POP server logon code here

for(i=0;i<10;i++)
{
startTimer = System.currentTimeMillis();
os.write("list\r\n".getBytes());    // send LIST command to POP server
bytesRead = is.read(inputBuffer, 0, bufferSize);    // read data sent from POP server
stopTimer = System.currentTimeMillis();
}


I am basically try to measure the amount of time it takes to do a LIST and for the server to return the result. The first time the loop runs, I get a reasonable time difference (stopTimer - startTimer) about 150ms. However for all the other 9 loops I get 0.

Why is this the case?

0
Comment
Question by:rukiman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 17143278
the accuracy depends on your os.
If the call takes less time than its accurate to then it could well show 0
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17143624
> startTimer = System.currentTimeMillis();
> os.write("list\r\n".getBytes());    // send LIST command to POP server
> bytesRead = is.read(inputBuffer, 0, bufferSize);    // read data sent
> from POP server
> stopTimer = System.currentTimeMillis();

If there is any error, you might get false numbers. Try changing the code to:

startTimer = System.currentTimeMillis();
try {
    os.write("list\r\n".getBytes());    // send LIST command to POP server
    bytesRead = is.read(inputBuffer, 0, bufferSize);    // read data sent from POP server
}finally{
    stopTimer = System.currentTimeMillis();
}
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 17143736
where you are calculating "stopTimer - startTimer"?

it should be immdtly after you get stopTimer or before you get startTimer!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 17143975
Perhaps the first time it tries to write to the output-stream/ read from the input-stream it takes some time, and thereafter it manages to do it faster - I don't see this as an issue. The result you are getting is probably correct in the milli-second range.
0
 
LVL 35

Expert Comment

by:girionis
ID: 17144236
rukiman,
> Why is this the case?

Maybe the compiler optimizes some part of the byte code with a JIT compiler and it makes it run faster.
0
 
LVL 12

Accepted Solution

by:
enachemc earned 125 total points
ID: 17144251
This might be a result from buffer size difference. Reading sockets is done in packets (a TCP/IP packet comes usualy with 1500 bytes). The first time you read, the information has to be read from the server, and if you do not read the entire buffer arrived, you will continue to read from the buffer, thus not involving any network time. You should do your analyses on big files and using bigger buffers (4K).
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17144295
Try changing that to

os.write("list\r\n".getBytes());    // send LIST command to POP server
os.flush();
0
 
LVL 4

Expert Comment

by:astorer
ID: 17145218
Option 1.  It could be that the server you are connecting to caches the information.  So, the first time around, it has to go and find the info before returning it taking 150ms.
Subsequently, it already has the information and can simply return it which is much quicker.  Zero is surprising though.

Option 2. It could be that the server has closed the socket.  So, when you do bytesRead = is.read(inputBuffer, 0, bufferSize); what is the value of bytesRead?  It could be -1 indicating that end of file has been reached.
You should always check for this.

In either case, you are not comparing like with like (i.e. the first request differes from subsequent).  The loop needs to go around the outer code that establishes the connection and gets the streams.
0
 
LVL 4

Expert Comment

by:orhanbaba
ID: 17152646
startTimer and stopTimer must be "long" not "integer"
0
 

Author Comment

by:rukiman
ID: 17269928
I found the problem....it was because I was out of sync and there was data already buffered to be read. I forgot that the server returned a response when I first connect to it and I wasn't reading this data.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java 8 to Java 6 8 43
software inventory tools 3 89
How to fix  socket closed error 11 64
Doing a system restore with a temporary profile. 5 46
Let’s list some of the technologies that enable smooth teleworking. 
In our personal lives, we have well-designed consumer apps to delight us and make even the most complex transactions simple. Many enterprise applications, however, are a bit behind the times. For an enterprise app to be successful in today's tech wo…
The viewer will learn how to implement Singleton Design Pattern in Java.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question