Jmeter:通過JAVA代碼創建僅一次控制器(OnceOnlyController)

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;
    }

歡迎留言交流。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章