Jmeter自定义Java请求开发
一、本次实验目的
IDEA新建maven项目,使用java开发自定义jmeter的请求。
本次开发使用的代码,会百度云分享给大家。
二、本次实验环境
Idea |
2017.02 |
Jmeter |
5.1.1 |
三、新建maven项目
pom.xml文件中导入依赖。
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_functions -->
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_functions</artifactId>
<version>5.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_core -->
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>5.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_java -->
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>5.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
四、代码编写
4.1、以上一节监控的demo实现的两个接口为例,来编写实现请求的自定义请求
打开jmeter,添加javarequest如下图所示:
默认只有两个class。
4.2、新建class实现JavaSamplerClient接口
package com.jmeter.functions;
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;
public class CustemRequest implements JavaSamplerClient {
我们可以看到,具体会实现4个方法。
4.3、实现具体方法
启动上一节实现的应用。localhost:8086
具体实现代码如下:
package com.jmeter.functions;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
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.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class CustemRequest implements JavaSamplerClient {
//定义显示请求地址的变量名
private static final String REQUEST_RUL = "URL";
//定义默认请求的URL地址
private static final String DEFAULT_URL = "http://localhost:8086/user/get";
//用户输入的URL
private String inputUrl;
//用来保存http请求的响应结果,并把结果放到jmeter的查看结果树里面
private String ResponseResult;
//返回成功的状态码
private static final int SUCCESS_CODE = 200;
//以下方法,打印输出的目的就是检验此4个方法的执行顺序,大家执行的时候可以后台结果详细查看