背景:可能在做定义化的要求上,jmeter原有的jar包,已经不能满足我们,就需要自己写一个方法,以下就是写入的一个模拟post的请求,在jmeter中使用的案例
一:写代码
1.代码中的有两个包,原谅我取名字取的cuo,第一个包名,com.databasecomputer这个包下有一个addcomputerinfo这个类,com.jmeter包下有httpcientPost这个类
com.jmeter.httpcientPost 一个post请求,是登录请求,打印的是code码和登录后的信息
package com.jemeter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
public class httpclientPost {
public static int sendpost() throws ClientProtocolException, IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost post = new HttpPost("https://user.XXX.com/m-users-a-act_sign.htm?pipeline=xx");
List params = new ArrayList();
params.add(new BasicNameValuePair("email", "zhang1@qq.com"));
params.add(new BasicNameValuePair("password", "zhangjun123"));
post.setEntity(new UrlEncodedFormEntity(params));
CloseableHttpResponse response2 = httpclient.execute(post);
int code = response2.getStatusLine().getStatusCode();
HttpEntity http = response2.getEntity();
InputStream instreams = http.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(instreams));
StringBuffer buffer = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "br ");
}
reader.close();
System.out.println(code +" respones:"+buffer);
return code;
}
}
com.databasecomputer.addcomputerinfo 以下就是调用,并且调用事务
package com.databasecomputer;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
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.jemeter.httpclientPost;
public class addcomputerInfo extends AbstractJavaSamplerClient {
public SampleResult runTest(JavaSamplerContext arg0) {
// TODO Auto-generated method stub
SampleResult result=new SampleResult();
result.sampleStart(); //事务启动
try {
int responeCode=httpclientPost.sendpost();
if (responeCode==200) {
result.setSuccessful(true);
}else {
result.setSuccessful(false);
}
} catch (ClientProtocolException e) {
result.setSuccessful(false);
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
result.setSuccessful(false);
e.printStackTrace();
}
result.sampleEnd();//事务关闭
return result;
}
public static void main(String[] args) {
new addcomputerInfo().runTest(new JavaSamplerContext(new Arguments()));
}
}
运行打印code码,返回登录成功,注意:在运行时,要导入jmeter的jar包,不然运行会报错,然后我就是不知道导入哪些包,出错一个问题,就导入相应的包,但是发现效率很低,干脆全部导入了
导包的过程,导入的是jmeter下lib的所有jar文件
二、打包
代码调试没有问题,就打包生成jar文件,生成文件可以用maven命令生成,也可以使用项目自己生成
生成文件为postdemo,文件路径是在jmeter下的lib/ext文件夹下
三、jmeter使用
注:如果生成jar成功,在启动jmeter中会去查找到这个方法名,自己加载出来,我在线程组中设置了10次
点击运行,控制台显示
运行的次数
说明调用jar成功