JMeter笔记:如何编写Java请求
本篇编写java脚本来请求数据库
准备工作
- idea安装和配置
- mysql安装和配置
- 准备好依赖的jar包(链接:https://pan.baidu.com/s/1xbY8-Wy161rEP9b8c5A8NA ,提取码:e7pp )
脚本编写
1、实现JavaSamplerClient接口
2、四个方法:
getDefaultParameters: 获取参数,设置的参数会在Jmeter的参数面板上显示出来
setupTest: 初始化方法,只执行一次,用于建立连接
runTest: 执行N次,处理业务
teardownTest: 结束方法,只执行一次,用于释放资源
3、导出JMeter的java脚本
1> 导出之前先把脚本运行一遍,使用main方法,空的也行
2> file-export-runnable java file - launch configuration 那里要选择你的类名
3> Library handing那里选择第三项copy xxxx
4> 将导出的xxx.jar 和xxx_lib文件夹放到JMeter的lib/ext目录下
5> 重新启动JMeter
Java请求编写
1、新建项目
2、新建lib目录,将依赖jar包复制到lib下
3、解压缩jar包,解压缩jar包步骤如下:
1> File-->Project Structure
2> 在弹出来的Project Structure左侧选择Libraries,点击+号,选择Java
3> 在弹出来的Select Librariy Files中选择当前项目的lib目录
4> 如下图点击OK
5> 如下图点击OK
6> 解压完成如下:
4、在src下编写代码
结构如下:
MyQueryTest内容如下:
package com.forest.jmeterSampler; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient; import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; import org.apache.jmeter.samplers.SampleResult; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; public class MyQueryTest implements JavaSamplerClient { Connection conn; PreparedStatement ps; String sql = "select user_name,password from user where id>?;"; ResultSet set; ArrayList<String> results = new ArrayList<>(); @Override public void setupTest(JavaSamplerContext javaSamplerContext) { String url = javaSamplerContext.getParameter("url"); String username = javaSamplerContext.getParameter("username"); String password = javaSamplerContext.getParameter("password"); try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url,username,password); ps = conn.prepareStatement(sql); System.out.println("setup"); } catch (Exception e) { e.printStackTrace(); } } @Override public SampleResult runTest(JavaSamplerContext javaSamplerContext) { SampleResult sampleResult = new SampleResult(); sampleResult.setSampleLabel("query-mysql-forest"); sampleResult.sampleStart(); try{ int id = javaSamplerContext.getIntParameter("id"); ps.setInt(1,id); set = ps.executeQuery(); while (set.next()){ String result = set.getString(1)+":"+set.getString(2); results.add(result); } if(results.size()>0){ sampleResult.setSuccessful(true); sampleResult.setResponseCode("200"); System.out.println(results); sampleResult.setResponseData(results.toString(),"utf-8"); sampleResult.setResponseMessage("query success!"); }else{ sampleResult.setSuccessful(false); sampleResult.setResponseCode("500"); sampleResult.setResponseMessage("query fail,sql is : "+sql); } }catch (Exception e) { sampleResult.setSuccessful(false); sampleResult.setResponseCode("500"); sampleResult.setResponseMessage(e.getMessage()); } sampleResult.sampleEnd(); return sampleResult; } @Override public void teardownTest(JavaSamplerContext javaSamplerContext) { try { ps.close(); //关闭对象 conn.close(); //关闭对象 }catch (Exception e){ e.printStackTrace(); } } @Override public Arguments getDefaultParameters() { Arguments arguments = new Arguments(); arguments.addArgument("url","jdbc:mysql://192.168.0.110:3306/forest"); arguments.addArgument("username","admin"); arguments.addArgument("password","admin123456"); arguments.addArgument("id","1"); return arguments; } public static void main(String[] args){ MyQueryTest myQueryTest = new MyQueryTest(); Arguments arguments = myQueryTest.getDefaultParameters(); JavaSamplerContext context = new JavaSamplerContext(arguments); myQueryTest.setupTest(context); myQueryTest.runTest(context); myQueryTest.teardownTest(context); } }
MysqlInsert内容如下:
package com.forest.jmeterSampler; import com.sun.xml.internal.bind.v2.util.QNameMap; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient; import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; import org.apache.jmeter.samplers.SampleResult; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class MysqlInsert implements JavaSamplerClient { Connection conn; //由setup中提到类下面 PreparedStatement ps; String sql = "insert into user(user_name,password) values(?,?);"; //预编译SQL语句,需要有对象去存它 @Override public void setupTest(JavaSamplerContext javaSamplerContext) { String url = javaSamplerContext.getParameter("url"); String username = javaSamplerContext.getParameter("username"); String password = javaSamplerContext.getParameter("password"); try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url,username,password); ps = conn.prepareStatement(sql); } catch (Exception e) { e.printStackTrace(); } } @Override public SampleResult runTest(JavaSamplerContext javaSamplerContext) { SampleResult sampleResult = new SampleResult(); sampleResult.setSampleLabel("insert-mysql-forest"); sampleResult.sampleStart(); //开始发请求 int row = 0; try{ ps.setString(1,javaSamplerContext.getParameter("username3")); ps.setString(2,javaSamplerContext.getParameter("password3")); row = ps.executeUpdate(); if(row==1){ sampleResult.setSuccessful(true); sampleResult.setResponseCode("200"); sampleResult.setResponseData("insert行数"+row,"utf-8"); sampleResult.setResponseMessage("insert success!"); }else{ sampleResult.setSuccessful(false); sampleResult.setResponseCode("500"); sampleResult.setResponseMessage("insert fail"); } } catch (Exception e) { sampleResult.setSuccessful(false); sampleResult.setResponseCode("500"); sampleResult.setResponseMessage(e.getMessage()); } sampleResult.sampleEnd(); return sampleResult; } @Override public void teardownTest(JavaSamplerContext javaSamplerContext) { try { ps.close(); //关闭对象 conn.close(); //关闭对象 }catch (Exception e){ e.printStackTrace(); } } @Override public Arguments getDefaultParameters() { Arguments arguments = new Arguments(); arguments.addArgument("url","jdbc:mysql://192.168.0.110:3306/forest"); arguments.addArgument("username","admin"); arguments.addArgument("password","admin123456"); arguments.addArgument("username3",""); arguments.addArgument("password3",""); return arguments; } public static void main(String[] args){ MysqlInsert mysqlInsert = new MysqlInsert(); JavaSamplerContext context = new JavaSamplerContext(mysqlInsert.getDefaultParameters()); String url = context.getParameter("url"); String username =context.getParameter("username"); String password = context.getParameter("password"); Arguments arguments = new Arguments(); arguments.addArgument("url",url); arguments.addArgument("username",username); arguments.addArgument("password",password); arguments.addArgument("username3","lintest20"); arguments.addArgument("password3","123456"); JavaSamplerContext context2 = new JavaSamplerContext(arguments); mysqlInsert.setupTest(context2); mysqlInsert.runTest(context2); mysqlInsert.teardownTest(context2); } }
5、源码打成jar包
1> build project
2> File -> Project Structure
3> Project Structure弹窗选择Artifacts,选择+号,选择JAR,选择Empty,如下图:
4> 填写名称,双击图中箭头位置
5> 上一步双击完如下图所示,点击OK
6> 如下图,out目录已经生成,点击Build -> Build Artifacts
7> 第6步点击之后如下图,点击Build
8> 查看out下,打包成功
使用java请求
1> 添加java请求
2> 在java请求中选择类名称,填写参数如下:
3> 运行脚本,运行成功如下图: