Android 使用iperf测试wifi吞吐量

Android使用iperf测试wifi吞吐量

大体上分为三个步骤

  1. 编译生成android端的可执行文件iperf
  2. 把该文件拷贝到/data/data/包名/iperf目录下
  3. 根据iperf命令执行该文件

第一部分,在android下编译生成iperf文件

1.获取需要编译的源代码:https://osdn.net/projects/sfnet_iperf/downloads/iperf-2.0.5.tar.gz/

2.将下载来的文件解压后放置到android/external目录下

3.在该android/external/iperf-2.0.5目录下创建Android.mk文件

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS) 
LOCAL_SRC_FILES := compat/Thread.c \
    compat/error.c \
    compat/delay.cpp \
    compat/gettimeofday.c \
    compat/inet_ntop.c \
    compat/inet_pton.c \
    compat/signal.c \
    compat/snprintf.c \
    compat/string.c \
    src/Client.cpp \
    src/Extractor.c \
    src/Launch.cpp \
    src/List.cpp \
    src/Listener.cpp \
    src/Locale.c \
    src/PerfSocket.cpp \
    src/ReportCSV.c \
    src/ReportDefault.c \
    src/Reporter.c \
    src/Server.cpp \
    src/Settings.cpp \
    src/SocketAddr.c \
    src/main.cpp \
    src/sockets.c \
    src/stdio.c \
    src/tcp_window_size.c \
    src/gnu_getopt.c \
    src/gnu_getopt_long.c \
    src/service.c
LOCAL_C_INCLUDES += \
    $(LOCAL_PATH) \
    $(LOCAL_PATH)/include
LOCAL_CFLAGS += -O2 
LOCAL_CFLAGS += -DHAVE_CONFIG_H
LOCAL_SHARED_LIBRARIES := libc libm libcutils libnetutils 
LOCAL_MODULE := iperf
LOCAL_MODULE_TAGS := debug
include $(BUILD_EXECUTABLE) 

4.4.生成需要的头文件

./configure --host=arm 

5.修改源码的头文件:

1. 把 compact/signal.c 里面的 #include "util.h"
改成 #include "../include/util.h"

2. 把 src/sockets.c 里面的 #include "util.h"
改成 #include "../include/util.h"

6.在android目录下编译生成iperf

$source build/envsetup.sh
$mmm external/iperf-2.0.5

7.会在out里面目录下生成一个iperf文件,这个文件就是要拷贝到/data/data…目录的文件

第二部分,copy iperf

 /*** 1. 拷贝iperf到该目录下*/
private static final String IPERF_PATH = "/data/data/com.android.wifiiperf/iperf";

public void copyiperf() {
    File localfile;
    Process p;
    try {
        localfile = new File(IPERF_PATH);
        p = Runtime.getRuntime().exec("chmod 777 " + localfile.getAbsolutePath());
        InputStream localInputStream = getAssets().open("iperf");
        Log.i(TAG,"chmod 777 " + localfile.getAbsolutePath());
        FileOutputStream localFileOutputStream = new FileOutputStream(localfile.getAbsolutePath());
        FileChannel fc = localFileOutputStream.getChannel();
        FileLock lock = fc.tryLock(); //给文件设置独占锁定
        if (lock == null) {
            Toast.makeText(this,"has been locked !",Toast.LENGTH_SHORT).show();
            return;
        } else {
            FileOutputStream fos = new FileOutputStream(new File(IPERF_PATH));
            byte[] buffer = new byte[1024];
            int byteCount = 0;
            while ((byteCount = localInputStream.read(buffer)) != -1) {// 循环从输入流读取
                // buffer字节
                fos.write(buffer, 0, byteCount);// 将读取的输入流写入到输出流
                Log.i(TAG, "byteCount: "+byteCount);
            }
            fos.flush();// 刷新缓冲区
            localInputStream.close();
            fos.close();

        }
        //两次才能确保开启权限成功
        p = Runtime.getRuntime().exec("chmod 777 " + localfile.getAbsolutePath());
        lock.release();
        p.destroy();
        Log.i(TAG, "the iperf file is ready");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

第三部分,执行iperf文件,其中CommandResult、CommandHelper和iperf文件在GitHub里面了。

/**
 * 2. 在Android应用中执行iperf命令
 */
private void sercomfun(final String cmd) {
    Log.i(TAG, "sercomfun = " + cmd);
    Thread lthread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                String errorreply = "";
                CommandHelper.DEFAULT_TIMEOUT = 150000;
                CommandResult result = CommandHelper.exec(cmd);
                if (result != null) {
                    //start to connect the service
                    if (result.getError() != null) {
                        errorreply = result.getError();
                        Message m = new Message();
                        m.obj = errorreply;
                        m.what = IPERF_ERROR;
                        handler.sendMessage(m);
                        Log.i(TAG,"Error:" + errorreply);
                    }
                    if (result.getOutput() != null) {
                        iperfreply = getThroughput(result.getOutput());
                        IPERF_OK = true;
                        Message m = new Message();
                        m.obj = iperfreply;
                        m.what = IPERF_SCCESS;
                        handler.sendMessage(m);
                        Log.i(TAG,"Output:" + iperfreply);
                    }
                    Log.i(TAG,"result.getExitValue(): "+result.getExitValue());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    lthread.start();
}

GitHub项目地址(包括wifi地址,信号),https://github.com/xhunmon/wifiIperf

你可能感兴趣的:(android)