51Testing软件测试论坛
标题:
JmeterJavaSampler的编写
[打印本页]
作者:
apollo0687
时间:
2017-11-23 11:37
标题:
JmeterJavaSampler的编写
本帖最后由 apollo0687 于 2017-12-5 14:07 编辑
本Demo是在eclipse中用maven工程编写,主要描述java插件的编写和使用。
作者:
梦想家
时间:
2017-11-23 14:26
支持分享
作者:
apollo0687
时间:
2017-11-23 14:36
受帖子发布篇幅的限制,就只有发链接了
作者:
apollo0687
时间:
2017-11-23 14:37
亲测可用
作者:
lsekfe
时间:
2017-11-24 09:50
不错支持下!建议如果比较长的内容的话,可以分楼层来发。
作者:
apollo0687
时间:
2017-11-24 13:34
1.在新建工程中增加依赖
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>3.3</version>
</dependency>
2.编写Demo
2.1新建 JmeterSample类,继承自AbstractJavaSamplerClient
public class JmeterSample extends AbstractJavaSamplerClient
2.2定义两个变量,传入Jmeter的变量
private String username;
private String password;
2.3执行主体前后的上下文关联
@Override
public void setupTest(JavaSamplerContext context) {
super.setupTest(context);
}
@Override
public void teardownTest(JavaSamplerContext context) {
super.teardownTest(context);
}
2.4准备从Jmeter中获取自定义的参数
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
params.addArgument("username", "apollo");
params.addArgument("password", "abcd1234");
return params;
}
2.5执行主体从Jmeter中获取参数
public SampleResult runTest(JavaSamplerContext arg0) {
SampleResult sr = new SampleResult();
sr.sampleStart();
username = arg0.getParameter("username");
password = arg0.getParameter("password");
2.6执行主体中工作方法HttpPost的实现
requset_str = "username:" + username + "password:" + password;
Http javaHttp = new Http();
res = javaHttp.Post(requset_str, 1);
2.7对HttpPost的返回值处理,显示在jmeter中
String arr[] = res.split("|");
if(arr[0].equals("200")) {
sr.setSamplerData(requset_str);
sr.setResponseData(arr[1], "utf-8");
sr.setSuccessful(true);
sr.setResponseCode("200");
sr.setResponseCodeOK();
sr.sampleEnd();
}else {
sr.setSamplerData(requset_str);
sr.setResponseData(arr[1], "utf-8");
sr.setSuccessful(false);
sr.sampleEnd();
}
作者:
apollo0687
时间:
2017-11-24 18:56
3.把代码导出为JAR file
[attach]109456[/attach]4.把jar包放在apache-jmeter-3.2\lib\ext中
5.在Jmeter3.2中运行jmeter.bat
新增java请求
[attach]109457[/attach]
6.下面上代码:
package com.perf.jmeter;
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;
public class JmeterSample extends AbstractJavaSamplerClient {
private String username;
private String password;
String requset_str ="";
String res ="";
@Override
public void setupTest(JavaSamplerContext context) {
super.setupTest(context);
}
@Override
public void teardownTest(JavaSamplerContext context) {
super.teardownTest(context);
}
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
params.addArgument("username", "apollo");
params.addArgument("password", "abcd1234");
return params;
}
public SampleResult runTest(JavaSamplerContext arg0) {
SampleResult sr = new SampleResult();
sr.sampleStart();
username = arg0.getParameter("username");
password = arg0.getParameter("password");
try {
requset_str = "username:" + username + "password:" + password;
Http javaHttp = new Http();
res = javaHttp.Post(requset_str, 1);
String arr[] = res.split("|");
if(arr[0].equals("200")) {
sr.setSamplerData(requset_str);
sr.setResponseData(arr[1], "utf-8");
sr.setSuccessful(true);
sr.setResponseCode("200");
sr.setResponseCodeOK();
sr.sampleEnd();
}else {
sr.setSamplerData(requset_str);
sr.setResponseData(arr[1], "utf-8");
sr.setSuccessful(false);
sr.sampleEnd();
}
} catch (Exception e) {
sr.setSamplerData(requset_str);
sr.setResponseData(e.getMessage(), "utf-8");
sr.setSuccessful(false);
sr.sampleEnd();
e.printStackTrace();
}
return sr;
}
public static void main(String[] args) {
JmeterSample runTestSample = new JmeterSample();
Arguments params = new Arguments();
JavaSamplerContext arg0 = new JavaSamplerContext(params);
runTestSample.runTest(arg0);
}
}
package com.perf.jmeter;
/**
* Created by xun.gong on 2016/12/26.
*/
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Http {
public String Post(String content, int seq) {
System.out.println(content);
String res="not response";
String code ="600";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
URL url = new URL("http://scottishroots.com/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(false);
connection.setRequestProperty("Content-Type", "text/html; charset=utf-8");
// connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
connection.setConnectTimeout(3*10000);
connection.setReadTimeout(3*10000);
connection.connect();
PrintWriter printWriter = new PrintWriter(connection.getOutputStream());
printWriter.write(content);
printWriter.flush();
printWriter.close();
try {
byte[] buf = new byte[1024];
int len = 0;
InputStream is = connection.getInputStream();
String responseCode = (new Integer(connection.getResponseCode()).toString());
String Message = (connection.getResponseMessage());
if(!(responseCode.equals("200")) || !(Message.equals("OK"))){
code="200";
}
while ((len = is.read(buf)) != -1) {
baos.write(buf, 0, len);
res = baos.toString();
}
baos.flush();
System.out.println("res: "+res);
baos.close();
is.close();
} catch (SocketTimeoutException e0) {
res = e0.getMessage();
System.out.println("SocketTimeoutException From (ServerResponse): " + e0);
}catch (Exception e1) {
res = e1.getMessage();
System.out.println("e1 From (ServerResponse): " + e1);
}
connection.disconnect();
} catch (Exception e2) {
res = e2.getMessage();
System.out.println("e2 From (ServerResponse): " + e2);
}
return code+"|"+res;
}
}
作者:
jingzizx
时间:
2017-11-29 13:47
学习
作者:
jingzizx
时间:
2017-11-29 13:47
真的很详细,赞!
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2