最近准备给jmeter增加⼀个web页⾯,不想通过上传jmx⽂件的⽅式,⽽是直接根据所填的信息⽣成jmx⽂件,下⾯展⽰⼀个最简单的http请求,其他的组件可以参考下⾯的组装。
import org.apache.jmeter.config.Arguments;import org.apache.jmeter.control.LoopController;import org.apache.jmeter.engine.JMeterEngine;
import org.apache.jmeter.engine.JMeterEngineException;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.sampler.HTTPSamplerProxy;import org.apache.jmeter.protocol.http.util.HTTPArgument;import org.apache.jmeter.reporters.ResultCollector;
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.testelement.property.*;import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.ListedHashTree;import org.testng.annotations.Test;
import org.apache.jmeter.threads.ThreadGroup;import java.io.File;
import java.io.IOException;import java.util.*;
public class JmeterTest { static{ {
JMeterUtils.setJMeterHome(new File(\"\").getAbsolutePath());
JMeterUtils.loadJMeterProperties(new File(\"src/main/resources/jmeter/jmeter.properties\").getAbsolutePath()); JMeterUtils.setProperty(\"saveservice_properties\ JMeterUtils.setProperty(\"user_properties\
JMeterUtils.setProperty(\"upgrade_properties\ JMeterUtils.setProperty(\"system_properties\// JMeterUtils.setProperty(\"proxy.cert.directory\ JMeterUtils.setLocale(Locale.CHINESE); JMeterUtils.initLocale();
} }
@Test
public void test() throws JMeterEngineException, IOException { File file = new File(\"D:\\\\jmeter_jmx\\\\HTTP Request.jmx\"); HashTree hashTree = SaveService.loadTree(file); JMeterEngine engine = new StandardJMeterEngine(); engine.configure(hashTree);
((StandardJMeterEngine) engine).run(); }
@Test
public void test04() throws IOException { String url = \"172.16.2.143\"; String port = \"8080\";
String api = \"/submit/job\"; String request = \"{}\";
//TestPlan
TestPlan testPlan = new TestPlan(\"Test Plan\"); testPlan.setFunctionalMode(false); testPlan.setSerialized(false);
testPlan.setTearDownOnShutdown(true);
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName()); testPlan.setProperty(TestElement.GUI_CLASS,\"TestPlanGui\");
testPlan.setProperty(new BooleanProperty(TestElement.ENABLED, true)); testPlan.setProperty(new StringProperty(\"TestPlan.comments\
testPlan.setProperty(new StringProperty(\"TestPlan.user_define_classpath\ Arguments arguments = new Arguments();
testPlan.setProperty(new TestElementProperty(\"TestPlan.user_defined_variables\
ThreadGroup threadGroup = new ThreadGroup(); threadGroup.setNumThreads(1); threadGroup.setRampUp(1); threadGroup.setDelay(0); threadGroup.setDuration(0);
threadGroup.setProperty(new StringProperty(ThreadGroup.ON_SAMPLE_ERROR, \"continue\")); threadGroup.setScheduler(false); threadGroup.setName(\"Group1\");
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName()); threadGroup.setProperty(TestElement.GUI_CLASS,\"ThreadGroup\");
threadGroup.setProperty(new BooleanProperty(TestElement.ENABLED, true));
LoopController loopController = new LoopController(); loopController.setLoops(0);
loopController.setProperty(new BooleanProperty(\"LoopController.continue_forever\
loopController.setProperty(new StringProperty(\"TestElement.gui_class\ loopController.setProperty(new StringProperty(\"TestElement.test_class\ loopController.setProperty(new StringProperty(\"TestElement.name\ loopController.setProperty(new StringProperty(\"TestElement.enabled\ loopController.setProperty(new StringProperty(\"LoopController.loops\
threadGroup.setProperty(new TestElementProperty(\"ThreadGroup.main_controller\
HTTPSamplerProxy httpSamplerProxy = new HTTPSamplerProxy(); Arguments HTTPsamplerArguments = new Arguments(); HTTPArgument httpArgument = new HTTPArgument();
httpArgument.setProperty(new BooleanProperty(\"HTTPArgument.always_encode\ httpArgument.setProperty(new StringProperty(\"Argument.value\ httpArgument.setProperty(new StringProperty(\"Argument.metadata\ ArrayList HTTPsamplerArguments.setProperty(new CollectionProperty(\"Arguments.arguments\ httpSamplerProxy.setProperty(new TestElementProperty(\"HTTPsampler.Arguments\ httpSamplerProxy.setProperty(new StringProperty(\"HTTPSampler.domain\ httpSamplerProxy.setProperty(new StringProperty(\"HTTPSampler.port\ httpSamplerProxy.setProperty(new StringProperty(\"HTTPSampler.protocol\ httpSamplerProxy.setProperty(new StringProperty(\"HTTPSampler.path\ httpSamplerProxy.setProperty(new StringProperty(\"HTTPSampler.method\ httpSamplerProxy.setProperty(new StringProperty(\"HTTPSampler.contentEncoding\ httpSamplerProxy.setProperty(new BooleanProperty(\"HTTPSampler.follow_redirects\ httpSamplerProxy.setProperty(new BooleanProperty(\"HTTPSampler.postBodyRaw\ httpSamplerProxy.setProperty(new BooleanProperty(\"HTTPSampler.auto_redirects\ httpSamplerProxy.setProperty(new BooleanProperty(\"HTTPSampler.use_keepalive\ httpSamplerProxy.setProperty(new BooleanProperty(\"HTTPSampler.DO_MULTIPART_POST\ httpSamplerProxy.setProperty(new StringProperty(\"TestElement.gui_class\ httpSamplerProxy.setProperty(new StringProperty(\"TestElement.test_class\ httpSamplerProxy.setProperty(new StringProperty(\"TestElement.name\ httpSamplerProxy.setProperty(new StringProperty(\"TestElement.enabled\ httpSamplerProxy.setProperty(new BooleanProperty(\"HTTPSampler.postBodyRaw\ httpSamplerProxy.setProperty(new StringProperty(\"HTTPSampler.embedded_url_re\ httpSamplerProxy.setProperty(new StringProperty(\"HTTPSampler.connect_timeout\ httpSamplerProxy.setProperty(new StringProperty(\"HTTPSampler.response_timeout\ ResultCollector resultCollector = new ResultCollector(); HeaderManager headerManager = new HeaderManager(); SampleSaveConfiguration sampleSaveConfiguration = new SampleSaveConfiguration(); sampleSaveConfiguration.setTime(true); sampleSaveConfiguration.setLatency(true); sampleSaveConfiguration.setTimestamp(true); sampleSaveConfiguration.setSuccess(true); sampleSaveConfiguration.setLabel(true); sampleSaveConfiguration.setCode(true); sampleSaveConfiguration.setMessage(true); sampleSaveConfiguration.setThreadName(true); sampleSaveConfiguration.setDataType(true); sampleSaveConfiguration.setEncoding(false); sampleSaveConfiguration.setAssertions(true); sampleSaveConfiguration.setSubresults(true); sampleSaveConfiguration.setResponseData(false); sampleSaveConfiguration.setSamplerData(false); sampleSaveConfiguration.setAsXml(false); sampleSaveConfiguration.setFieldNames(true); sampleSaveConfiguration.setResponseHeaders(false); sampleSaveConfiguration.setRequestHeaders(false); //sampleSaveConfiguration.setAssertionResultsFailureMessage(true); responseDataOnError sampleSaveConfiguration.setAssertionResultsFailureMessage(true); //sampleSaveConfiguration.setsserAtionsResultsToSave(0); assertionsResultsToSave sampleSaveConfiguration.setBytes(true); sampleSaveConfiguration.setSentBytes(true); sampleSaveConfiguration.setUrl(true); sampleSaveConfiguration.setThreadCounts(true); sampleSaveConfiguration.setIdleTime(true); sampleSaveConfiguration.setConnectTime(true); resultCollector.setProperty(new BooleanProperty(\"ResultCollector.error_logging\ resultCollector.setProperty(new ObjectProperty(\"saveConfig\ resultCollector.setProperty(new StringProperty(\"TestElement.gui_class\ resultCollector.setProperty(new StringProperty(\"TestElement.name\ resultCollector.setProperty(new StringProperty(\"TestElement.enabled\ resultCollector.setProperty(new StringProperty(\"filename\ ArrayList Header header = new Header(); header.setProperty(new StringProperty(\"Header.name\ header.setProperty(new StringProperty(\"Header.value\ TestElementProperty HeaderElement = new TestElementProperty(\"\ list2.add(HeaderElement); headerManager.setProperty(new CollectionProperty(\"HeaderManager.headers\ headerManager.setProperty(new StringProperty(\"TestElement.test_class\ headerManager.setProperty(new StringProperty(\"TestElement.name\ headerManager.setProperty(new StringProperty(\"TestElement.enabled\ headerManager.setProperty(new StringProperty(\"TestElement.gui_class\ ListedHashTree hashTreeResultCollectorAndHeaderManager = new ListedHashTree(); hashTreeResultCollectorAndHeaderManager.add(resultCollector); hashTreeResultCollectorAndHeaderManager.add(headerManager); ListedHashTree hashTreeHTTPSamplerProxy = new ListedHashTree(); hashTreeHTTPSamplerProxy.add(httpSamplerProxy,hashTreeResultCollectorAndHeaderManager); ListedHashTree hashTreeThreadGroup = new ListedHashTree(); hashTreeThreadGroup.add(threadGroup,hashTreeHTTPSamplerProxy); ListedHashTree hashTreeTestPlan = new ListedHashTree(); hashTreeTestPlan.add(testPlan,hashTreeThreadGroup); //可以将⽣成的hashTree保存成⼀个jmx⽂件 与Jmeter⾃⼰⽣成的做对⽐ //SaveService.saveTree(hashTreeTestPlan, new FileOutputStream(\"D:\\\\jmeter_jmx\\\est.jmx\")); JMeterEngine engine = new StandardJMeterEngine(); //engine.configure(SaveService.loadTree(new File(\"D:\\\\jmeter_jmx\\\est.jmx\"))); engine.configure(hashTreeTestPlan); ((StandardJMeterEngine) engine).run(); }} 上⽅代码中Test04⽣成的jmx⽂件如下所⽰ 如果需要添加其他元素的,可以参考jmeter⽣成jmx的结构,在此基础上⾃⾏拼装 因篇幅问题不能全部显示,请点此查看更多更全内容true