这是一个java代码形式的Jmeter POST请求。我运行此代码以获得没有错误,但我也没有得到任何响应,因为我的请求正文数据的方法似乎是错误的。
import java.io.FileOutputStream;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.gui.LoopControlPanel;
import org.apache.jmeter.control.gui.TestPlanGui;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.http.control.Header;
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
import org.apache.jmeter.protocol.http.gui.HeaderPanel;
//import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
//import org.apache.jmeter.samplers.SampleEvent;
//import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.SampleSaveConfiguration;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import org.apache.jmeter.protocol.http.util.HTTPArgument;
public class PostRequest {
public static void main(String[] argv) throws Exception {
//JMeter Engine
StandardJMeterEngine jmeter = new StandardJMeterEngine();
//JMeter initialization (properties, log levels, locale, etc)
JMeterUtils.setJMeterHome("/C:/Users/xxx");
JMeterUtils.loadJMeterProperties("/C:/xxx");
JMeterUtils.initLogging();
JMeterUtils.initLocale();
//JMeter Test Plan, basically JOrphan HashTree
HashTree testPlanTree = new HashTree();
//HTTP Sampler
HTTPSamplerProxy httpSampler = new HTTPSamplerProxy();
httpSampler.setDomain("100.100.100.100");
httpSampler.setPort(1111);
httpSampler.setPath("/");
httpSampler.setMethod("POST");
httpSampler.addNonEncodedArgument("","{data:{ \"email\" :
\"xx@gmail.com\",there are more values in the body"}]}","" );
httpSampler.setPostBodyRaw(true);
httpSampler.setFollowRedirects(true);
httpSampler.setAutoRedirects(false);
httpSampler.setUseKeepAlive(true);
httpSampler.setDoMultipartPost(false);
httpSampler.setProperty(TestElement.TEST_CLASS,
HTTPSamplerProxy.class.getName());
httpSampler.setProperty(TestElement.GUI_CLASS,
HttpTestSampleGui.class.getName());
//Header Manager
HeaderManager headerManager = new HeaderManager();
headerManager.setName(JMeterUtils.getResString("header_manager_title"));
headerManager.add(new Header("abcs", "asdasd"));
headerManager.add(new Header("dsferdg", "ertret"));
headerManager.setProperty(TestElement.TEST_CLASS,
HeaderManager.class.getName());
headerManager.setProperty(TestElement.GUI_CLASS,
HeaderPanel.class.getName());
httpSampler.setHeaderManager(headerManager);
//Loop Controller
LoopController loopController = new LoopController();
loopController.setLoops(1);
loopController.setFirst(true);
loopController.addTestElement(httpSampler);
loopController.setProperty(TestElement.TEST_CLASS,
LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS,
LoopControlPanel.class.getName());
loopController.initialize();
//Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Sample Thread Group");
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopController);
threadGroup.setProperty(TestElement.TEST_CLASS,
ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS,
ThreadGroupGui.class.getName());
//Test Plan
TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS,
TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new
ArgumentsPanel().createTestElement());
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(httpSampler,headerManager);
//Construct Test Plan from previously initialized elements
testPlanTree.add("testPlan", testPlan);
testPlanTree.add("loopController", loopController);
testPlanTree.add("threadGroup", threadGroup);
testPlanTree.add("httpSampler", httpSampler);
testPlanTree.add("headerManager", headerManager);
Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name",
"summary");
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}
ResultCollector resultcoll = new ResultCollector();
testPlanTree.add("resultcoll", resultcoll);
resultcoll.setFilename("C:/Users/xxx");
SampleSaveConfiguration saveConfiguration = new
SampleSaveConfiguration();
saveConfiguration.setAsXml(true);
saveConfiguration.setCode(true);
saveConfiguration.setLatency(true);
saveConfiguration.setTime(true);
saveConfiguration.setTimestamp(true);
resultcoll.setSaveConfig(saveConfiguration);
//Store execution results into a .jtl file, we can save file as csv also
String reportFile = "C:/Users/xxx.jtl";
String csvFile = "C:/Users/xxx.csv";
ResultCollector logger = new ResultCollector(summer);
logger.setFilename(reportFile);
ResultCollector csvlogger = new ResultCollector(summer);
csvlogger.setFilename(csvFile);
testPlanTree.add(testPlanTree.getArray()[0], logger);
testPlanTree.add(testPlanTree.getArray()[0], csvlogger);
//save generated test plan to JMeter's .jmx file format
SaveService.saveTree(threadGroupHashTree, new
FileOutputStream("C:\\Users\read.jmx"));
// Run Test Plan
jmeter.configure(testPlanTree);
jmeter.run();
/*System.out.println("Test completed. See " + JMeterHome + slash +
"report.jtl file for results");
System.out.println("JMeter .jmx script is available at " + jmeterHome +
slash + "jmeter_api_sample.jmx");*/
System.exit(0);
}
}
我已使用此代码配置jmeter POST请求并添加正文数据,但在检查响应时请求正文显示为null
。
答案 0 :(得分:1)
也许您应该向httpSampler
添加参数,而不是sampler
,例如:
HTTPSamplerProxy httpSampler = new HTTPSamplerProxy();
httpSampler.setDomain(xx);
httpSampler.setPort(xx);
httpSampler.setPath("xx");
httpSampler.setMethod("xx");
httpSampler.addNonEncodedArgument("body", body, "");
如果您正在使用JMeter API从Java代码构建JMeter测试,请查看Five Ways To Launch a JMeter Test without Using the JMeter GUI和jmeter-from-code存储库以获取可以重复使用的一些代码段。