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

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值