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;
}
歡迎留言交流。