Upload and Download rate profiling in Android [closed]

 ̄綄美尐妖づ 提交于 2019-12-07 01:46:28

问题


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.

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?


回答1:


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)

This is pretty much what SpeedTest.net does as well

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);
     }
}



回答2:


To do the uploads, you will need to set up a two-way communication with the server. I would do it with a simple loop:

while(x){
Starttime =getCurrentTime()
Sendfile() //Send a Xmb file that the server can verify
waitForVerification() // Wait for a reply from server.
compareCurrentTimeWithStartingTime() // compare the times.
}


来源:https://stackoverflow.com/questions/35536777/upload-and-download-rate-profiling-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!