|
使用静态块加载JMeter配置文件
static{ { JMeterUtils.setJMeterHome(new File("G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1").getAbsolutePath()); JMeterUtils.loadJMeterProperties(new File("G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1\\bin/jmeter.properties").getAbsolutePath()); JMeterUtils.setProperty("saveservice_properties", "G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1\\bin/saveservice.properties"); JMeterUtils.setProperty("user_properties", "G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1\\bin/user.properties"); JMeterUtils.setProperty("upgrade_properties", "G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1\\bin/upgrade.properties"); JMeterUtils.setProperty("system_properties", "G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1\\bin/system.properties"); JMeterUtils.setProperty("proxy.cert.directory", new File("").getAbsolutePath()); JMeterUtils.setLocale(Locale.SIMPLIFIED_CHINESE); JMeterUtils.initLocale();
} } |
//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",arguments)); |
对应JMeter_GUI:
对应设置请求次数、线程数等
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)); |
对应JMeter_GUI:
主要设置请求IP、请求端口、请求类型、请求参数
HTTPSamplerProxy httpSamplerProxy = new HTTPSamplerProxy(); Arguments HTTPsamplerArguments = new Arguments(); HTTPArgument httpArgument = new HTTPArgument(); httpArgument.setProperty(new BooleanProperty("HTTPArgument.always_encode",false)); httpArgument.setProperty(new StringProperty("Argument.value", request)); httpArgument.setProperty(new StringProperty("Argument.metadata","=")); ArrayList list1.add(new TestElementProperty("",httpArgument)); HTTPsamplerArguments.setProperty(new CollectionProperty("Arguments.arguments",list1)); httpSamplerProxy.setProperty(new TestElementProperty("HTTPsampler.Arguments",HTTPsamplerArguments)); httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.domain", url)); httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.port", port)); httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.protocol", "http")); httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.path", api)); httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.method", "POST")); httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.contentEncoding", "")); httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.follow_redirects", true)); httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.postBodyRaw", true)); httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.auto_redirects", false)); httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.use_keepalive", true)); httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.DO_MULTIPART_POST", false)); httpSamplerProxy.setProperty(new StringProperty("TestElement.gui_class", "org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui")); httpSamplerProxy.setProperty(new StringProperty("TestElement.test_class", "org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy")); httpSamplerProxy.setProperty(new StringProperty("TestElement.name", "HTTP Request")); httpSamplerProxy.setProperty(new StringProperty("TestElement.enabled", "true")); httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.postBodyRaw", true)); httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.embedded_url_re", "")); httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.connect_timeout", "")); httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.response_timeout", ""));
|
对应JMeter_GUI:
Header header = new Header(); header.setProperty(new StringProperty("Header.name","Content-Type")); header.setProperty(new StringProperty("Header.value","application/json")); TestElementProperty HeaderElement = new TestElementProperty("",header); list2.add(HeaderElement); headerManager.setProperty(new CollectionProperty("HeaderManager.headers",list2)); headerManager.setProperty(new StringProperty("TestElement.test_class","org.apache.jmeter.protocol.http.control.HeaderManager")); headerManager.setProperty(new StringProperty("TestElement.name","HTTP Header Manager")); headerManager.setProperty(new StringProperty("TestElement.enabled","true")); headerManager.setProperty(new StringProperty("TestElement.gui_class","org.apache.jmeter.protocol.http.gui.HeaderPanel"));
|
对应JMeter_GUI:
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);
SaveService.saveTree(hashTreeTestPlan, new FileOutputStream("F:\\test.jmx")); |
两种方式实现
@Test public void testCmd() throws IOException { String command = "G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1\\bin\\jmeter -n -t F:\\test.jmx -l F:\\test.jtl"; Runtime.getRuntime().exec("cmd.exe /C start " + command); } |
执行结果:
2. 执行jmx文件,并调用
@Test public void test() throws IOException { File file = new File("F:\\test.jmx"); HashTree hashTree = SaveService.loadTree(file); JMeterEngine engine = new StandardJMeterEngine(); engine.configure(hashTree); ((StandardJMeterEngine) engine).run(); } |
执行结果: