System.nanotime and Applet

Hello all,

I'm creating an applet to upload potentially big files to a HTTP server.

As uploading may take some time, I would like to show the user the speed and eta of the upload.

Here is my code :

while ((n = fi.read(buff)) > 0){

startTime = System.nanoTime();

this.output.write(buff, 0, n);

this.output.flush();

estimatedTime = System.nanoTime() - startTime;

curSpeed = ((double) n * 1000 / (double) estimatedTime) * 1000000;

[...]

}

An average of curSpeed is then calculated and shown to the user.

This code works perfectly well in the applet viewer, but when I run it in the browser, I get a totally wrong speed (I get approximatly 40MB/s instead of 100KB/s).

What could possibly cause this?

Any help would be greatly appreciated.

--

strawks.

[1012 byte] By [strawksa] at [2007-11-27 3:59:33]
# 1
Same OS for the AppletViewer and browser?
xiarcela at 2007-7-12 9:04:05 > top of Java-index,Java Essentials,Java Programming...
# 2
I ask the above question because some systems (and perhaps browsers?) are incapable of handling nanoseconds.You might try using milliseconds (naturally changing your computation to reflect the difference) .. and see if the results are better.
xiarcela at 2007-7-12 9:04:05 > top of Java-index,Java Essentials,Java Programming...
# 3

Yes, same OS, same computer.

Using milliseconds works with a buffer size of at least 20KB with my connection, but I think that a connection with higher bandwidth would require a larger buffer to work.

The probleme is that the resolution of the timer is too small and I get an elapsed time of 0 sometimes.

I wonder why it works in the AppletViewer but not in the browser.

strawksa at 2007-7-12 9:04:05 > top of Java-index,Java Essentials,Java Programming...
# 4
Your calculation is wrong. It should compare cumulative number of bytes sent to cumulative elapsed time. Doing that calculation for the number of bytes sent in one buffer is meaningless.
DrClapa at 2007-7-12 9:04:05 > top of Java-index,Java Essentials,Java Programming...
# 5
Computing cumulative numbers would give me an average for the whole transfer, from the beginning.What I would like is an average on a short period of time.
strawksa at 2007-7-12 9:04:05 > top of Java-index,Java Essentials,Java Programming...
# 6
I think you really want average..But if elapsed is 0, pass on updating.
xiarcela at 2007-7-12 9:04:05 > top of Java-index,Java Essentials,Java Programming...
# 7
> I think you really want average..Restated:The velocity for a tiny window of time is not as useful as you think.The average speed over the whole transfer allows you to predict how long remains based on how much you have left.
xiarcela at 2007-7-12 9:04:05 > top of Java-index,Java Essentials,Java Programming...
# 8
For the speed of the transfer I think a 3s average or so would be more useful than the average of the whole transfer.But I agree that for the ETA the whole average is probably better.
strawksa at 2007-7-12 9:04:05 > top of Java-index,Java Essentials,Java Programming...
# 9
You can then wait 3000ms between computations...using the last time/total as reference (nowtime-lasttime, nowtotal-lasttotal) for speed.
xiarcela at 2007-7-12 9:04:05 > top of Java-index,Java Essentials,Java Programming...
# 10
nanotime is also NOT correct to the nanosecond.The actual accuracy depends on the operating system and hardware used, and is usually no better than 20-200 microseconds.And even that is way more accurate than you need.
jwentinga at 2007-7-12 9:04:05 > top of Java-index,Java Essentials,Java Programming...
# 11

Yes of course it's not correct to the nanosecond, but my question is why does it work in the AppletViewer but not in the browser? what's the difference?

A resolution of 200ms would be insufficient in most cases, I think the resolution of this timer is tens of ms (10 or 20ms) on most OS.

strawksa at 2007-7-12 9:04:05 > top of Java-index,Java Essentials,Java Programming...
# 12
200ms is 200 milliseconds. We're talking microseconds here. 200 microseconds is 0.2 milliseconds :)
jwentinga at 2007-7-12 9:04:05 > top of Java-index,Java Essentials,Java Programming...
# 13
sorry I misread your post.Actually the resolution with System.currentTimeMillis() seems to be 10 milliseconds on my system.
strawksa at 2007-7-12 9:04:05 > top of Java-index,Java Essentials,Java Programming...
# 14
possible. Depending on your OS currentTimeMillis can have resolutions up to a few hundred milliseconds, nanotime to a few hundred microseconds (so a factor 1000 more precise).
jwentinga at 2007-7-12 9:04:05 > top of Java-index,Java Essentials,Java Programming...