我正在尝试以编程方式遍历现有 jmeter jmx 文件中的所有元素( Controller 、管理器、采样器等),但我找到的每个示例资源只允许我遍历一个大集合集群中的所有给定元素(无需维护分层顺序)。
到目前为止,我有以下内容:
JMeterUtils.loadJMeterProperties(".../jmeter-3.0/bin/jmeter.properties");
JMeterUtils.setJMeterHome(".../apache-jmeter-3.0");
JMeterUtils.initLogging();
JMeterUtils.initLocale();
SaveService.loadProperties();
HashTree testPlanTree = SaveService.loadTree(new File(".../example.jmx"));
SearchByClass<ThreadGroup> threadGroups = new SearchByClass<>(ThreadGroup.class);
testPlanTree.traverse(threadGroups);
Collection<ThreadGroup> threadGroupsRes = threadGroups.getSearchResults();
for (ThreadGroup threadGroup : threadGroupsRes) {
/* Gets me the main ThreadGroups for the test plan, but can't find a way
to traverse the threadGroup to find its Managers, Controllers,
Sub-Controllers, Response Assertions, etc */
}
SearchByClass<LoopController> controllerGroups = new SearchByClass<>(LoopController.class);
testPlanTree.traverse(controllerGroups);
Collection<LoopController> loopControllers = controllerGroups.getSearchResults();
for(LoopController loopController : loopControllers){
/* Gets every LoopController in the test plan but I can't find
a way to determine what its parent and children elements are */
}
我们的 jmx 文件非常复杂,使用了 jmeter 可用的多个元素,包括子 Controller ,我最终希望能够解析和重新编译 jmx 并进行任何所需的更改(同时保持相同的层次结构),但还没有找到许多完全解释 Jmeter java API 的资源。
最佳答案
jmx 只是一个带有
JMeterUtils.loadJMeterProperties(jmeterHome+"/bin/jmeter.properties");
JMeterUtils.setJMeterHome(jmeterHome);
//JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
JMeterUtils.initLocale();
SaveService.loadProperties();
//load the jmx file
HashTree testPlanTree = SaveService.loadTree(new File("yourJmxFile.jmx"));
//list of keys on testPlan level
List<Object> testPlanTreeKeys= (List<Object>) testPlanTree.list();
//access testplan hashtree
HashTree testPlanRoot=testPlanTree.get(testPlanTreeKeys.get(0));
// get the list of thread groups
List<Object> testPlanRootKeys= (List<Object>) testPlanRoot.list();
//access first thread group
HashTree threadGroupRoot=testPlanRoot.get(testPlanRootKeys.get(0));
//Iterate over thread group elements
for(Object obj : threadGroupRoot.list()){
if( obj instanceof HTTPSampler ) {
// http sampler
}
}
关于java - 如何使用 java jmeter API 按顺序遍历所有 Jmeter 测试计划元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42730315/