JMeter+Beanshell高级数据库断言

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011466469/article/details/88867111

背景:当我们使用JMeter测试工具的普通断言不能满足需求时,就需要考虑从数据库开始断言,但是添加的beanshell Assertion写入的代码量有点可观,纵使可读性行,但是不方便管理,而且是多个接口需要断言都需要重新添加,虽然没有代码量但内容还是太多。所以建议把常用的方法自己开发封装好,然后再导出到%JMETER_HOME%/lib/ext目录下,启动导入包即可试用。

1、先来看看beanshell Assertion的代码如何之长,光import导入的包就有好几个:

import java.sql.*;  
import java.util.*;  
import java.lang.*;  
import org.apache.regexp.*;  
import org.json.JSONArray;  
import org.json.JSONException;  
import org.json.JSONObject;  
import com.google.gson.JsonObject;    
import com.google.gson.JsonParser;   

		// 数据库连接字段
		String drive = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://${mysqlIpPort}/";
		String dbName = "your_database";
		String user = "your_user";
		String pass = "your_passwd";

		Connection Mycon = null;
		Statement Mystmt = null;
		ResultSet Myrset = null;

		String user_id = vars.get("userId");

		String query = "select * from databases.table where user_id='"
				+ user_id + "' and id='${productId}'";

		Set historySet = new HashSet();
		try {
			Mycon = DriverManager.getConnection(url + dbName + "?useSSL=false",
					user, pass);
		} catch (SQLException e) {
			System.out.println(e);
		}
		Mystmt = Mycon.createStatement();
		Myrset = Mystmt.executeQuery(query);

		while (Myrset.next()) {
			String history = Myrset.getString(1);
			historySet.add(history);
		}
		Myrset.close();
		Mystmt.close();

        // 获取响应数据信息,根据返回格式来确定试用哪种方法接收,取值,怎么序列化。
		String strData = prev.getResponseDataAsString();
		JSONObject Response = new JSONObject(strData);
		String Response1 = Response.get("data").get("result").get("fid")
				.toString();

		String response = strData.toString();

        // 前面都是数据的获取,后面才是断言部分
		if (history == "") {
			Failure = true;
			FailureMessage = "数据库没有记录,${msg}";
		} else if (!historySet.contains(Response1) == true) {
			Failure = true;
			FailureMessage = "响应不包含历史记录Id";
		} else {
			FailureMessage = "匹配成功!";
		}

2、看着太长,每次修改都挺费劲,不如在eclipse调试好,再导出使用,且看数据库连接及查询sql部分的封装: 

public class MySQLConnOpares {
	/**
	 * 在JMeter测试工具方面使用,当其它断言元件不满足时,需要写beanshell高级断言
	 * 其中需要对mysql进行数据库连接、sql执行、结果收集,然后再进行断言
	 * 原来是把所有代码写在beanshell,篇幅太长,所以想导成jar供其使用
	 * 
	 * 为了脚本的可读性,在if条件判断处,就不封装了,同是简单的equals和contains; 最后还是需要JMeter控制断言输出:
	 * Failture和FailtureMessage
	 */

	public Connection getConn(String url, String user, String passwd) {

		Connection conn = null;
		try {
			conn = DriverManager.getConnection(url, user, passwd);
			// System.out.println("mysql连接成功!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("连接mysql异常:" + e.getMessage());
		}

		return conn;
	}

	public String queryOneResult(ResultSet myrSat) {
		String historyResult = null;
		try {
			while (myrSat.next()) {
				historyResult = myrSat.getString(1);
			}
			myrSat.close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return historyResult;
	}

	public Set queryMoreResult(ResultSet myrSat) {
		String queryResult = null;
		Set historySet = new HashSet();
		try {
			while (myrSat.next()) {
				queryResult = myrSat.getString(1);
				historySet.add(queryResult);
			}
			myrSat.close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return historySet;
	}

	public boolean equals(String historyRusult, String responseResult) {
		// 字符串不能进行==比较运算
		return historyRusult.equals(responseResult) ? true : false;
	}

	public boolean contains(Set historyRusult, String responseResult) {
		// 字符串contains包含判断
		return historyRusult.contains(responseResult) ? true : false;
	}

}

3、是吧,封装的代码就很多了,那么再测试一下:

public class AppTest {

	public static void main(String[] args) {

		MySQLConnOpares conn = new MySQLConnOpares();

		try {
			Statement myStat = conn.getConn(
					"jdbc:mysql://你的数据库地址及端口,",
					"root", "root").createStatement();
			String history=conn.queryOneResult(myStat.executeQuery("select *from databases.table limit 1"));
			
			Set historySet=conn.queryMoreResult(myStat.executeQuery("select *from databases.table limit 5"));
			
			System.out.println("查询的结果:"+history);
			System.out.println("查询的结果:"+historySet);
			
			String response="16";
			System.out.println(conn.equals(history, response));
			System.out.println(conn.contains(historySet, history));
			myStat.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
	}
}

4、测试结果:至于这个ssl警告,在数据库连接后面加上参数?userSSL=false就可以了。

5、那么在JMeter中怎么使用呢?先从eclipse导出jar,放在lib/ext目录,在重启jmeter就好了。导入导出步骤略;

6、 重启jmeter,新建测试计划,脚本结构如下,重点不在断言,所以没使用beanshell Assertion:

7、Jmeter-beanshell sampler测试代码如下:

import com.jmeter.www.TestTools.*;
import java.sql.Statement;
import java.util.Set;

	MySQLConnOpares conn = new MySQLConnOpares();

	String query1="select *from wg_tongs.tb_user_order limit 1";
	String query2="select *from wg_tongs.tb_user_order limit 5";

	Statement myStat = conn.getConn("${url}","${user}","${passwd}").createStatement();
			
	String history=conn.queryOneResult(myStat.executeQuery(query1));
			
	Set historySet=conn.queryMoreResult(myStat.executeQuery(query2));
		
	System.out.println("查询的结果:"+history);
	System.out.println("查询的结果:"+historySet);
	
	String response="16";
	System.out.println(conn.equals(history, response));
	System.out.println(conn.contains(historySet, history));
	myStat.close();

 8、测试结果输出如下,因为是system打印输出,在控制台,在jmeter现在日志看就要使用log的方法;

 9、这样是不是比之前的长篇大论要简单明了,那么剩下的工作就是对响应报文的分析,使用什么方法提取数据,然后再使用beanshell Assertion进行if条件判断断言输出即可。

猜你喜欢

转载自blog.csdn.net/u011466469/article/details/88867111