jmeter自定义Java Request里面写了一个简单的java Request
下面弄一个比较真实一点的例子,比如我有一个DBUtil的类,我想测试一下这个类或者类里面的方法性能怎么样,这时我们就可以写一个自定义的java Request来测试。
DBUtil类如下:
package com.matt.DBUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.*;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.*;
import org.apache.log4j.Logger;
public class DBUtil {
private static final Logger logger = Logger.getLogger(DBUtil.class);
static Connection conn = null;
// 打开数据库连接
public static Connection openConnection(String host, String port,
String name, String username, String password) {
try {
String driver;
String url;
driver = "com.mysql.jdbc.Driver";
url = "jdbc:mysql://" + host + ":" + port + "/" + name;
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
return conn;
}
// 关闭数据库连接
public static void closeConnection() {
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
}
// 查询(返回 Array)
public static Object[] queryArray(QueryRunner runner, String sql,
Object... params) {
Object[] result = null;
try {
result = runner.query(conn, sql, new ArrayHandler(), params);
} catch (SQLException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
printSQL(sql);
return result;
}
// 查询(返回 ArrayList)
public static List<Object[]> queryArrayList(QueryRunner runner, String sql,
Object... params) {
List<Object[]> result = null;
try {
result = runner.query(conn, sql, new ArrayListHandler(), params);
} catch (SQLException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
printSQL(sql);
return result;
}
// 查询(返回 Map)
public static Map<String, Object> queryMap(QueryRunner runner, String sql,
Object... params) {
Map<String, Object> result = null;
try {
result = runner.query(conn, sql, new MapHandler(), params);
} catch (SQLException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
printSQL(sql);
return result;
}
// 查询(返回 MapList)
public static List<Map<String, Object>> queryMapList(QueryRunner runner,
String sql, Object... params) {
List<Map<String, Object>> result = null;
try {
result = runner.query(conn, sql, new MapListHandler(), params);
} catch (SQLException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
printSQL(sql);
return result;
}
// 查询(返回 Bean)
public static <T> T queryBean(QueryRunner runner, Class<T> cls,
Map<String, String> map, String sql, Object... params) {
T result = null;
try {
if (map.isEmpty()) {
result = runner.query(conn, sql, new BeanHandler<T>(cls,
new BasicRowProcessor(new BeanProcessor(map))), params);
} else {
result = runner.query(conn, sql, new BeanHandler<T>(cls),
params);
}
printSQL(sql);
} catch (SQLException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
return result;
}
// 查询(返回 BeanList)
public static <T> List<T> queryBeanList(QueryRunner runner, Class<T> cls,
Map<String, String> map, String sql, Object... params) {
List<T> result = null;
try {
if (map.isEmpty()) {
result = runner.query(conn, sql, new BeanListHandler<T>(cls,
new BasicRowProcessor(new BeanProcessor(map))), params);
} else {
result = runner.query(conn, sql, new BeanListHandler<T>(cls),
params);
}
printSQL(sql);
} catch (SQLException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
return result;
}
// 查询指定列名的值(单条数据)
public static Object queryColumn(QueryRunner runner, String column,
String sql, Object... params) {
Object result = null;
try {
result = runner.query(conn, sql, new ScalarHandler<Object>(column),
params);
} catch (SQLException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
printSQL(sql);
return result;
}
// 查询指定列名的值(多条数据)
public static <T> List<T> queryColumnList(QueryRunner runner,
String column, String sql, Object... params) {
List<T> result = null;
try {
result = runner.query(conn, sql, new ColumnListHandler<T>(column),
params);
} catch (SQLException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
printSQL(sql);
return result;
}
// 查询指定列名对应的记录映射
public static <T> Map<T, Map<String, Object>> queryKeyMap(
QueryRunner runner, String column, String sql, Object... params) {
Map<T, Map<String, Object>> result = null;
try {
result = runner.query(conn, sql, new KeyedHandler<T>(column),
params);
} catch (SQLException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
printSQL(sql);
return result;
}
// 更新(包括 UPDATE、INSERT、DELETE,返回受影响的行数)
public static int update(QueryRunner runner, Connection conn, String sql,
Object... params) {
int result = 0;
try {
if (conn != null) {
result = runner.update(conn, sql, params);
} else {
result = runner.update(sql, params);
}
printSQL(sql);
} catch (SQLException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
return result;
}
private static void printSQL(String sql) {
if (logger.isDebugEnabled()) {
logger.debug("SQL: " + sql);
}
}
}
注: 这个类要引入commons-dbutils-1.6.jar 、log4j-1.2.16.jar、 mysql-connector-java-5.1.22-bin.jar
然后我想用jmeter测试一下这个类里面的一些方法的性能,下面以queryArrayList这个方法为例。
和jmeter自定义Java Request里面一样要写一个自定义的Java Request, 代码如下:
package com.matt.jmeter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import com.matt.DBUtil.DBUtil;
public class TestDBUtilsSampler extends AbstractJavaSamplerClient {
private String host = null;
private String port = null;
private String name = null;
private String username = null;
private String password = null;
private String sql = null;
private SampleResult sr;
public Arguments getDefaultParameters() {
Arguments args = new Arguments();
args.addArgument("Host", "localhost");
args.addArgument("Port", "3306");
args.addArgument("Name", "dbName");
args.addArgument("Username", "yourUsername");
args.addArgument("Password", "yourPwd");
args.addArgument("SqlString", "SqlString");
return args;
}
public void setupTest(JavaSamplerContext arg0) {
host = arg0.getParameter("Host");
port = arg0.getParameter("Port");
name = arg0.getParameter("Name");
username = arg0.getParameter("Username");
password = arg0.getParameter("Password");
sql = arg0.getParameter("SqlString");
sr = new SampleResult();
sr.setSampleLabel("Test DBUtil Sampler");
if (sql != null && sql.length() > 0) {
sr.setSamplerData(sql);
}
}
public SampleResult runTest(JavaSamplerContext arg0) {
sr.sampleStart();
try {
DBUtil.openConnection(host, port, name, username, password);
QueryRunner runner = new QueryRunner();
List<Object[]> list = DBUtil.queryArrayList(runner, sql);
String str1 = "";
for(Object[] o : list){
for(Object ob : o){
//System.out.println(ob);
str1 = str1 + ob + "|";
}
str1 = str1 + ",";
}
sr.setResponseMessage("Get the result size:\t" + list.size());
sr.setResponseData(str1, "UTF-8");
sr.setDataType(SampleResult.TEXT);
sr.setSuccessful(true);
sr.setResponseCodeOK();
} catch (Throwable e) {
sr.setResponseMessage("Exception is " + e);
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
sr.setResponseData(stringWriter.toString(),"UTF-8");
sr.setResponseCode("500");
sr.setSuccessful(false);
} finally {
DBUtil.closeConnection();
sr.sampleEnd();
}
return sr;
}
public void teardownTest(JavaSamplerContext arg0) {
}
}
打成jar包, 放到jmeter的/lib/ext目录下。
上面说的另外三个依赖包,放到/lib目录下
重启Jmeter
在Thread Group上是右击,Add-->Sampler-->Java Request。
在下拉框选择com.matt.jmeter.TestDBUtilsSampler
在输入对应的数据库配制,
最后加上Results Tree 和 Aggregate Report, 运行。