本简介是在 eclipse 中用 maven 工程编写,主要描述 java 插件的编写和使用。
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();
}
3.把代码导出为 JAR file
4.把 jar 包放在 apache-jmeter-3.3\lib\ext 中
5.在 Jmeter3.3 中运行 jmeter.bat
6.新增 java 请求,并选择相应的类再执行
7.下面上代码:
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("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;
}
}