Eri*_*rik 3 java performance download
我有这个while loop下载文件
while ((val = bis.read(buffer, 0, 1024)) > 0) {
out.write(buffer, 0, val);
fileSize -= val;
if (fileSize < 1024) {
val = (int) fileSize;
}
Run Code Online (Sandbox Code Playgroud)
试图找出如何像许多速度站点一样显示Mbit/s.(链接)
http://www.speedtest.net/
.
希望mesurment留在while循环内但我已经
看到使用on-minute-threads射击的例子,但我不知道..
我不知道数据的数量,或者数据总是1024.那
就是我认为的
任何帮助都是问题?
我可能错了一个数量级或两个数量级,一般的想法如下:
long start = System.nanoTime();
long totalRead = 0;
final double NANOS_PER_SECOND = 1000000000.0;
final double BYTES_PER_MIB = 1024 * 1024;
while ((val = bis.read(buffer, 0, 1024)) > 0) {
//...
totalRead += val;
double speedInMBps = NANOS_PER_SECOND / BYTES_PER_MIB * totalRead / (System.nanoTime() - start + 1);
double speedInMbps = speed * 8;
}
Run Code Online (Sandbox Code Playgroud)
请注意,这会从头开始计算平均速度,而不是当前速度.