Android中的上载和下载速率分析 [英] Upload and Download rate profiling in Android
问题描述
我正在尝试开发一个Android应用程序,以测量网络的上载和下载速度,可能我需要每隔2秒钟左右运行和配置一次. SpeedTest.net
应用程序是类似工作的理想应用程序,但是它不是开源的.而且,我需要每2秒运行一次.完成测试需要几秒钟.
I'm trying to develop an Android app to measure the upload and download speed of my network, which probably I would need to run and profile it every 2 seconds or so. SpeedTest.net
app is an ideal app working similarly, however, it is not open-source. Moreover, I would need it to be run every 2 seconds. It takes couple of seconds to finish test.
我该怎么做?目前,我只是在Internet上某个地方下载一个小的随机.txt文件,并以size/time-to-download
表示下载速率.但是我每次都会得到奇怪的结果.显然,这种方法行不通.
How can I do that? Currently I'm just downloading a small random .txt file found somewhere on Internet and measure size/time-to-download
as a measure of download rate. But I get weird results every time. Apparently this approach is not working.
更新:下载完成.有关如何实现上传速度的任何建议?
UPDATE: download is done. Any advices on how to implement the upload speeds?
推荐答案
您需要下载一个相当大的文件,下载该文件至少需要15秒.文件越大,您收到的效果越好.使用具有高可用性的永远在线服务器.另外,仅在网络调用时使用累积(我相信您必须使用某些套接字来读取while
循环.因此,在socket.read()
之前和之后执行System.currentTimeMillis()
并继续添加它们)
You need to download a considerably large file, something that takes atleast 15 seconds to download. The bigger the file, the better result you would receive. Use an always-on server with high availability. Also, use accumulation on the time of your network calls only (I believe that you must be using some socket to read in a while
loop. So do System.currentTimeMillis()
before and after socket.read()
and keep adding them)
这几乎也是SpeedTest.net
所做的
就上传而言,您可以做同样的事情.粗略的伪代码:
As far as upload is concerned, you can do the same thing. A rough pseudo code :
upload (String remote, InputStream localfile){
Socket s = openDataConnection(remote);
OutputStream os = new BufferedOutputStream (s.getOutputStream(), MAX_BUFFER_SIZE);
byte[] buffer = new byte[MAX_BUFFER_SIZE];
long totalTime = 0L;
while((buffer = localfile.read())!= -1){
long startTime = System.currentTimeMillis();
os.write(buffer);
long endTime = System.currentTimeMillis();
totalTime += (endTime - startTime);
}
}
这篇关于Android中的上载和下载速率分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!