使用Java JMeter创建测试场景

时间:2017-10-30 10:35:48

标签: java jmeter jmeter-plugins

我试图找到一种方法来找到转换JTL和amp;的方法。 JMX进入dummys采样器以获得结果&一个组件中的请求。但是我在使用Java创建组件时遇到了困难。我一直在关注这个教程blazermaeter 5 ways launch jmeter without gui,但它对我帮助不大 这是我生成所有组件的函数:

public static HashTree GenerateTestPlan(ArrayList<Dummy> dummys)
{

    HashTree testTreePlan = new HashTree();

    // Creating the test plan component
    TestPlan testPlan = new TestPlan("My Dummy Plan");

    // Creating the thread group
    Calendar calendar1 = Calendar.getInstance();
    Calendar calendar2 = Calendar.getInstance();
    calendar1.set(1970, 01, 01);
    long milliseconds1 = calendar1.getTimeInMillis();
    long milliseconds2 = calendar2.getTimeInMillis();
    long diff = milliseconds2 - milliseconds1;
    long seconds = diff / 1000;
    ThreadGroupGui threadGroupGui = new ThreadGroupGui();       
    ThreadGroup threadGroup = new ThreadGroup();
    threadGroup.setName("Thread group");
    threadGroup.setNumThreads(1);
    threadGroup.setRampUp(1);
    threadGroup.setStartTime(seconds);
    threadGroup.setEndTime(seconds);
    threadGroup.setScheduler(false);
    threadGroup.setDuration(7200);
    threadGroup.setDelay(0);


    // Loop controller
    LoopController loopController = new LoopController();
    loopController.setLoops(1);
    loopController.setFirst(true);
    ArrayList<DummySampler> initializedDummy = new ArrayList<DummySampler>();

    // For each dummy, creating a dummy sampler  
    for(Dummy dummy : dummys)
    {
        DummySampler dummySampler = new DummySampler(); 
        dummySampler.setComment("Auto-generated dummy-sampler");
        dummySampler.setSuccessful(dummy.successfulSample);
        dummySampler.setResponseCode(Integer.toString(dummy.responseCode));
        dummySampler.setConnectTime(Integer.toString(dummy.connectTime));
        dummySampler.setLatency(Integer.toString(dummy.latency));
        dummySampler.setResponseTime(Integer.toString(dummy.responseTime));
        dummySampler.setRequestData(dummy.requestData);
        dummySampler.setResponseMessage(dummy.responseMessage);
        loopController.addTestElement(dummySampler);
        initializedDummy.add(dummySampler);
    }
    loopController.initialize();
    threadGroup.setSamplerController(loopController);
    threadGroup.initialize();



    // Ajout à ce groupe d'utilisateur un thread group
    testTreePlan.add("testPlan",testPlan);
    testTreePlan.add("loopController",loopController);
    testTreePlan.add("threadGroup",threadGroup);
    for(DummySampler dummyS : initializedDummy)
    {
        testTreePlan.add("Dummy Sampler",dummyS);
    }       


    return testTreePlan;        
}

然后我以这种方式创建一个jmx文件:

HashTree testPlan = utils.GenerateTestPlan(dummys);
jmeter.configure(testPlan);
SaveService.loadProperties();
String destinationPath = JMeterUtils.getJMeterHome()+"test.jmx";
SaveService.saveTree(testPlan,new FileOutputStream(destinationPath));

1 个答案:

答案 0 :(得分:0)

按照此代码Blazemeter java from code

找到一种方法

我的代码现在:

public static HashTree GenerateTestPlan(ArrayList<Dummy> dummys)
{

    HashTree testPlanTree = new HashTree();

    // Creating the test plan component
    TestPlan testPlan = new TestPlan("My Dummy Plan");
    testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
    testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
    testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

    // Loop controller
    LoopController loopController = new LoopController();
    loopController.setLoops(1);
    loopController.setFirst(true);
    loopController.setProperty(loopController.TEST_CLASS, LoopController.class.getName());
    loopController.setProperty(loopController.GUI_CLASS, LoopControlPanel.class.getName());

    loopController.initialize();

    // Creating the thread group
    Calendar calendar1 = Calendar.getInstance();
    Calendar calendar2 = Calendar.getInstance();
    calendar1.set(1970, 01, 01);
    long milliseconds1 = calendar1.getTimeInMillis();
    long milliseconds2 = calendar2.getTimeInMillis();
    long diff = milliseconds2 - milliseconds1;
    long seconds = diff / 1000;

    ThreadGroup threadGroup = new ThreadGroup();
    threadGroup.setName("Thread group");
    threadGroup.setNumThreads(1);
    threadGroup.setRampUp(1);
    threadGroup.setStartTime(seconds);
    threadGroup.setEndTime(seconds);
    threadGroup.setScheduler(false);
    threadGroup.setDuration(7200);
    threadGroup.setDelay(0);
    threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
    threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
    threadGroup.setSamplerController(loopController);

    // Creating Hash trees
    testPlanTree.add(testPlan);
    HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
    // Adding all the dummy sampler 
    for(Dummy dummy : dummys)
    {
        DummySampler dummySampler = new DummySampler(); 
        dummySampler.setProperty(TestElement.TEST_CLASS, DummySampler.class.getName());
        dummySampler.setProperty(TestElement.GUI_CLASS, DummySamplerGui.class.getName());
        dummySampler.setComment("Auto-generated dummy-sampler");
        dummySampler.setSuccessful(dummy.successfulSample);
        dummySampler.setResponseCode(Integer.toString(dummy.responseCode));
        dummySampler.setConnectTime(Integer.toString(dummy.connectTime));
        dummySampler.setLatency(Integer.toString(dummy.latency));
        dummySampler.setResponseTime(Integer.toString(dummy.responseTime));
        System.out.println(dummy.requestData);
        dummySampler.setRequestData(dummy.requestData);
        dummySampler.setResponseData(dummy.responseData);
        dummySampler.setResponseMessage(dummy.responseMessage);
        threadGroupHashTree.add(dummySampler);

    }       
    return testPlanTree;        
}