如何在jmeter中使用beanshell预处理器获取给事务控制器的名称

How to get the name given to transaction controller using beanshell preprocesssor in jmeter

我想在 JMeter 中使用 BeanShell 预处理器获取指定给事务控制器的名称。 我想稍后使用 header 管理器在 dynaTrace 中连接和显示。

我使用 BeanShell 侦听器尝试过类似的东西

String test = sampleResult.getSampleLabel();
log.info(test);

但我想使用预处理器。

log.info(sampler.getName());

这个是用来获取sampler的名字,和我想获取transaction controller的名字类似

具体来说,我想使用 BeanShell 预处理器。

有人可以帮助我吗?

你不能走得比 Previous Result or Previous Sampler 更远,所以我要说这不是你可以轻易实现的东西。看起来您的测试设计得不是很好,因为通常人们不需要知道父采样器控制器的名称。

尽管如此,您仍可以访问 JMeter Test Plan Tree 并从中获取信息。示例代码类似于:

import org.apache.jmeter.control.TransactionController;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.SearchByClass;

import java.lang.reflect.Field;
import java.util.Collection;

StandardJMeterEngine engine = ctx.getEngine();
Field test = engine.getClass().getDeclaredField("test");
test.setAccessible(true);
HashTree testPlanTree = (HashTree) test.get(engine);

SearchByClass txnCtrlSearch = new SearchByClass(TransactionController.class);
testPlanTree.traverse(txnCtrlSearch);
Collection txnControllers = txnCtrlSearch.getSearchResults();

for (Object txnController : txnControllers) {
    log.info(((TransactionController) txnController).getName());
}

演示:

关于从 Beanshell 脚本使用 JMeter API 的一些信息:How to Use BeanShell: JMeter's Favorite Built-in Component