您的位置:软件测试 > 开源软件测试 > 开源性能测试工具 > Jmeter
Jmeter导入DB数据再再优化
作者:niuzhigang 发布时间:[ 2017/5/4 14:49:13 ] 推荐标签:性能测试工具 Jmeter

 

  支持和兼容:
  第一:支持同个jmx文件多个接口用例场景
  第二:兼容同个jmx文件虽同一个接口不同顺序的输入
  一个要求:
  jmeter命名规则为终端AAA版本BBB类型CCC校验DDD接口EEE
  AAA为终端类型(如app、网站) ,BBB为迭代号(如9.0.1) ,CCC为接口类型(如搜索、跟团) ,DDD为接口名称(如默认出发城市),EEE为用例名称(如检验推荐城市否正确)
  由于获取SQL每个字段均为List,因此若jmeter想使用,必须进行二次封装!!!
//封装上面获取终端类型、版本号、接口类型、接口名称、方法提供jmeter使用
public static ArrayList<String> getTerminalTypeArray(String path) throws Exception{
ArrayList<String> terminalTypeArray = readCsv.getTerminalType(path);
return terminalTypeArray;
}
public static ArrayList<String> getVersionArray(String path) throws Exception{
ArrayList<String> versionTypeArray = readCsv.getVersion(path);
return versionTypeArray;
}
public static ArrayList<String> getInterfaceTypeArray(String path) throws Exception{
ArrayList<String> interfaceTypeArray = readCsv.getInterfaceType(path);
return interfaceTypeArray;
}
public static ArrayList<String> getInterfaceNameArray(String path) throws Exception{
ArrayList<String> interfaceNameArray = readCsv.getInterfaceName(path);
return interfaceNameArray;
}
public static ArrayList<String> getCaseNameArray(String path) throws Exception{
ArrayList<String> caseNameArray = readCsv.getCaseName(path);
return caseNameArray;
}
public static ArrayList<String> getCaseIsPassArray(String path) throws Exception{
ArrayList<String> caseIsPassArray = readCsv.getcaseIsPass(path);
return caseIsPassArray;
}
  好了获取到了,我们做插入DB操作:
  详细表跟之前改变不大!!(索引扩充一个createTime字段、时间有精确到秒改为精确到日)
  如:
public static String currTime(){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
String now = df.format(new Date());// new Date()为获取当前系统时间
return now;
}
//插入详细数据
public static boolean insertDetailDB(String terminalType,String excVersion,String interfaceType,String interfaceName,String caseName,String excResult){
try {
Class.forName("com.mysql.jdbc.Driver");
String databaseName = "test";// 已经在MySQL数据库中创建好的数据库。
String userName = "mobtest";// MySQL默认的root账户名
String password = "tuniu520";// 默认的root账户密码为空
String connUrl = "jdbc:mysql://10.10.30.200:3306/";//连接地址
Connection conn = DriverManager.getConnection(connUrl + databaseName, userName, password);
PreparedStatement st = null;
Statement stmt = conn.createStatement();
String createTime = readCsv.currTime();
String sql = "create table if NOT EXISTS AutoTest_DetailInterface(id int NOT NULL auto_increment primary key ,terminalType varchar(50) NOT  NULL DEFAULT 'App' ,"
+ "excVersion varchar(50),interfaceType varchar(50) ,interfaceName varchar(50),"
+ "caseName varchar(50) ,excResult varchar(50),creatTime varchar(50) NOT NULL DEFAULT '"+createTime+"',"
+ " UNIQUE INDEX  ( terminalType,excVersion,interfaceType,interfaceName,caseName,creatTime ) )";
//                System.out.println(sql);
// 创建数据库中的表,
int result = stmt.executeUpdate(sql);
if (result != -1) {
sql = "insert into AutoTest_DetailInterface(terminalType,excVersion,interfaceType,interfaceName,caseName,excResult) values(?,?,?,?,?,?) "
+ "ON DUPLICATE KEY UPDATE excResult=?";
//                    sql = "insert into AutoTest_DetailInterface (permaryTitle,secondaryTitle,excVersion,excTerminal,excResult)
//                    +values(primaryTitle,secordaryTitle,excVersion,excTerminal,excResult)";
st = conn.prepareStatement(sql);
//存入终端类型转为小写
st.setString(1, terminalType.toLowerCase());
st.setString(2, excVersion);
st.setString(3, interfaceType);
st.setString(4, interfaceName);
st.setString(5, caseName);
//存入执行结果true或者false转为小写
st.setString(6, excResult.toLowerCase());
st.setString(7, excResult.toLowerCase());
st.executeUpdate();
sql = "SELECT * FROM AutoTest_DetailInterface";
System.out.println(stmt.executeQuery(sql));
ResultSet rs = stmt.executeQuery(sql);
System.out.println("id terminalType excVersion interfaceType interfaceName caseName excResult creatTime");
while (rs.next()) {
System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4) + " " + rs.getString(5) + " " + rs.getString(6) + " " + rs.getString(7)+ rs.getString(8));
}
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
  插入统计表:(相比之前思路都改变了~是通过详细表select计算统计后insert进去的而不是通过java依次获取每个字段值set进去的)
  具体如下:
  有一点说明:是SQL在insert的时候若是insert整个sql的时候且当联合索引键相同时,做更新不能用 key=vaule这种平常的用法(不然拿到第一行的数据更新了所有行的数据),而是 set key=VALUES(key)
  具体用法可以看下http://www.jb51.net/article/39255.htm 这个用法,备注,因为这点纠结了我大半天时间!!!
//插入统计数据
public static boolean insertTotalDB(){
try {
Class.forName("com.mysql.jdbc.Driver");
String databaseName = "test";// 已经在MySQL数据库中创建好的数据库。
String userName = "mobtest";// MySQL默认的root账户名
String password = "tuniu520";// 默认的root账户密码为空
String connUrl = "jdbc:mysql://10.10.30.200:3306/";//连接地址
Connection conn = DriverManager.getConnection(connUrl + databaseName, userName, password);
PreparedStatement st = null;
Statement stmt = conn.createStatement();
String createTime = readCsv.currTime();
String sql = "create table if NOT EXISTS AutoTest_TotalInterface(id int NOT NULL auto_increment primary key ,terminalType varchar(50) NOT  NULL DEFAULT 'App' ,"
+ "excVersion varchar(50),interfaceType varchar(50) ,interfaceName varchar(50),"
+ "caseTotalNum int,caseSucNum int,excRate varchar(50) ,creatTime varchar(50) NOT NULL DEFAULT '"+createTime+"',"
+ " UNIQUE INDEX  ( terminalType,excVersion,interfaceType,interfaceName,creatTime ) )";
// 创建数据库中的表,
int result = stmt.executeUpdate(sql);
if (result != -1) {
String selectSQL = "SELECT  terminalType,excVersion,interfaceType,"
+ "interfaceName,caseTotalNum,caseSucNum,"
+ "ROUND(caseSucNum/caseTotalNum,3) "
+ "as excRate,creatTime from("
+ "SELECT terminalType,excVersion,interfaceType,interfaceName,count(1) "
+ "as caseTotalNum,sum(ex) as caseSucNum,creatTime from("
+ "SELECT terminalType,excVersion,interfaceType,interfaceName,"
+ "creatTime,case excResult "
+ "when 'true' then 1 when 'false' then 0 end as ex from "
+ "AutoTest_DetailInterface)m group by "
+ "terminalType,excVersion,interfaceType,interfaceName,creatTime)n";
sql = "insert into AutoTest_TotalInterface("
+ "terminalType,excVersion,interfaceType,interfaceName,caseTotalNum,caseSucNum,"
+ "excRate,creatTime) "+selectSQL+" ON DUPLICATE KEY UPDATE caseTotalNum=VALUES(caseTotalNum),caseSucNum=VALUES(caseSucNum),excRate=VALUES(excRate)";
System.out.println(sql);
//执行插入操作
st = conn.prepareStatement(sql);
st.executeUpdate();
sql = "SELECT * FROM AutoTest_TotalInterface";
System.out.println(stmt.executeQuery(sql));
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4) + " " + rs.getString(5) + " " + rs.getString(6) + " " + rs.getString(7)+ " " + rs.getString(8) + rs.getString(9) );
}
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
  好了,到此结束,当然,importDB的jmx文件也要发生变化(直接调用方法即可),具体如下:
import readDB.*;
import excFile.*;
String path = "D:\excResult.csv";
int length = readCsv.getTerminalTypeArray(path).size();
log.info("用例数量为:"+length);
//循环获取各个参数
for(int i=0;i<length;i++){
String terminalTypeKey = readCsv.getTerminalTypeArray(path).get(i);
log.info("获取终端类型:"+terminalTypeKey);
String excVersionKey = readCsv.getVersionArray(path).get(i);
log.info("获取版本号:"+excVersionKey);
String interfaceTypeKey = readCsv.getInterfaceTypeArray(path).get(i);
log.info("获取接口类型:"+interfaceTypeKey);
String interfaceNameKey = readCsv.getInterfaceNameArray(path).get(i);
log.info("获取接口名称:"+interfaceNameKey);
String caseNameKey = readCsv.getCaseNameArray(path).get(i);
log.info("获取用例名称:"+caseNameKey);
String excResultKey = readCsv.getCaseIsPassArray(path).get(i);
log.info("获取执行结果:"+excResultKey);
readCsv.insertDetailDB(terminalTypeKey, excVersionKey, interfaceTypeKey, interfaceNameKey, caseNameKey, excResultKey);
}
//插入统计表
readCsv.insertTotalDB();
String dir = "D:\";
String oldname = "excResult.csv";
log.info("获取新文件名称:"+renFile.currTime());
String newname = "excResult"+renFile.currTime()+".csv";
log.info("获取新文件名称:"+newname);
renFile.renameFile(dir, oldname, newname);
  导入DB数据如下:
  原始数据:

  导入详细表的数据:

  导入统计表数据:

上一页12下一页
软件测试工具 | 联系我们 | 投诉建议 | 诚聘英才 | 申请使用列表 | 网站地图
沪ICP备07036474 2003-2017 版权所有 上海泽众软件科技有限公司 Shanghai ZeZhong Software Co.,Ltd