Networking 如何计算Android网络吞吐量

Networking 如何计算Android网络吞吐量,networking,throughput,Networking,Throughput,我正在计算网络吞吐量 吞吐量=总数据包(接收)/总消耗时间 我已经与服务器创建了一个专用套接字,并上传和下载了文件。我得到的输出不合格 代码如下: public void uploadTest() throws Exception { byte[] arrayOfByte = new byte[BUF_SIZE]; Utility.debugMessage(getName()+" uploadTest getLocalAddress() = "+ mUploadSocke

我正在计算网络吞吐量

吞吐量=总数据包(接收)/总消耗时间

我已经与服务器创建了一个专用套接字,并上传和下载了文件。我得到的输出不合格 代码如下:

public void uploadTest() throws Exception
{
      byte[] arrayOfByte = new byte[BUF_SIZE];
      Utility.debugMessage(getName()+" uploadTest getLocalAddress() = "+ mUploadSocket.getLocalAddress()+" getLocalPort() = "+ mUploadSocket.getLocalPort()+" isBound() = "+mUploadSocket.isBound()+" isConnected() = "+ mUploadSocket.isConnected());
      DataOutputStream localDataOutputStream = new DataOutputStream(mUploadSocket.getOutputStream());
      sendMessage(Constants.MSG_CONN_LATENCY,(int)initiatingConnectionTime/* (System.currentTimeMillis()-initiatingConnectionTime)*/,-1,null);
      String uploadFilePath=null;

      uploadFilePath= Utility.createFileOnSdCard(testCaseDetails.getFileSize());

    int totalCycle=testCaseDetails.getCycle();
    double allCycleThroughPut=0;
      Utility.debugMessage(getName()+" uploadTest ToTal Available bytes","localDataInputStream.available() :: "+localDataOutputStream.size());
      for(int i=1;i<=totalCycle;i++)
      {
      long totalTime=0L;`enter code here`
      long updateDelta=0;
      long bytesRead=0L;
      long totalReceived=0L;
      File testFile = new File(uploadFilePath);
      fileSize=testFile.length();
      FileInputStream fis = new FileInputStream(testFile);
      BufferedInputStream bis = new BufferedInputStream(fis);
      while((bytesRead=bis.read(arrayOfByte))>Constants.NO_DATA)
      {
          long currentTime=System.currentTimeMillis();
          localDataOutputStream.write(arrayOfByte,0,(int)bytesRead);
          totalReceived+=bytesRead;
          int progress=(int)((totalReceived/(double)fileSize)*100);
          Utility.debugMessage(NAME+" uploadTest Downloading  "," progress = "+progress+"totalSend = "+totalReceived);
          updateDelta =System.currentTimeMillis() - currentTime;

          if(sendYUpdate)
          {//Check y axis max value
                  sendMessage(Constants.MSG_UPDATE_Y_AXIS_LIMIT,(int)(Utility.calculate(updateDelta, bytesRead).getKilobits()),-1,  null);
              if(skipCounter==0)
              {
                  sendYUpdate=false;
              }
              else
              {
                  skipCounter--;
              }
          }
          else
          {

              sendMessage(Constants.MSG_UPDATE_STATUS,progress,(int) totalReceived, Utility.calculate(updateDelta, bytesRead));

          }
          totalTime+=updateDelta;
      }

      bis.close();
      long downloadTime=totalTime;

      SpeedInfo currentAverageThrougput=Utility.calculate(downloadTime, totalReceived);
      allCycleThroughPut+=currentAverageThrougput.getKilobits();
      sendMessage(Constants.MSG_CYCLE_STATUS, -1,i,currentAverageThrougput );
      sendYUpdate=true;
      }
      localDataOutputStream.close();

      sendMessage(Constants.MSG_COMPLETE_STATUS,(int)(allCycleThroughPut/totalCycle),-1,null);

        if(!Constants.RUN_ON_LOCAL)
            {

                    try {

                        Utility.closeSocket( con,Utility.getSharePrefValue(con, Constants.SERVER_BASE_URL)+"/TestMetrico/metrico/closeConnection",Integer.parseInt(Utility.getSharePrefValue(con, Constants.SOCKET_SERVER_PORT)));
                    } catch (Exception e1) {
                        e1.printStackTrace();
                        }

        }
}


public void downloadTest() throws IOException
{
      int totalCycle=testCaseDetails.getCycle();
         double allCycleThroughPut=0;
     for(int i=1;i<=totalCycle;i++)
     {
      byte[] arrayOfByte = new byte[BUF_SIZE];
      if(mDownloadSocket.isClosed())
      {
          mDownloadSocket=null;
          mDownloadSocket=new Socket();
          connectDownload();
      }

      Utility.debugMessage(NAME+" downloadTest Start Downloading LocalAddress = "+ mDownloadSocket.getLocalAddress()+" getLocalPort = "+mDownloadSocket.getLocalPort()+" isBound = "+mDownloadSocket.isBound()+" isConnected = "+mDownloadSocket.isConnected());
      DataInputStream localDataInputStream = new DataInputStream(mDownloadSocket.getInputStream());
      sendMessage(Constants.MSG_CONN_LATENCY,(int) initiatingConnectionTime /*(System.currentTimeMillis()-initiatingConnectionTime)*/,Constants.PARAMETER_NOT_USED,null);

      Utility.debugMessage(NAME+" downloadTest ToTal Available bytes","localDataInputStream.available() :: "+localDataInputStream.available());



      float updateDelta=0;
      long received=0L;
      long totalTime=0L;
      long totalReceived=0L;
      long currentTime=System.currentTimeMillis();
      while (( received=localDataInputStream.read(arrayOfByte)) > Constants.NO_DATA)
      {
          totalReceived+=received;
          int progress=(int)((totalReceived/(double)1048576)*100);

          updateDelta = System.currentTimeMillis()-currentTime;

          Utility.debugMessage(NAME+" downloadTest Downloading buffer", "Received bytes= "+received+"totalReceived bytes= "+totalReceived+"updateDelta time = "+updateDelta);
          if(sendYUpdate)
          {//Check y axis max value
                  sendMessage(Constants.MSG_UPDATE_Y_AXIS_LIMIT,(int)( Utility.calculate(updateDelta, received).getKilobits()),-1,  null);
              if(skipCounter==0)
              {
                  sendYUpdate=false;
              }
              else
              {
                  skipCounter--;
              }
          }
          else
          {

              sendMessage(Constants.MSG_UPDATE_STATUS,progress,(int) totalReceived,  Utility.calculate(updateDelta, received));

          }

          totalTime+=updateDelta;
          currentTime=System.currentTimeMillis();
      } 


      long downloadTime=totalTime;//(totalTime-start);
      SpeedInfo currentAverageThrougput=Utility.calculate(downloadTime, totalReceived);
      allCycleThroughPut+=currentAverageThrougput.getKilobits();
      sendMessage(Constants.MSG_CYCLE_STATUS, -1,i,currentAverageThrougput );
      sendYUpdate=true;

      localDataInputStream.close();
      mDownloadSocket.close();
      }


      sendMessage(Constants.MSG_COMPLETE_STATUS,(int)(allCycleThroughPut/totalCycle),-1,null);

        if(!Constants.RUN_ON_LOCAL)
            {
                    try {

                        Utility.closeSocket( con,Utility.getSharePrefValue(con, Constants.SERVER_BASE_URL)+"/TestMetrico/metrico/closeConnection",Integer.parseInt(Utility.getSharePrefValue(con, Constants.SOCKET_SERVER_PORT)));
                    } catch (Exception e1) {
                        e1.printStackTrace();
                        }

        }

}


    public static SpeedInfo calculate(final float downloadTime,
            final float bytesIn) {

        SpeedInfo info = new SpeedInfo();
        float downloadTimeTemp = downloadTime;
        // if(downloadTimeTemp==0) downloadTimeTemp=1;
        // from mil to sec
        // TimeUnit.MILLISECONDS.toSeconds(downloadTime)
        long bytespersecond = (long) (bytesIn / downloadTimeTemp) * 1000;
        double kilobits = bytespersecond * Constants.BYTE_TO_KILOBIT;
        double kilobytes = bytespersecond * Constants.BYTE_TO_KILOBYTE;// Constants.BYTE_TO_KILOBIT;
        //double megabits = kilobits * Constants.KILOBIT_TO_MEGABIT;

        double megabits = bytespersecond*0.000000953674;
        info.setDownspeed(bytespersecond);
        info.setKilobits(kilobits);
        info.setMegabits(megabits);

        TotalKB += kilobits;
        TotalTime += downloadTime;
        Utility.debugMessage(" downloadTest time in Downloading buffer",
                "bytespersecond = " + bytespersecond);
        Utility.debugMessage(" downloadTest time in Downloading buffer",
                "kilobits = " + kilobits);
        Utility.debugMessage(" Total downloadTest time in Downloading buffer TotalKB = "
                + TotalKB + " , TotalTime = " + TotalTime);
        return info;
    }

    public static  final double EDGE_THRESHOLD = 176.0;
    public static  final double BYTE_TO_KILOBIT = 0.0078125;
    public static  final double KILOBIT_TO_MEGABIT = 0.0009765625;
    public static final double BYTE_TO_KILOBYTE=0.000976562;
public void uploadTest()引发异常
{
byte[]arrayOfByte=新字节[BUF_大小];
实用工具.debugMessage(getName()+“uploadTest getLocalAddress()=”+mUploadSocket.getLocalAddress()+“getLocalPort()=”+mUploadSocket.getLocalPort()+“isBound()=”+mUploadSocket.isBound()+“isConnected()=”+mUploadSocket.isConnected());
DataOutputStream localDataOutputStream=新的DataOutputStream(mUploadSocket.getOutputStream());
sendMessage(常数.MSG_CONN_LATENCY,(int)initiatingConnectionTime/*(System.currentTimeMillis()-initiatingConnectionTime)*/,-1,null);
字符串uploadFilePath=null;
uploadFilePath=Utility.createFileOnSdCard(testCaseDetails.getFileSize());
int totalCycle=testCaseDetails.getCycle();
double AllCycleThroutput=0;
debugMessage(getName()+“uploadTest可用字节总数”、“localDataInputStream.Available()::”+localDataOutputStream.size());
对于(int i=1;iConstants.NO_数据)
{
长currentTime=System.currentTimeMillis();
write(arrayOfByte,0,(int)字节读取);
totalReceived+=字节读取;
int进度=(int)((总接收/(双)文件大小)*100);
实用程序.debugMessage(NAME+“uploadTest下载”,“progress=“+progress+”totalSend=“+totalReceived”);
updateDelta=System.currentTimeMillis()-currentTime;
如果(发送更新)
{//检查y轴最大值
sendMessage(Constants.MSG\u UPDATE\u Y\u AXIS\u LIMIT,(int)(Utility.calculate(updateDelta,bytesRead.getKilobits()),-1,null);
如果(skipCounter==0)
{
sendYUpdate=false;
}
其他的
{
skipCounter--;
}
}
其他的
{
sendMessage(Constants.MSG_UPDATE_STATUS,progress,(int)totalReceived,Utility.calculate(updateDelta,bytesRead));
}
totalTime+=updateDelta;
}
二、关闭();
长下载时间=总时间;
SpeedInfo CurrentAverageThroughput=实用程序.calculate(下载时间,totalReceived);
AllCycleThrowput+=CurrentAverageThroughput.getKilobits();
sendMessage(Constants.MSG\u CYCLE\u STATUS,-1,i,CurrentAverageThroughput);
sendYUpdate=true;
}
localDataOutputStream.close();
sendMessage(Constants.MSG_COMPLETE_STATUS,(int)(AllCycleThroutput/totalCycle),-1,null);
如果(!Constants.RUN_ON_LOCAL)
{
试一试{
closeSocket(con,Utility.getSharePrefValue(con,Constants.SERVER_BASE_URL)+“/TestMetrico/metrico/closeConnection”,Integer.parseInt(Utility.getSharePrefValue(con,Constants.SOCKET_SERVER_PORT));
}捕获(异常e1){
e1.printStackTrace();
}
}
}
public void downloadTest()引发IOException
{
int totalCycle=testCaseDetails.getCycle();
double AllCycleThroutput=0;
对于(int i=1;i常数。无数据)
{
totalReceived+=已接收;
内部进度=(内部)(总接收/(双)1048576)*100);
updateDelta=System.currentTimeMillis()-currentTime;
实用程序.debugMessage(NAME+“downloadTest下载缓冲区”,“Received bytes=“+Received+”totalReceived bytes=“+totalReceived+”updateDelta time=“+updateDelta”);
如果(发送更新)
{//检查y轴最大值
sendMessage(Constants.MSG\u UPDATE\u Y\u AXIS\u LIMIT,(int)(Utility.calculate(updateDelta,received.getKilobits()),-1,null);
如果(skipCounter==0)
{
sendYUpdate=false;
}
其他的
{
skipCounter--;
}
}
其他的
{
sendMessage(Constants.MSG_UPDATE_STATUS,progress,(int)totalReceived,Utility.calculate(updateDelta,received));
}
totalTime+=updateDelta;
currentTime=System.currentTimeMillis();
} 
长下载时间=总时间;//(总时间开始);
SpeedInfo CurrentAverageThroughput=实用程序.calculate(下载时间,totalReceived);
AllCycleThrowput+=CurrentAverageThroughput.getKilobits();
sendMessage(Constants.MSG\u CYCLE\u STATUS,-1,i,CurrentAverageThroughput);
sendYUpdate=true;
localDataInputStream.close();
mDownloadSocket.close();
}
sendMessage(Constants.MSG_COMPLETE_STATUS,(int)(AllCycleThroutput/totalCycle),-1,null);
如果(!Constants.RUN_ON_LOCAL)
{
试一试{
closeSocket(con,Utility.getSharePrefValue(con,Constants.SERVER_BASE_URL)+“/TestMetrico/metrico/closeConnection”,Integer.parseInt(Utility.getSharePrefValue(con,Constants.SOCKET_SERVER_PORT));
}捕获(异常e1){
e1.printStackTrace();
}
}
}
公共静态速度信息计算(最终浮动下载时间,
最终浮点数(字节){
SpeedInfo=新SpeedInfo();
浮动下载时间温度=下载时间;
//如果(downloadTimeTemp==0)downloadTimeTemp=1;
//从密耳到秒
//时间单位。毫秒。至秒(下载时间)
long bytespersecond=(long)(bytesIn/downloadTimeTemp)*1000;
双千位=字节秒*常数。字节到千位;
double KB=bytespersecond*常量。字节到字节;//常量。字节到字节;
//双兆比特=千比特*常数。千比特到兆比特;
双兆字节=字节秒*0.000000953674;
信息集