Jmeter:通过JAVA代码创建仅一次控制器(OnceOnlyController)

最近在研究通过JAVA动态创建Jmeter脚本,对于创建OnceOnlyController ,实在没找到相关资料,就自己研究了下,不是很难,代码如下:

/**
* 创建OnceOnlyController 仅一次控制器
* @param threadGroupHashTree
* @param taskId
*/
public static void createOnceOnlyController(HashTree threadGroupHashTree ,String taskId) {
OnceOnlyController onceOnlyController=new OnceOnlyController();
onceOnlyController.setProperty("testname", "test");
onceOnlyController.setProperty("enable", true);

HashTree testPlanTree = new HashTree();
testPlanTree.add(createBeanShellPreProcessor(taskId));

BeanShellSampler beanShellSampler = new BeanShellSampler();
beanShellSampler.setName("test" + taskId);
beanShellSampler.setProperty("BeanShellSampler.query", "");
testPlanTree.add(beanShellSampler);

//将BeanShellPreProcessor和BeanShellSampler 添加到OnceOnlyController下
threadGroupHashTree.add(onceOnlyController,testPlanTree);
}

/**
* 创建BeanShell 预处理器
* @return
*/
public static BeanShellPreProcessor createBeanShellPreProcessor(String taskId) {
BeanShellPreProcessor preProcessor=new BeanShellPreProcessor();
preProcessor.setEnabled(true);
preProcessor.setProperty("resetInterpreter", false);


String script="System.out.println(\"预处理num:\"+ctx.getThreadNum());";

preProcessor.setProperty("script", script);



preProcessor.setProperty(TestElement.ENABLED, true);
preProcessor.setProperty(TestElement.NAME,"abcd");
preProcessor.setProperty(TestElement.TEST_CLASS, preProcessor.getClass().getName());
preProcessor.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());
System.out.println("createBeanShellPreProcessor:"+preProcessor.getPropertyAsString("script"));
return preProcessor;
}

欢迎留言交流。