首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android网络速度测试

Android网络速度测试
EN

Stack Overflow用户
提问于 2015-09-14 22:43:10
回答 1查看 2.2K关注 0票数 0

我正在开发一个像OKLA应用程序 (http://www.speedtest.net/)这样的速度测试应用程序。

我一直试图用最常用的方法来获取带宽:

  1. 下载前有时间。
  2. 下载一些文件一段时间X。
  3. 获取下载后的时间和下载的总大小。
  4. 从时间和接收到的字节计算速度。

另外,我在两个不同的线程中同时执行这一点,因为需要饱和de连接才能获得良好的结果。

这种方法在PC环境中很好地使用了这个JAVA代码:

代码语言:javascript
运行
复制
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

public class TestVelocidad {

static long totalBytesReceived = 0; //
static long startSample; 
static long endSample ; 
private static final long TIME_FOR_DOWNLOAD_MILISECONDS = (long) 10000.0;      
private static final long MILI_TO_NANO = 1000000; 

public static void main(String[] args) throws InterruptedException, ExecutionException {


    try{
        final ExecutorService service;
        String downloadFileUrl100MB = "http://cachefly.cachefly.net/100mb.test";


        startSample = System.nanoTime();

        service = Executors.newFixedThreadPool(6);
        FutureTask futureTask_1 = new FutureTask(new SpeedTestThread(downloadFileUrl100MB));
        service.execute(futureTask_1);
        FutureTask futureTask_2 = new FutureTask(new SpeedTestThread(downloadFileUrl100MB));
        service.execute(futureTask_2);

        service.shutdownNow();
        long result1 = (Long) futureTask_1.get();
        long result2 = (Long) futureTask_2.get();

        endSample = System.nanoTime();



       long timeSpent = (long) endSample-startSample;
            long totalBytesReceived = result1 + result2;
            System.out.println("Time of threads: " + timeSpent/1000000000.0   + " seconds " + "\nbytes received: " + (totalBytesReceived) );
            double calculatedSpeed;
//            long finalTimeSpent ;
//            finalTimeSpent = (long) ((TIME_FOR_DOWNLOAD_MILISECONDS * MILI_TO_NANO - diff));
            calculatedSpeed =  SpeedInfo.calculate(timeSpent, totalBytesReceived).megabits;
            System.out.println("Velocidad calculada: " + calculatedSpeed   + " mbps" );

    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

}

}


class SpeedTestThread implements Callable<Long> {

private String url = new String("");
private static final long TIME_FOR_DOWNLOAD_NANOSECONDS = (long) 10000000000.0;
private static final long MILI_TO_NANO = 1000000;
private long bytesThread;

public SpeedTestThread(String urlToDownload){
    url = urlToDownload;
}

public void run() {


}

@Override
public Long call() throws Exception {


    System.out.println("FileDownload " +  " File to download: " + url );
    InputStream stream = null;
    long startCon = System.nanoTime();
    URL urlToDownload = null;
    try {
        urlToDownload = new URL(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    URLConnection con = null;
    try {
        con = urlToDownload.openConnection();
    } catch (IOException e) {
        e.printStackTrace();
    }
    con.setUseCaches(false);
    //Tiempo de acceso al archivo.
    long connectionLatency = (System.nanoTime() - startCon)/MILI_TO_NANO;
    System.out.println("Connection latency = " + connectionLatency + "");

    con.setConnectTimeout(5000);

    try {
        stream = con.getInputStream();
    } catch (IOException e) {
        e.printStackTrace();
    }
    long startNano = System.nanoTime();



    int currentByte = 0;
    try {

        while ((currentByte = stream.read()) != -1 ) {
            bytesThread++;
            if ((System.nanoTime() - startNano) > TIME_FOR_DOWNLOAD_NANOSECONDS){
                System.out.println("Time");
                break;
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("Thread bytes received:  " + bytesThread);
    return bytesThread;
}
}



class SpeedInfo {
public double kilobits = 0;
public double megabits = 0;
public double downspeed = 0;
private static final double BYTE_TO_KILOBIT = 0.008;
private static final double KILOBIT_TO_MEGABIT = 0.001;

/**
 * 1 byte = 0.0078125 kilobits
 * 1 kilobits = 0.0009765625 megabit
 *
 * @param downloadTime in miliseconds
 * @param bytesIn      number of bytes downloaded
 * @return SpeedInfo containing current testVelocidadThread
 */
public static SpeedInfo calculate(final long downloadTime, final long bytesIn) {
    SpeedInfo info = new SpeedInfo();
    //from mil to sec
    System.out.println("Bytes transferidos: " + bytesIn + "Tiempo de descarga: " + downloadTime/1000000000);
    double time = downloadTime;
    double byteIn1 = bytesIn;
    double division = (double)(byteIn1 / time);
    double bytespersecond = ((division) * 1000000000);
    double kilobits = bytespersecond * BYTE_TO_KILOBIT;
    double megabits = kilobits * KILOBIT_TO_MEGABIT;
    info.downspeed = bytespersecond;
    info.kilobits = kilobits;
    info.megabits = megabits;
    return info;
}
}

问题是,当我在一个安卓应用程序上运行这个程序时,我在具有更多处理能力和内存容量的手机上获得了良好的效果,而在容量较低的手机上,效果较差。

在大多数android手机上有什么好的想法来取得好的效果吗?

EN

回答 1

Stack Overflow用户

发布于 2022-02-08 09:24:34

尝试使用java nio而不是java io下载该文件。

java首先将文件传输到内存中,这使得低端设备的性能较差。

当java nio使用通道时,您可以将文件传输到存储中,这将使所有设备的性能大致相同。

使用以下代码:

代码语言:javascript
运行
复制
len = out.getChannel().transferFrom(readableByteChannel , seekPos , Long.MAX_VALUE);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32575058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档