最近个人情绪不稳定、心情很不美丽,特别懒,没有博客和技术文章更新,这个文章也是迟到了周多的时间,啰嗦完了,开始正题吧。
我们在实际做性能测试的时候,一般会采用jmeter、Lr进行测试,但是很少有同学用jmeter的测试Java请求,也可以这么说,一般是开发自己搞的多一些。
准备工作自己编写一个接口(我用springboot随便写了一个,需要的可以加我好友)
新建一个maven工程
pom文件引用ApacheJMeter_core.jar ApacheJMeter_java.jar
写代码前该知道的1、创建类的时候需要去继承AbstractJavaSamplerClient抽象类或者是实现JavaSamplerClient接口
我在这里只继承AbstractJavaSamplerClient抽象类,需要复写的方法有:
public SampleResult runTest(JavaSamplerContext context) {}
public void setupTest(JavaSamplerContext context) {}
public void teardownTest(JavaSamplerContext context) {}
public Arguments getDefaultParameters() {}
2、针对复写方法说明:
public Arguments getDefaultParameters() {}
这个方法由Jmeter在进行添加javaRequest时第一个运行,它决定了你要在GUI中默认显示出哪些属性。
public void setupTest(JavaSamplerContext context) {}
这个方法相当于loadrunner中的init,我们可以用它来进行一些初始化的动作。
public SampleResult runTest(JavaSamplerContext context) {}
这个方法相当于loadrunner中的action,我们的核心测试代码就在这里了。
public void teardownTest(JavaSamplerContext context) {}
这个方法相当于loadrunner中的end,收尾的工作可以由它来做。
示例代码package com.jmeter.demo; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; 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; import org.apache.http.util.EntityUtils; 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 org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author rongrong * jmeter之自定义java请求性能测试 */ public class JmeterForJavaTest extends AbstractJavaSamplerClient { private static final Logger log = LoggerFactory.getLogger(AbstractJavaSamplerClient.class); //测试结果 private SampleResult sr; private static String lable = "放大招了奥!"; /** * 初始化性能测试时,每个测试线程 * * @param context */ @Override public void setupTest(JavaSamplerContext context) { log.debug(getClass().getName() + ": setupTest"); } /** * 主要设置传入的参数 * * @return */ @Override public Arguments getDefaultParameters() { Arguments arguments = new Arguments(); arguments.addArgument("url", "http://localhost:8888/login?"); arguments.addArgument("userName", "rongrong"); arguments.addArgument("passWord", "123456"); return arguments; } /** * 为性能测试的运行体 * * @param context * @return */ @Override public SampleResult runTest(JavaSamplerContext context) { String url = context.getParameter("url"); String userName = context.getParameter("userName"); String passWord = context.getParameter("passWord"); String sbResultData = null; sr = new SampleResult(); sr.setSampleLabel(lable); //记录测试时间及结果 sr.sampleStart(); //执行请求 sbResultData = executRequest(url, userName, passWord, sbResultData); sr.setSuccessful(true); sr.setResponseCode("200"); sr.setResponseCodeOK(); sr.setRequestHeaders("这是个header呀,但是我就没写呀"); sr.setResponseMessage("这块是自己瞎写的呀,测试是不是该严谨些呢"); sr.setContentType("application/json 没想到这块我也要皮一下呢"); //将数据打印到查看结果树当中 sr.setResponseData(sbResultData, null); sr.setDataType(SampleResult.TEXT); return sr; } /** * 模拟接口请求 * * @param url * @param userName * @param passWord * @param sbResultData * @return */ private String executRequest(String url, String userName, String passWord, String sbResultData) { CloseableHttpClient httpClient = null; HttpPost httpPost = null; try { httpClient = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build(); httpPost = new HttpPost(url); httpPost.setConfig(requestConfig); List<NameValuePair> ps = new ArrayList<NameValuePair>(); Map<String, String> params = new HashMap<String, String>(); params.put("userName", userName); params.put("passWord", passWord); for (String pKey : params.keySet()) { ps.add(new BasicNameValuePair(pKey, params.get(pKey))); } httpPost.setEntity(new UrlEncodedFormEntity(ps)); CloseableHttpResponse response = httpClient.execute(httpPost); HttpEntity httpEntity = response.getEntity(); sbResultData = EntityUtils.toString(httpEntity, "utf-8"); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (httpPost != null) { httpPost.releaseConnection(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } return sbResultData; } @Override public void teardownTest(JavaSamplerContext context) { log.debug(getClass().getName() + ": teardownTest"); } }