jmeter 压测duobbo接口,jmeter代码不合理,导致每执行一次请求,会调用一次消耗内存的实例化。导致越压越慢,请求发不出去。这个时候需要考虑修改代码了。
截图中,tps越来越少。
原来初始化的代码放在
runTest中执行。
修改后放在
setupTest方法中运行,只运行一次。解决实例化消耗内存问题。
import com.alibaba.fastjson.JSONObject; 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.springframework.context.support.ClassPathXmlApplicationContext;import java.util.Date;public class TestMsgTemplate extends AbstractJavaSamplerClient {private static TemplateMsgFacade templateMsgFacade;@Overridepublic void setupTest(JavaSamplerContext arg0) {if (TestMsgTemplate.templateMsgFacade == null) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:app-dubbo-client-context.xml");TestMsgTemplate.templateMsgFacade = (TemplateMsgFacade) context.getBean("templateMsgFacade");}}@Overridepublic Arguments getDefaultParameters() {Arguments params = new Arguments();params.addArgument("templateCode", "");return params;}@Overridepublic SampleResult runTest(JavaSamplerContext javaSamplerContext) {String templateCode = javaSamplerContext.getParameter("templateCode");SampleResult sr = new SampleResult();sr.setSampleLabel("MsgTemplate:消息模板");sr.sampleStart();TemplateMsgDto dto = new TemplateMsgDto();dto.setTemplateCode(templateCode);dto.setMobile("13922808010");dto.setPointCode("FC7555535");TemplateMsg msg = templateMsgFacade.getMsgTemplate(dto);String abc = JSONObject.toJSONString(msg);msg = null;if(abc.contains("templateSource")){//sr.setResponseData("templateSource 成功"+ templateCode, null);sr.setResponseData(templateCode+"成功", null);sr.setDataType("text");sr.setSuccessful(true);sr.setResponseCodeOK();}else{// sr.setResponseData("templateSource 失败"+ templateCode, null);sr.setResponseData(templateCode+"失败", null);sr.setDataType("text");sr.setSuccessful(false);sr.setResponseCode("404");}sr.sampleEnd();// abc = null;return sr;}/*** @param args*/public static void main(String[] args){for(int i = 0;i <20000000;i ++){System.out.println(i);Date nowTime = new Date();System.out.println(nowTime);//方法二:Date方式,输出现在时间Arguments params = new Arguments();params.addArgument("templateCode", "hibox.noticeStaffAgainPost_ZH_CN");JavaSamplerContext arg0 = new JavaSamplerContext(params);TestMsgTemplate qap = new TestMsgTemplate();qap.setupTest(arg0);qap.runTest(arg0);qap.teardownTest(arg0);}} }
重新运行压测代码j结果,tps 明显提高很多: