我正在从头开始创建 JMeter 测试计划。这是我的代码:
public static void main(String[] args) throws Exception {
StandardJMeterEngine jmeterEngine = new StandardJMeterEngine();
JMeterUtils.setJMeterHome("/Users/myDir/Documents/JMeter/apache-jmeter-3.3");
JMeterUtils.loadJMeterProperties("src/main/resources/jmeter.properties");
JMeterUtils.initLocale();
//create test plan
TestPlan testPlan = new TestPlan();
testPlan.setEnabled(true);
testPlan.setName("Test Plan");
//create thread group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
// create http sampler
HTTPSampler httpSampler = new HTTPSampler();
httpSampler.setProtocol("https");
httpSampler.setDomain("myDomain.services.company.com");
httpSampler.setPath("ims/login/v1/token");
httpSampler.setMethod("POST");
httpSampler.setFollowRedirects(true);
httpSampler.setAutoRedirects(false);
httpSampler.setUseKeepAlive(true);
httpSampler.addArgument("client_id", "argValue1");
httpSampler.addArgument("scope", "argValue2");
httpSampler.addArgument("userName", "abc%2B249%40gmail.com");
httpSampler.addArgument("password", "Abc123");
//Add sampler to the thread group
threadGroup.addTestElement(httpSampler);
HashTree testPlanHashTree = new HashTree();
testPlanHashTree.add(testPlan);
testPlanHashTree.add(threadGroup);
// Generating the JMX file
SaveService.saveTree(testPlanHashTree, new FileOutputStream(JMeterUtils.getJMeterHome() + "/bin/Test2.jmx"));
}
当我执行此代码时,我的 Test2.jmx 被创建。
接下来,我尝试使用以下命令通过 Jmeter 的非 GUI 模式运行 Test2.jmx:
./jmeter.sh -n -t Test2.jmx
而不是运行我的测试计划 Jmeter 会引发以下错误:
Error in NonGUIDriver java.lang.IllegalArgumentException: Problem loading XML from:'/Users/chandrat/Documents/JMeter/apache-jmeter-3.3/bin/Test2.jmx', missing class com.thoughtworks.xstream.converters.ConversionException:
---- Debugging information ----
cause-exception : com.thoughtworks.xstream.converters.ConversionException
cause-message :
first-jmeter-class : org.apache.jmeter.save.converters.HashTreeConverter.unmarshal(HashTreeConverter.java:67)
class : org.apache.jmeter.save.ScriptWrapper
required-type : org.apache.jmeter.threads.ThreadGroup
converter-type : org.apache.jmeter.save.ScriptWrapperConverter
path : /jmeterTestPlan/org.apache.jorphan.collections.HashTree/org.apache.jorphan.collections.HashTree/ThreadGroup
line number : 6
version : 3.3 r1808647
-------------------------------
1)我不确定如何解决这个问题?原因信息是空白的?我应该怎么做才能执行我的测试计划而不会出现上述错误?
2)你能看看我的测试计划生成代码吗?那个代码对吗?应该将 testGroup 直接添加到树中还是先添加到测试计划中,然后再将测试计划添加到树中?
这是我的 pom 文件中的依赖项,以防我丢失了一个 jar。
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>jorphan</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_http</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>oro</groupId>
<artifactId>oro</artifactId>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>org.apache.avalon.framework</groupId>
<artifactId>avalon-framework-api</artifactId>
<version>4.3.1</version>
</dependency>
</dependencies>
请帮忙。谢谢!