首先,jmeter中的sample的原理:
jmeter 中的java 请求,sample 原理,java testjmeter自带的包,把包放在类路径下面,通过反射机制,通过反射机制扫出来。
先导入五个jar
包
package com.young.testing91;
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;
public class addComputerInfoClient extends AbstractJavaSamplerClient {
private static String uriPOST = "http://localhost:9000/computers";
@Override
public Arguments getDefaultParameters() {
Arguments arguments = new Arguments();
arguments.addArgument("val", "");
return arguments;
}
public SampleResult runTest(JavaSamplerContext arg0) {
// 添加事务 new SampleResult
SampleResult result = new SampleResult();
result.sampleStart();
String value = arg0.getParameter("val");
try {
int responseCode = SendHttpRequest.sendPostRequest(value);
if (responseCode == 200) {
result.setSuccessful(true);
} else {
result.setSuccessful(false);
}
} catch (ClientProtocolException e) {
result.setSuccessful(false);
e.printStackTrace();
} catch (IOException e) {
result.setSuccessful(false);
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
result.sampleEnd();
return result;
}
public static void main(String[] args) {
new addComputerInfoClient().runTest(new JavaSamplerContext(new Arguments()));
}
}
package com.young.testing91;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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 SendHttpRequest {
// send url is http://localhost:9000/computers?f=ACE
private static String uriPOST = "http://localhost:9000/computers";
public static int sendPostRequest(String value) throws ClientProtocolException, IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(uriPOST);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", value));
params.add(new BasicNameValuePair("introduced", "2017-2-19"));
params.add(new BasicNameValuePair("discontinued", "2017-2-19"));
params.add(new BasicNameValuePair("company", "22"));
httpPost.setEntity(new UrlEncodedFormEntity(params));
long startTime = System.currentTimeMillis();
CloseableHttpResponse httpResponse = httpclient.execute(httpPost);
int responseCode = httpResponse.getStatusLine().getStatusCode();
System.out.println(responseCode + " ," + (System.currentTimeMillis() - startTime) + "ms");
return responseCode;
在jmeter里,多个参数用这个方法增加
arguments.addArgument("val", "");
导包的时候用runnable JAR file, 此方法可以把依赖的类都导出
在ecplise中的httpclient jar包版本要于jmeter中的版本一直或是比jmeter中的高,否则会报错java.lang.NoSuchFieldError: INSTANCE。
把导出的jar包放到jmeter中 lib 中的ext目录下,然后在jmeter中新建一个java请求,就可以选择刚才导入的jar包,选择参数
在这分享一个之前学习过的jmeter视频,还不错,关注公众号【测试开发加油站】
回复jmeter即可获得下载链接。
标签:java,请求,import,org,new,apache,http,jmeter From: https://blog.51cto.com/u_16084838/6217435