netperf备忘。
服务端:在网站http://blog.csdn.net/hjason2042/article/details/7741028里链接地址NetPerf-2.4.5-w32.zip(http://i18n-zh.googlecode.com/files/NetPerf-2.4.5-w32.zip)下载安装windows的netServer。解压zip。我解压在D:\pakeage\NetPerf-2.4.5-w32目录下。
打开CMD,进入D:\pakeage\NetPerf-2.4.5-w32>。再输入netserver.exe。这时候是会出现“netserver: fopen of debug file as new stdout failed!: 系统找不到指定的路径。”的问题,如上面给的网站里解决的办法就是在C盘创建文件夹temp即可。再输入netserver.exe 【即D:\pakeage\NetPerf-2.4.5-w32>netserver.exe】就进入服务器的监听状态。
客户端:在Android源码的\external\netperf\目录下的文件拷贝到工程jni下,用cygwin编译生成netperf二进制文件,netperf拷贝到/data/data/目录下(拷贝方法:CMD下进入工程的libs目录下下:adb push netperf /data/data/ )。这时候netperf的环境就搭建好了。
adb shell进入输入命令状态:
root@android:/ # cd data/data
root@android:/data/data # ./netperf -H 192.168.1.118
...............................................输出值............................................................................................................................................
./netperf -H 192.168.1.118
TCP STREAM TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 192.168.1.118 (192.168.1.118) port 0 AF_INET
Recv Send Send
Socket Socket Message Elapsed
Size Size Size Time Throughput
bytes bytes bytes secs. 10^6bits/sec
8192 524288 524288 10.07 34.44
OK。。。。
附上我编译的android的客户端netperf 二进制文件(2.4.4版本)
http://download.csdn.net/detail/zyp009/6805433
最近发展:
Android环境下,将放在Android工程下的assets目录的可执行二进制文件netperfcopy到apk下的私有文件目录下:
/**
* 复制assets目录下的netperf到私有文件目录下/data/data/pakeage_/files/netperf。
*/
private boolean cpNetperfLib(){
String path = TestBroadBand.this.getApplicationContext().getFilesDir()
.getAbsolutePath()+ "/netperf"; //data/data/包名/files/
File file = new File(path);
if(file.exists()){
return true;
}
FileOutputStream out = null;
InputStream in = null ;
try {
in = TestBroadBand.this.getAssets().open("netperf"); //从assets目录下复制
out = new FileOutputStream(file);
int length = -1;
byte[] buf = new byte[1024];
while ((length = in.read(buf)) != -1){
out.write(buf, 0, length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}finally{
try {
out.flush();
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
再将/data/data/pakeage_/files/netperf设置为可读写。
/**
* 执行chmod 777 netperf
*/
private boolean chmodExec() {
Process process = null;
try {
String path = TestBroadBand.this.getApplicationContext().getFilesDir()
.getAbsolutePath();
String cmd = "chmod 777 "+path+"/netperf \n";
process = Runtime.getRuntime().exec(cmd);
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (InterruptedException e) {
e.printStackTrace();
return false;
}finally {
try {
process.destroy();
} catch (Exception e) {
}
}
return true;
}
而后在服务器开启的状态就可以发送命令:执行/data/data/pakeage_/files/netperf -H 【ip】,不需要root:
/**
* 执行/data/data/pakeage_/files/netperf -H ip
*获得带宽值
*/
private double netperfExec(String address) {
Process process = null;
String foutStr = "";
String data = "";
try {
String path = TestBroadBand.this.getApplicationContext().getFilesDir()
.getAbsolutePath();
String cmd = path+"/netperf -H "+" "+address;
process = Runtime.getRuntime().exec(cmd);
InputStreamReader r = new InputStreamReader(
process.getInputStream());
LineNumberReader returnData = new LineNumberReader(r);
String line = "";
String lineLast = "";
while ((line = returnData.readLine()) != null) {
System.out.println(line);
foutStr += line;
lineLast = line;
}
if(foutStr.contains("Recv") && foutStr.contains("Send")
&& foutStr.contains("Socket") && foutStr.contains("Size")
&& foutStr.contains("Elapsed") && foutStr.contains("Throughput")
&& foutStr.contains("bytes")){
String strSp[] = lineLast.trim().split(" "); //处理最后一行数值
if(strSp.length>0)
data = strSp[strSp.length-1];
}
// returnMsg = "Recv Send Send
// Socket Socket Message Elapsed
// Size Size Size Time Throughput
// bytes bytes bytes secs. 10^6bits/sec
//
// 8192 524288 524288 11.31 4.32";
} catch (IOException e) {
e.printStackTrace();
return -1;
}finally {
try {
process.destroy();
} catch (Exception e) {
}
}
if(data.equalsIgnoreCase("") || data.equalsIgnoreCase(" "))
return -1;
return Double.valueOf(data);
}