다운로드 및 업로드를 위해 네트워크 연결 속도를 분석하는 Android 애플리케이션에서 AsyncTask 클래스를 수행하려고합니다. 지금 다운로드 부분에 대한 작업을하고 있지만 기대했던 결과를 얻지 못하고 있습니다. 나는 15Mbps 다운/업 속도를 일관되게 유지하는 Wi-Fi 네트워크를 테스트 중이지만, 애플리케이션에서 얻는 결과는 겨우 1Mpbs 정도입니다. 내가 속도 테스트 APK를 실행할 때 약 3.5Mbps의 속도로 테스트하고있다. 이 기능은 작동하며 속도가 절반에 불과합니다. 다음 코드가 정확한 결과를 산출해야합니까? 도움에 미리Java/Android로 다운로드 속도를 정확하게 측정하는 방법
try {
String DownloadUrl = "http://ipv4.download.thinkbroadband.com:8080/5MB.zip";
String fileName = "testfile.bin";
File dir = new File (context.getFilesDir() + "/temp/");
if(dir.exists()==false) {
dir.mkdirs();
}
URL url = new URL(DownloadUrl); //you can write here any link
File file = new File(context.getFilesDir() + "/temp/" + fileName);
long startTime = System.currentTimeMillis();
Log.d("DownloadManager", "download begining: " + startTime);
Log.d("DownloadManager", "download url:" + url);
Log.d("DownloadManager", "downloaded file name:" + fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
//Define InputStreams to read from the URLConnection.
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
//Read bytes to the Buffer until there is nothing more to read(-1).
ByteArrayBuffer baf = new ByteArrayBuffer(1024);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
long endTime = System.currentTimeMillis(); //maybe
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
File done = new File(context.getFilesDir() + "/temp/" + fileName);
Log.d("DownloadManager", "Location being searched: "+ context.getFilesDir() + "/temp/" + fileName);
double size = done.length();
if(done.exists()) {
done.delete();
}
Log.d("DownloadManager", "download ended: " + ((endTime - startTime)/1000) + " secs");
double rate = (((size/1024)/((endTime - startTime)/1000)) * 8);
rate = Math.round(rate * 100.0)/100.0;
String ratevalue;
if(rate > 1000)
ratevalue = String.valueOf(rate/1024).concat(" Mbps");
else
ratevalue = String.valueOf(rate).concat(" Kbps");
Log.d("DownloadManager", "download speed: "+ratevalue);
} catch (IOException e) {
Log.d("DownloadManager", "Error: " + e);
}
예 출력
10-08 15:09:52.658: D/DownloadManager(13714): download ended: 70 secs
10-08 15:09:52.662: D/DownloadManager(13714): download speed: 585.14 Kbps
감사합니다. 더 나은 방법이 있다면 알려 주시기 바랍니다.
한 번에 한 바이트 씩 읽는다면 상당히 길어질 수 있습니다. 바이트 []를 사용하지 않고 한 번에 여러 KB를 읽는 것이 어떻습니까? – njzk2
일반적으로 매초마다'read'와'append' 호출이 ~ 600K입니다. – njzk2
또한'size','endTime' 및'startTime'가 1024, 1000 및 8처럼 정수이기 때문에 비율 값이 정확하지 않을 수 있습니다. 따라서 long으로 형변환되기 전에 정수로 계산됩니다 부정확 한 mesure를 제공해야하지만 관찰 한 큰 차이에 대해서는 책임을지지 않습니다. – njzk2