2017-05-25 80 views
0

我正在使用自定義線程組,FreeFormArrivalsThreadGroup由火焰特性提供使用jmeter-plugins-casutg-2.1.jar如何使用java在jmeter中創建自定義線程組?

當我嘗試創建用java該線程組(在創建從Java JMX的過程)

FreeFormArrivalsThreadGroup freeFormArrivalsThreadGroup = new FreeFormArrivalsThreadGroup(); 

代碼:

package com.create.jmx; 

import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 

import org.apache.jmeter.control.LoopController; 
import org.apache.jmeter.control.gui.LoopControlPanel; 
import org.apache.jmeter.control.gui.TestPlanGui; 
import org.apache.jmeter.gui.util.PowerTableModel; 
import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui; 
import org.apache.jmeter.protocol.http.sampler.HTTPSampler; 
import org.apache.jmeter.save.SaveService; 
import org.apache.jmeter.testelement.TestElement; 
import org.apache.jmeter.testelement.TestPlan; 
import org.apache.jmeter.util.JMeterUtils; 
import org.apache.jorphan.collections.HashTree; 

import com.blazemeter.jmeter.threads.arrivals.FreeFormArrivalsThreadGroup; 
import com.blazemeter.jmeter.threads.arrivals.FreeFormArrivalsThreadGroupGui; 


public class CreateJMX 
{ 
    final static String JMETER_HOME = "jmeter_home_location"; 

    public static void main(String[] args) throws FileNotFoundException, IOException 
    { 
     try 
     { 
      //creating a test plan 
      TestPlan testPlan = new TestPlan(); 
      testPlan.setName("From Program"); 
      testPlan.setEnabled(true); 
      testPlan.setProperty(TestElement.TEST_CLASS, 
            TestPlan.class.getName()); 
      testPlan.setProperty(TestElement.GUI_CLASS, 
            TestPlanGui.class.getName()); 
      //create a loop controller 
      LoopController loopController = new LoopController(); 
      loopController.setEnabled(true); 
      loopController.setLoops(3); 
      loopController.setProperty(TestElement.TEST_CLASS, 
             LoopController.class.getName()); 
      loopController.setProperty(TestElement.GUI_CLASS, 
             LoopControlPanel.class.getName()); 

      // creating freeFormArrivalsThreadGroup 
      FreeFormArrivalsThreadGroup freeFormArrivalsThreadGroup = new FreeFormArrivalsThreadGroup(); 
      freeFormArrivalsThreadGroup.setName("Free Form Arrivals Thread Group"); 
      PowerTableModel schedule = new PowerTableModel(new String[]{"Start", "End", "Duration"}, 
        new Class[]{Integer.class, Integer.class, Integer.class}); 
      schedule.addRow(new Object[]{1, 10, 60}); 
      freeFormArrivalsThreadGroup.setSamplerController(loopController); 
      freeFormArrivalsThreadGroup.setData(schedule); 
      freeFormArrivalsThreadGroup.setProperty(TestElement.TEST_CLASS, FreeFormArrivalsThreadGroup.class.getName()); 
      freeFormArrivalsThreadGroup.setProperty(TestElement.GUI_CLASS, FreeFormArrivalsThreadGroupGui.class.getName()); 

      //creating HTTP sampler 
      HTTPSampler httpSampler = new HTTPSampler(); 
      httpSampler.setEnabled(true); 
      httpSampler.setName("Google"); 
      httpSampler.setDomain("www.google.com"); 
      httpSampler.setPath("/"); 
      httpSampler.setMethod("GET"); 
      httpSampler.setProperty(TestElement.TEST_CLASS, 
        HTTPSampler.class.getName()); 
      httpSampler.setProperty(TestElement.GUI_CLASS, 
        HttpTestSampleGui.class.getName()); 

      HashTree hashTree = new HashTree(); 

      hashTree.add(testPlan).add(freeFormArrivalsThreadGroup).add(httpSampler); 

      JMeterUtils.setJMeterHome(JMETER_HOME); 
      JMeterUtils.loadJMeterProperties(JMeterUtils.getJMeterBinDir()+"/jmeter.properties"); 

      SaveService.saveTree(hashTree, new FileOutputStream("ouputfile.jmx")); 
     } 
     catch(Throwable e) 
     { 
      e.printStackTrace(); 
     } 

    } 
} 

上述線拋出異常。這裏是堆棧跟蹤

java.lang.ExceptionInInitializerError 
    at org.apache.jmeter.reporters.ResultCollector.<init>(ResultCollector.java:173) 
    at org.apache.jmeter.reporters.ResultCollector.<init>(ResultCollector.java:163) 
    at com.blazemeter.jmeter.reporters.FlushingResultCollector.<init>(FlushingResultCollector.java:7) 
    at com.blazemeter.jmeter.threads.AbstractDynamicThreadGroupModel.<init>(AbstractDynamicThreadGroupModel.java:28) 
    at com.blazemeter.jmeter.threads.AbstractDynamicThreadGroup.<init>(AbstractDynamicThreadGroup.java:23) 
    at com.blazemeter.jmeter.threads.arrivals.ArrivalsThreadGroup.<init>(ArrivalsThreadGroup.java:20) 
    at com.blazemeter.jmeter.threads.arrivals.FreeFormArrivalsThreadGroup.<init>(FreeFormArrivalsThreadGroup.java:11) 
    at com.create.jmx.CreateJMX.main(CreateJMX.java:49) 
Caused by: java.lang.NullPointerException 
    at org.apache.jmeter.samplers.SampleSaveConfiguration.<clinit>(SampleSaveConfiguration.java:296) 
    ... 8 more 

幫我用java

回答

0
  1. 這是不是一個完整的異常堆棧跟蹤創建時間表這個插件,您需要提供輸出(更好地與您的代碼清單顯示之前的行失敗)
  2. Free-Form Arrivals Thread Groupjmeter-plugins-casutg提供,而不是jmeter-plugins-cmn-jmeter,但是您需要在您的項目類路徑中同時具有這兩個jar。
  3. 如果您無法找到以編程方式創建和配置JMeter測試元素的方式,我會建議考慮在GUI模式下創建它。您將能夠更容易地從Java代碼運行現有的.jmx測試腳本,而不僅僅是使用JMeter API來創建它。

以防萬一,如果你還需要這一點,代碼應該是這樣的:

FreeFormArrivalsThreadGroup freeFormArrivalsThreadGroup = new FreeFormArrivalsThreadGroup(); 
freeFormArrivalsThreadGroup.setName("Free Form Arrivals Thread Group"); 
PowerTableModel schedule = new PowerTableModel(new String[]{"Start", "End", "Duration"}, 
     new Class[]{Integer.class, Integer.class, Integer.class}); 
schedule.addRow(new Object[]{1, 10, 60}); 
freeFormArrivalsThreadGroup.setSamplerController(loopController); 
freeFormArrivalsThreadGroup.setData(schedule); 
freeFormArrivalsThreadGroup.setProperty(TestElement.TEST_CLASS, FreeFormArrivalsThreadGroup.class.getName()); 
freeFormArrivalsThreadGroup.setProperty(TestElement.GUI_CLASS, FreeFormArrivalsThreadGroupGui.class.getName()); 

Five Ways To Launch a JMeter Test without Using the JMeter GUI指南關於開展JMeter的測試(包括計劃的)

+0

的不同方式的詳細信息在使用示例代碼之後,我也會遇到同樣的異常。當創建FreeFormArrivalsThreadGroup的對象時,它拋出_NullPointerException_ – KC7

+0

謝謝@Dmitri T.問題是我在創建_FreeFormArrivalsThreadGroup_之後配置/添加了_JMeterUtils_。只是重新安排了代碼,其工作正常。接受了答案。 – KC7

相關問題