运行这段程序前,需要拷贝JMeter安装目录下的/bin/saveservice.properties和/bin/jmeter.properties到工程的resources目录下/resources/jmeter/bin/.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.jmeter.assertions.ResponseAssertion;
import org.apache.jmeter.assertions.gui.AssertionGui;
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.protocol.java.control.gui.JavaTestSamplerGui;
import org.apache.jmeter.protocol.java.sampler.JavaSampler;
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.threads.ThreadGroup;
import org.apache.jmeter.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.visualizers.ViewResultsFullVisualizer;
import org.apache.jorphan.collections.HashTree;
public class JMeterTest {
public static HashTree testPlanTree;
// public static List<Object> listLeaf = new LinkedList<Object>();
public static HashTree javaSamplerHashTree = new HashTree();
/*****
*****添加没有assertion的java Sampler
public static void addJavaSampler(String nameOfJavaSampler) {
JavaSampler javaSampler = new JavaSampler();
javaSampler.setName(nameOfJavaSampler);
javaSampler.setClassname("com.mycompany.MySampler"); //这里的Java Sampler要另外写代码实现
Arguments arguments = new Arguments();
arguments.addArgument("something","somevalue");
arguments.addArgument("otherthing", "othervalue"));
javaSampler.setArguments(arguments);
javaSampler.setProperty(TestElement.TEST_CLASS, JavaSampler.class.getName());
javaSampler.setProperty(TestElement.GUI_CLASS, JavaTestSamplerGui.class.getName());
listLeaf.add(javaSampler);
}
****/
public void addJavaSamplerWithAssertion(String nameOfJavaSampler, String responseField,
String responsePatternType, String testStr) {
ResponseAssertion responseAssertion = new ResponseAssertion();
responseAssertion.setProperty(TestElement.TEST_CLASS, ResponseAssertion.class.getName());
responseAssertion.setProperty(TestElement.GUI_CLASS, AssertionGui.class.getName());
responseAssertion.setName("Response Assertion");
responseAssertion.setEnabled(true);
switch (responseField) {
case "TEXT":
responseAssertion.setTestFieldResponseData();
break;
case "DOCUMENT":
responseAssertion.setTestFieldResponseDataAsDocument();
break;
case "URL":
responseAssertion.setTestFieldURL();
break;
case "RESPONSE_CODE":
responseAssertion.setTestFieldResponseCode();
break;
case "RESPONSE_MESSAGE":
responseAssertion.setTestFieldResponseMessage();
break;
case "RESPONSE_HEADERS":
responseAssertion.setTestFieldResponseHeaders();
break;
}
responseAssertion.setAssumeSuccess(true);
switch (responsePatternType) {
case "CONTAINS":
responseAssertion.setToContainsType();
break;
case "MATCHES":
responseAssertion.setToMatchType();
break;
case "EQUALS":
responseAssertion.setToEqualsType();
break;
case "SUBSTRING":
responseAssertion.setToSubstringType();
break;
}
// responseAssertion.setToNotType();
responseAssertion.addTestString(testStr);
JavaSampler javaSampler = new JavaSampler();
javaSampler.setName(nameOfJavaSampler);
javaSampler.setClassname("com.mycompany.MySampler");
Arguments arguments = new Arguments();
arguments.addArgument("something","somevalue");
arguments.addArgument("otherthing", "othervalue"));
javaSampler.setArguments(arguments);
javaSampler.setProperty(TestElement.TEST_CLASS, JavaSampler.class.getName());
javaSampler.setProperty(TestElement.GUI_CLASS, JavaTestSamplerGui.class.getName());
javaSamplerHashTree.add(javaSampler).add(responseAssertion);
}
public static void genericScript() {
String slash = System.getProperty("file.separator");
File jmeterHome = new File(JMeterTest.class.getClassLoader().getResource("").getPath() + slash + "jmeter");
System.out.println(JMeterTest.class.getClassLoader().getResource("jmeter/bin/jmeter.properties").getPath());
System.out.println(jmeterHome.getPath());
File jmeterProperties = new File(JMeterTest.class.getClassLoader().getResource("jmeter/bin/jmeter.properties").getPath());
JMeterUtils.loadJMeterProperties(jmeterProperties.getPath());
JMeterUtils.setJMeterHome(jmeterHome.getPath());
JMeterUtils.initLocale();
HashTree testPlanTree = new HashTree();
SampleSaveConfiguration ssc = new SampleSaveConfiguration();
ResultCollector resultCollector = new ResultCollector();
resultCollector.setProperty(TestElement.TEST_CLASS, ResultCollector.class.getName());
resultCollector.setName("test result tree");
resultCollector.setErrorLogging(false);
resultCollector.setSaveConfig(ssc);
resultCollector.setProperty(TestElement.GUI_CLASS, ViewResultsFullVisualizer.class.getName());
// Loop Controller
TestElement loopController = new LoopController();
((LoopController) loopController).setLoops(3);
((LoopController) loopController).setFirst(true);
loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
((LoopController) loopController).initialize();
// Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setNumThreads(10);
threadGroup.setRampUp(1);
threadGroup.setName("Thread Group");
threadGroup.setSamplerController(((LoopController) 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).add(threadGroup);
threadGroupHashTree.add(resultCollector);
threadGroupHashTree.add(javaSamplerHashTree);
try {
SaveService.saveTree(testPlanTree,
new FileOutputStream(MyConfig.JMX_FOLDER + slash + MyConfig.JMETER_FILE));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("finish generating jmx file:" + MyConfig.JMX_FOLDER + slash + MyConfig.JMETER_FILE);
}
}