在用monkey做android压力测试时,一般都会选择直接运行monkey命令的形式,由于重复性较强,我就写了个monkey可视化运行的工具,monkey参数可自行调整。下面先附上效果图:
以下为代码:
1.cmd命令执行工具类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class CmdUtils {
ListresultList=new ArrayList();
public List excuteCmd(String command){
//String command="adb devices";
String line = null;
StringBuilder sb = new StringBuilder();
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec(command);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = bufferedReader.readLine()) != null) {
resultList.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return resultList;
}
}
2.获取连接设备工具类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class AndroidDevicesInfo {
ListdevicesList=new ArrayList();
public List getAndroidDevicesInfo(){
String command="adb devices";
String line = null;
StringBuilder sb = new StringBuilder();
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec(command);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = bufferedReader.readLine()) != null) {
if(line.contains("device")){
if(!line.contains("devices")){
String deviceId=line.substring(0, line.indexOf("device"));
devicesList.add(deviceId);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return devicesList;
}
}
3.加载指定设备的应用包集合
package MonkeyTool;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class AndroidPackageInfo {
ListpackageList=new ArrayList();
//获取指定设备下的包集合
public List getAndroidPackageInfo(String deviceId){
String command="adb -s "+deviceId+" shell pm list packages";
String line = null;
StringBuilder sb = new StringBuilder();
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec(command);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = bufferedReader.readLine()) != null) {
if(line.contains("package")){
//截取包字符串放入list中
String packageItem=line.substring(line.indexOf("package")+8,line.length());
//String packageItem=line;
packageList.add(packageItem);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return packageList;
}
}
4.加载设备线程
package MonkeyTool;
import java.util.List;
import javax.swing.JComboBox;
//加载设备任务
public class DevicesThread extends Thread{
AndroidDevicesInfo androidDevicesInfo;
Object obj;
String[] s;
JComboBox devicesComBox;
List list;
String devicesId;
public DevicesThread(JComboBox devicesComBox) {
// TODO Auto-generated constructor stub
this.devicesComBox=devicesComBox;
//System.out.println("aa");
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
// TODO Auto-generated method stub
devicesId=(String) devicesComBox.getSelectedItem();
System.out.println(devicesId);
androidDevicesInfo=new AndroidDevicesInfo();
list=androidDevicesInfo.getAndroidDevicesInfo();
devicesComBox.removeAllItems();
if(list.size()==0){
devicesComBox.addItem("无设备连接");
}
else{
for (String item : list) {
System.out.println(item);
devicesComBox.addItem(item);
}
}
}
}
5.加载包名线程类
package MonkeyTool;
import java.util.List;
import javax.swing.JComboBox;
//加载包名任务
public class PackageThread extends Thread {
AndroidDevicesInfo androidDevicesInfo;
AndroidPackageInfo androidPackageInfo;
Object obj;
String[] s;
JComboBox devicesComBox;
JComboBox packageComBox;
List list;
String devicesId;
public PackageThread(JComboBox devicesComBox,JComboBox packageComBox,String devicesId) {
// TODO Auto-generated constructor stub
this.devicesComBox=devicesComBox;
this.packageComBox=packageComBox;
this.devicesId=devicesId;
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
// TODO Auto-generated method stub
devicesId=(String) devicesComBox.getSelectedItem();
System.out.println(devicesId);
androidPackageInfo=new AndroidPackageInfo();
list=androidPackageInfo.getAndroidPackageInfo(devicesId);
packageComBox.removeAllItems();
if(list.size()==0){
packageComBox.addItem("未识别应用包名");
}
else{
for (String item : list) {
System.out.println(item);
packageComBox.addItem(item);
}
}
}
}
6.xml工具类
package MonkeyTool;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
//xml解析类
public class ReadMonkeyConfig {
Listcase_list;
Map map;
List>monkeyArgs;
//获取id名
public List XmlToList_id() throws Exception {
case_list=new ArrayList<>();
// File xmlFile = new File("src/Testcase.xml");
// File xmlFile = new File("E:/YallaProject/Testcase.xml");
File file=new File("");
System.out.println(file.getAbsolutePath());
File xmlFile=new File(file.getAbsolutePath()+"\\MonkeyConfig.xml");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element: "+doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("case");
for(int i = 0 ; i();
List
7.monkey配置加载线程
package MonkeyTool;
import java.util.List;
import javax.swing.JComboBox;
//展示monkey配置项线程
public class MonkeyConfigThread extends Thread {
String[] s;
JComboBox monkeyComBox;
ReadMonkeyConfig config;
List list;
String devicesId;
public MonkeyConfigThread(JComboBox monkeyComBox) {
this.monkeyComBox=monkeyComBox;
}
@Override
public void run() {
super.run();
config=new ReadMonkeyConfig();
try {
list=config.XmlToList_id();
monkeyComBox.removeAllItems();
if(list.size()==0){
monkeyComBox.addItem("配置文件中未配置");
}
else{
for (String item : list) {
System.out.println(item);
monkeyComBox.addItem(item);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
8.设备面板代码
package MonkeyTool;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class JpanelDevices extends JPanel{
AndroidDevicesInfo androidDevicesInfo;
AndroidPackageInfo androidPackageInfo;
Object obj;
String[] s;
JComboBox devicesComBox;
JComboBox packageComBox;
List list;
String devicesId;
String devicesId_s;
JButton jButton;
public String getDevice(){
//JComboBox devicesComBox=new JComboBox<>();
//this.devicesComBox=devicesComBox;
return (String) devicesComBox.getSelectedItem();
}
public JpanelDevices(){
setBounds(0,15, 720,30);
setLayout(null);
initView();
addView();
clickListenser();
getDevice();
}
//初始化控件
public void initView(){
jButton=new JButton("刷新设备");
jButton.setBounds(10,0,100,25);
devicesComBox=new JComboBox();
packageComBox=new JComboBox();
devicesComBox.setBounds(170, 0,192, 25);
packageComBox.setBounds(438, 0,265, 25);
}
public void addView(){
add(jButton);
add(devicesComBox);
add(packageComBox);
}
private void clickListenser(){
jButton.addActionListener(new ActionListener() {
//点击刷新按钮,刷新设备Id
@Override
public void actionPerformed(ActionEvent e) {
new DevicesThread(devicesComBox).start();
}
});
devicesComBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new PackageThread(devicesComBox,packageComBox,devicesId).start();
}
});
}
}
9.monkey面板代码
package MonkeyTool;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class JpanelMonkey extends JPanel{
ReadMonkeyConfig config=new ReadMonkeyConfig();
JpanelMonkey jpanelMonkey;
JLabel touchLable,motionLable,appSwitchLable,throttle,seedLable,timesLable,IgnoreCrashLable,IgnoreTimeoutLable,IgnoreSecurityExceptionLable,KillProcessAfterErrorLable,MonitorNativeCrashLable,pinchzoomLable,trackballLable,navLable,majornavLable,syskeysLable,flipLable,anyeventLable;
JTextField touchTextField,motionTextField,appswtichTextField,throttleTextField,seedTextField,timesTextField,pinchzoomTextField,trackballTextField,navTextField,majornavTextField,syskeysTextField,flipTextField,anyeventTextField;
JButton button,button2,monkeyConfigLable,startConfigWrite;
JCheckBox jCheckBox;
JCheckBox jCheckBox2;
JCheckBox jCheckBox3;
JCheckBox jCheckBox4;
JCheckBox jCheckBox5;
JComboBox monkeyComboBox;
String devicesId_s;
StringBuilder builder,builderEventPercent;
String clickPercent,swipePercent,swichPercent,waitTime,seed,times,pinchzoom,trackball,nav,majornav,syskeys,flip,anyevent;
//CmdUtils cmdUtils=new CmdUtils();
public JpanelDevices jpanelDevices;
//构造方法穿参实时获取值
public JpanelMonkey(JpanelDevices jpanelDevices){
setBorder(BorderFactory.createTitledBorder("分组框")); //设置面板边框,实现分组框的效果,此句代码为关键代码
setBorder(BorderFactory.createLineBorder(Color.blue));
setLayout(null);
setBounds(0,55,720,500);
initView();
addView();
this.jpanelDevices=jpanelDevices;
clickLinstner(jpanelDevices);
new MonkeyConfigThread(monkeyComboBox).start();
}
//以下为监听事件
private void clickLinstner(JpanelDevices jpanelDevices) {
//启动monkey
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new GoMonkey().start();
}
});
//停止monkey
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new StopMonkey().start();
}
});
//获取monkey配置项
monkeyConfigLable.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new MonkeyConfigThread(monkeyComboBox).start();
}
});
//monkey配置项写入
startConfigWrite.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new SetMonkeyConfig().start();
}
});
}
//写入monkey配置项线程
class SetMonkeyConfig extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
String id=(String) monkeyComboBox.getSelectedItem();
List>monkeyArgs=config.getConfigData(id);
touchTextField.setText(monkeyArgs.get(0).get("touchEvent"));
motionTextField.setText(monkeyArgs.get(0).get("motionEvent"));
appswtichTextField.setText(monkeyArgs.get(0).get("switchEvent"));
throttleTextField.setText(monkeyArgs.get(0).get("throttle"));
seedTextField.setText(monkeyArgs.get(0).get("seed"));
timesTextField.setText(monkeyArgs.get(0).get("times"));
pinchzoomTextField.setText(monkeyArgs.get(0).get("pinchzoomEvent"));
trackballTextField.setText(monkeyArgs.get(0).get("trackballEvent"));
navTextField.setText(monkeyArgs.get(0).get("navEvent"));
majornavTextField.setText(monkeyArgs.get(0).get("majornavEvent"));
syskeysTextField.setText(monkeyArgs.get(0).get("syskeysEvent"));
flipTextField.setText(monkeyArgs.get(0).get("flipEvent"));
anyeventTextField.setText(monkeyArgs.get(0).get("anyeventEvent"));
} catch (Exception e) {
// TODO Auto-generated catch block
JOptionPane.showConfirmDialog(null, "配置文件不存在","错误提示", JOptionPane.DEFAULT_OPTION);
}
}
}
class StopMonkey extends Thread{
CmdUtils cmdUtils=new CmdUtils();
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
if((String)jpanelDevices.devicesComBox.getSelectedItem()!=null){
devicesId_s=" -s "+(String)jpanelDevices.devicesComBox.getSelectedItem();
}
else{
devicesId_s=" ";
}
//获取monkey进程并kill
String commands="adb"+devicesId_s+" shell ps |grep monkey";
System.out.println(cmdUtils.excuteCmd(commands));
String log_pid_result=cmdUtils.excuteCmd(commands).get(0);
System.out.println(log_pid_result);
String g=log_pid_result.substring(7,15);
System.out.println(g);
System.out.println(Thread.currentThread().getName());
cmdUtils.excuteCmd("adb "+devicesId_s+" shell kill "+g);
System.out.println("ok");
}
}
//monkey启动线程
class GoMonkey extends Thread{
CmdUtils cmdUtils=new CmdUtils();
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
String devicesId=(String) jpanelDevices.devicesComBox.getSelectedItem();
String packageName=(String) jpanelDevices.packageComBox.getSelectedItem();
builder=new StringBuilder();
builderEventPercent=new StringBuilder();
if(jCheckBox.isSelected()){
builder.append(" --ignore-crashes");
}
else{
builder.append("");
}
if(jCheckBox2.isSelected()){
builder.append(" --ignore-timeouts");
}
else{
builder.append("");
}
if(jCheckBox3.isSelected()){
builder.append(" --ignore-security-exceptions");
}
else{
builder.append("");
}
if(jCheckBox4.isSelected()){
builder.append(" --kill-process-after-error");
}
else{
builder.append("");
}
if(jCheckBox5.isSelected()){
builder.append(" --monitor-native-crashes");
}
else{
builder.append("");
}
System.out.println(builder);
if(touchTextField.getText().equals("")==false){
clickPercent=" --pct-touch "+touchTextField.getText().toString();
builderEventPercent.append(clickPercent);
}
else{
clickPercent="";
}
if(motionTextField.getText().equals("")==false){
swipePercent=" --pct-motion "+motionTextField.getText().toString();
builderEventPercent.append(swipePercent);
}
else{
swipePercent="";
}
if(appswtichTextField.getText().equals("")==false){
swichPercent=" --pct-appswitch "+appswtichTextField.getText().toString();
builderEventPercent.append(swichPercent);
}
else{
swichPercent="";
}
if(throttleTextField.getText().equals("")==false){
waitTime=" --throttle "+throttleTextField.getText().toString();
}
else{
waitTime="";
}
if(seedTextField.getText().equals("")==false){
seed=" -s "+seedTextField.getText().toString();
}
else{
seed="";
}
if(timesTextField.getText().equals("")){
JOptionPane.showConfirmDialog(null, "必须输入运行次数","输入提示", JOptionPane.DEFAULT_OPTION);
}
else{
times=" "+timesTextField.getText().toString();
}
if(pinchzoomTextField.getText().equals("")){
pinchzoom="";
}
else{
pinchzoom=" --pct-pinchzoom "+pinchzoomTextField.getText().toString();
builderEventPercent.append(pinchzoom);
}
if(trackballTextField.getText().equals("")){
trackball="";
}
else{
trackball=" --pct-trackball "+trackballTextField.getText().toString();
builderEventPercent.append(trackball);
}
if(navTextField.getText().equals("")){
nav="";
}
else{
nav=" --pct-nav "+navTextField.getText().toString();
builderEventPercent.append(nav);
}
if(majornavTextField.getText().equals("")){
majornav="";
}
else{
majornav=" --pct-majornav "+majornavTextField.getText().toString();
builderEventPercent.append(majornav);
}
if(syskeysTextField.getText().equals("")){
syskeys="";
}
else{
syskeys=" --pct-syskeys "+syskeysTextField.getText().toString();
builderEventPercent.append(syskeys);
}
if(flipTextField.getText().equals("")){
flip="";
}
else{
flip=" --pct-flip "+flipTextField.getText().toString();
builderEventPercent.append(flip);
}
if(anyeventTextField.getText().equals("")){
anyevent="";
}
else{
anyevent=" --pct-anyevent "+anyeventTextField.getText().toString();
builderEventPercent.append(anyevent);
}
System.out.println(devicesId);
System.out.println(packageName);
//String monkeyCommand="adb -s "+devicesId+" shell monkey -p "+packageName+waitTime+clickPercent+swipePercent+swichPercent+builder.toString()+seedLable+timesLable;
//System.out.println(j.getSelectedItem().toString());
String monkeyCommand="adb -s "+devicesId+" shell monkey -p "+packageName+waitTime+builderEventPercent.toString()+builder.toString()+seed+times;
System.out.println(monkeyCommand);
System.out.println(cmdUtils.excuteCmd(monkeyCommand));
}
}
//初始化控件
private void initView(){
touchLable=new JLabel("Touch Event (%)");
touchLable.setBounds(10, 30,100, 25);
motionLable=new JLabel("Motion Event (%)");
motionLable.setBounds(10, 57,100, 25);
appSwitchLable=new JLabel("Appswitch (%)");
appSwitchLable.setBounds(10, 84,100, 25);
pinchzoomLable=new JLabel("pinchzoom (%)");
pinchzoomLable.setBounds(10, 111,100, 25);
trackballLable=new JLabel("trackball (%)");
trackballLable.setBounds(10,138,100, 25);
navLable=new JLabel("nav (%)");
navLable.setBounds(10,165,100, 25);
majornavLable=new JLabel("majornav (%)");
majornavLable.setBounds(10,192,100, 25);
syskeysLable=new JLabel("syskeys (%)");
syskeysLable.setBounds(10,219,100, 25);
flipLable=new JLabel("flip (%)");
flipLable.setBounds(10,246,100, 25);
anyeventLable=new JLabel("anevent (%)");
anyeventLable.setBounds(10,273,100, 25);
throttle=new JLabel("Throttle (S)");
throttle.setBounds(10, 300,100, 25);
seedLable=new JLabel("Seed");
seedLable.setBounds(10, 327,100, 25);
timesLable=new JLabel("Times");
timesLable.setBounds(10, 354,100, 25);
IgnoreCrashLable=new JLabel("Ignore Crash");
IgnoreCrashLable.setBounds(228, 30,120, 25);
IgnoreTimeoutLable=new JLabel("Ignore Timeout");
IgnoreTimeoutLable.setBounds(228, 57,150, 25);
IgnoreSecurityExceptionLable=new JLabel("Ignore Security Exception");
IgnoreSecurityExceptionLable.setBounds(228, 84,150, 25);
KillProcessAfterErrorLable=new JLabel("Kill Process After Error");
KillProcessAfterErrorLable.setBounds(228, 111,150, 25);
MonitorNativeCrashLable=new JLabel("Monitor Native Crash");
MonitorNativeCrashLable.setBounds(228, 138,150, 25);
touchTextField=new JTextField();
touchTextField.setBounds(113, 30,80, 25);
motionTextField=new JTextField();
motionTextField.setBounds(113, 57,80, 25);
appswtichTextField=new JTextField();
appswtichTextField.setBounds(113, 84,80, 25);
pinchzoomTextField=new JTextField();
pinchzoomTextField.setBounds(113, 111,80, 25);
trackballTextField=new JTextField();
trackballTextField.setBounds(113, 138,80, 25);
navTextField=new JTextField();
navTextField.setBounds(113, 165,80, 25);
majornavTextField=new JTextField();
majornavTextField.setBounds(113, 192,80, 25);
syskeysTextField=new JTextField();
syskeysTextField.setBounds(113, 219,80, 25);
flipTextField=new JTextField();
flipTextField.setBounds(113, 246,80, 25);
anyeventTextField=new JTextField();
anyeventTextField.setBounds(113, 273,80, 25);
throttleTextField=new JTextField();
throttleTextField.setBounds(113, 300,80, 25);
seedTextField=new JTextField();
seedTextField.setBounds(113, 327,80, 25);
timesTextField=new JTextField();
timesTextField.setBounds(113,354,80, 25);
jCheckBox=new JCheckBox("Ignore Crash");
jCheckBox.setBounds(202, 30,170, 25);
jCheckBox2=new JCheckBox("Ignore Timeout");
jCheckBox2.setBounds(202, 57,170, 25);
jCheckBox3=new JCheckBox("Ignore Security Exception");
jCheckBox3.setBounds(202, 84,170, 25);
jCheckBox4=new JCheckBox("Kill Process After Error");
jCheckBox4.setBounds(202, 111,170, 25);
jCheckBox5=new JCheckBox("Monitor Native Crash");
jCheckBox5.setBounds(202, 138,170, 25);
monkeyConfigLable=new JButton("Monkey一键配置选择");
monkeyConfigLable.setBounds(205,170,170, 25);
monkeyComboBox=new JComboBox<>();
monkeyComboBox.setBounds(375,170,150, 25);
startConfigWrite=new JButton("配置一键写入");
startConfigWrite.setBounds(527,170,150, 25);
button=new JButton("Excute Monkey");
button.setBounds(205, 250,150, 25);
button2=new JButton("Stop Monkey");
button2.setBounds(360,250,150, 25);
}
private void addView (){
add(touchLable);
add(motionLable);
add(appSwitchLable);
add(throttle);
add(seedLable);
add(timesLable);
add(pinchzoomLable);
add(trackballLable);
add(navLable);
add(majornavLable);
add(syskeysLable);
add(flipLable);
add(anyeventLable);
add(button);
add(button2);
add(touchTextField);
add(motionTextField);
add(appswtichTextField);
add(throttleTextField);
add(seedTextField);
add(timesTextField);
add(pinchzoomTextField);
add(trackballTextField);
add(navTextField);
add(majornavTextField);
add(syskeysTextField);
add(flipTextField);
add(anyeventTextField);
add(jCheckBox);
add(jCheckBox2);
add(jCheckBox3);
add(jCheckBox4);
add(jCheckBox5);
add(monkeyConfigLable);
add(monkeyComboBox);
add(startConfigWrite);
}
}
10.主类代码
package MonkeyTool;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class MonkeyMain extends JFrame {
public MonkeyMain(){
setTitle("MonkeyTool");
setLayout(null);
setBounds(0,0,720,500);
JpanelDevices jpanelDevices=new JpanelDevices();
JpanelMonkey jpanelMonkey=new JpanelMonkey(jpanelDevices);
Container container=getContentPane();
container.add(jpanelDevices);
container.add(jpanelMonkey);
setLayout(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String []args){
new MonkeyMain();
}
}