测量下载速度 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6322767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Measuring Download Speed Java
提问by Jesus David Gulfo Agudelo
I'm working on downloading a file on a software, this is what i got, it sucesfully download, and also i can get progress, but still 1 thing left that I dont know how to do. Measure download speed. I would appreciate your help. Thanks. This is the current download method code
我正在下载一个软件上的文件,这就是我得到的,它成功下载,而且我可以取得进展,但还有一件事我不知道该怎么做。测量下载速度。我会很感激你的帮助。谢谢。这是当前的下载方法代码
public void run()
{
OutputStream out = null;
URLConnection conn = null;
InputStream in = null;
try
{
URL url1 = new URL(url);
out = new BufferedOutputStream(
new FileOutputStream(sysDir+"\"+where));
conn = url1.openConnection();
in = conn.getInputStream();
byte[] buffer = new byte[1024];
int numRead;
long numWritten = 0;
double progress1;
while ((numRead = in.read(buffer)) != -1)
{
out.write(buffer, 0, numRead);
numWritten += numRead;
this.speed= (int) (((double)
buffer.length)/8);
progress1 = (double) numWritten;
this.progress=(int) progress1;
}
}
catch (Exception ex)
{
echo("Unknown Error: " + ex);
}
finally
{
try
{
if (in != null)
{
in.close();
}
if (out != null)
{
out.close();
}
}
catch (IOException ex)
{
echo("Unknown Error: " + ex);
}
}
}
回答by Brian Roach
The same way you would measure anything.
就像你测量任何东西一样。
System.nanoTime()
returns a Long
you can use to measure how long something takes:
System.nanoTime()
返回一个Long
你可以用来衡量某件事需要多长时间:
Long start = System.nanoTime();
// do your read
Long end = System.nanoTime();
Now you have the number of nanoseconds it took to read X bytes. Do the math and you have your download rate.
现在您获得了读取 X 字节所需的纳秒数。算一算,你就有了下载率。
More than likely you're looking for bytes per second. Keep track of the total number of bytes you've read, checking to see if one second has elapsed. Once one second has gone by figure out the rate based on how many bytes you've read in that amount of time. Reset the total, repeat.
您很可能正在寻找每秒字节数。跟踪您已阅读的总字节数,检查是否已过去一秒钟。一秒过去后,根据您在这段时间内读取的字节数计算出速率。重置总数,重复。
回答by AZ_
here is my implementation
这是我的实现
while (mStatus == DownloadStatus.DOWNLOADING) {
/*
* Size buffer according to how much of the file is left to
* download.
*/
byte buffer[];
// handled resume case.
if ((mSize < mDownloaded ? mSize : mSize - mDownloaded <= 0 ? mSize : mSize - mDownloaded) > MAX_BUFFER_SIZE) {
buffer = new byte[MAX_BUFFER_SIZE];
} else {
buffer = new byte[(int) (mSize - mDownloaded)];
}
// Read from server into buffer.
int read = stream.read(buffer);
if (read == -1)
break;// EOF, break while loop
// Write buffer to file.
file.write(buffer, 0, read);
mDownloaded += read;
double speedInKBps = 0.0D;
try {
long timeInSecs = (System.currentTimeMillis() - startTime) / 1000; //converting millis to seconds as 1000m in 1 second
speedInKBps = (mDownloaded / timeInSecs) / 1024D;
} catch (ArithmeticException ae) {
}
this.mListener.publishProgress(this.getProgress(), this.getTotalSize(), speedInKBps);
}
回答by Anirudh Ramanathan
I can give you a general idea. Start a timer at the beginning of the download. Now, multiply the (percentage downloaded)
by the download size
, and divide it by the time elapsed.
That gives you average download time. Hope I get you on the right track!
我可以给你一个大致的想法。在下载开始时启动计时器。现在,在乘(percentage downloaded)
用download size
,并通过划分它time elapsed.
那让你的平均下载时间。希望我能让你走上正轨!
You can use System.nanoTime();
as suggested by Brian.
您可以System.nanoTime();
按照布赖恩的建议使用。
Put long startTime = System.nanoTime();
outside your while loop. and
放在long startTime = System.nanoTime();
你的 while 循环之外。和
long estimatedTime = System.nanoTime() - startTime;
will give you the elapsed time within your loop.
long estimatedTime = System.nanoTime() - startTime;
将为您提供循环内经过的时间。