Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JMeter将结果保存到文件_Java_Jmeter - Fatal编程技术网

Java JMeter将结果保存到文件

Java JMeter将结果保存到文件,java,jmeter,Java,Jmeter,在JMeterGUI中,可以很容易地添加一个监听器,例如“查看结果树”或“查看表中的结果”,并添加一个文件名 我有一个方法,它创建并运行一个测试计划,如下所示 //JMeter Engine StandardJMeterEngine jmeter = new StandardJMeterEngine(); //JMeter initialization (properties, log levels, locale, etc) JMeterUtils.loadJMeterProperties

在JMeterGUI中,可以很容易地添加一个监听器,例如“查看结果树”或“查看表中的结果”,并添加一个文件名

我有一个方法,它创建并运行一个测试计划,如下所示

//JMeter Engine
StandardJMeterEngine jmeter = new StandardJMeterEngine();

//JMeter initialization (properties, log levels, locale, etc)
JMeterUtils.loadJMeterProperties("/path/to/apache-jmeter-4.0/bin/jmeter.properties");
JMeterUtils.setJMeterHome("/path/to/apache-jmeter-4.0");
JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
JMeterUtils.initLocale();

// JMeter Test Plan, basic all u JOrphan HashTree
HashTree testPlanTree = new HashTree();

// HTTP Sampler
HTTPSampler httpSampler = new HTTPSampler();
httpSampler.setDomain("example.com");
httpSampler.setPort(80);
httpSampler.setPath("/");
httpSampler.setMethod("GET");

// Loop Controller
LoopController loopController = new LoopController();
loopController.setLoops(1);
loopController.addTestElement(httpSampler);
loopController.setFirst(true);
loopController.initialize();


// Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopController);

// Test Plan
TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");

// Construct Test Plan from previously initialized elements
testPlanTree.add("testPlan", testPlan);
testPlanTree.add("loopController", loopController);
testPlanTree.add("threadGroup", threadGroup);
testPlanTree.add("httpSampler", httpSampler);

// Run Test Plan
jmeter.configure(testPlanTree);
jmeter.run();
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'log4j2.debug' to show Log4j2 internal initialization logging.
Apr 16, 2018 1:40:32 PM java.util.prefs.WindowsPreferences <init>
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5. summary =      0 in 00:00:00 = ******/s Avg:     0 Min: 9223372036854775807 Max: -9223372036854775808 Err:     0 (0.00%)`
summary =      0 in 00:00:00 = ******/s Avg:     0 Min: 9223372036854775807 Max: -9223372036854775808 Err:     0 (0.00%)
  • 是否有一个方便的预制测试元素可以简单地添加到线程组,并将结果直接保存到文件中(与gui中的“在表中查看结果”侦听器的方式相同)
  • 实现执行此任务的侦听器看起来是什么样子的
  • 编辑:我使用ResultCollector添加了更改,它成功地保存了data.csv文件,但是,它只创建了csv列名,但从未创建任何结果

    Summariser summer = null;
    String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");//$NON-NLS-1$
    if (summariserName.length() > 0) {
        summer = new Summariser(summariserName);
    }
    
    ResultCollector rc = new ResultCollector(summer);
    rc.setFilename("C:\\Users\\user\\data.csv");
    rc.setErrorLogging(true);
    
    threadGroup.addTestElement(rc);
    testPlanTree.add("rc", rc);
    
    我的完整输出如下所示

    //JMeter Engine
    StandardJMeterEngine jmeter = new StandardJMeterEngine();
    
    //JMeter initialization (properties, log levels, locale, etc)
    JMeterUtils.loadJMeterProperties("/path/to/apache-jmeter-4.0/bin/jmeter.properties");
    JMeterUtils.setJMeterHome("/path/to/apache-jmeter-4.0");
    JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
    JMeterUtils.initLocale();
    
    // JMeter Test Plan, basic all u JOrphan HashTree
    HashTree testPlanTree = new HashTree();
    
    // HTTP Sampler
    HTTPSampler httpSampler = new HTTPSampler();
    httpSampler.setDomain("example.com");
    httpSampler.setPort(80);
    httpSampler.setPath("/");
    httpSampler.setMethod("GET");
    
    // Loop Controller
    LoopController loopController = new LoopController();
    loopController.setLoops(1);
    loopController.addTestElement(httpSampler);
    loopController.setFirst(true);
    loopController.initialize();
    
    
    // Thread Group
    ThreadGroup threadGroup = new ThreadGroup();
    threadGroup.setNumThreads(1);
    threadGroup.setRampUp(1);
    threadGroup.setSamplerController(loopController);
    
    // Test Plan
    TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
    
    // Construct Test Plan from previously initialized elements
    testPlanTree.add("testPlan", testPlan);
    testPlanTree.add("loopController", loopController);
    testPlanTree.add("threadGroup", threadGroup);
    testPlanTree.add("httpSampler", httpSampler);
    
    // Run Test Plan
    jmeter.configure(testPlanTree);
    jmeter.run();
    
    ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'log4j2.debug' to show Log4j2 internal initialization logging.
    Apr 16, 2018 1:40:32 PM java.util.prefs.WindowsPreferences <init>
    WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5. summary =      0 in 00:00:00 = ******/s Avg:     0 Min: 9223372036854775807 Max: -9223372036854775808 Err:     0 (0.00%)`
    summary =      0 in 00:00:00 = ******/s Avg:     0 Min: 9223372036854775807 Max: -9223372036854775808 Err:     0 (0.00%)
    
    错误状态记录器未找到log4j2配置文件。使用默认配置:仅将错误记录到控制台。设置系统属性“log4j2.debug”以显示log4j2内部初始化日志。
    2018年4月16日下午1:40:32 java.util.prefs.WindowsPreferences
    警告:无法在根0x8000002处打开/创建prefs根节点Software\JavaSoft\prefs。Windows RegCreateKeyEx(…)返回错误代码5。汇总=0 in 00:00:00=******/s平均值:0 Min:9223372036854775807 Max:-9223372036854775808错误:0(0.00%)`
    汇总=0 in 00:00:00=******/s平均值:0 Min:9223372036854775807 Max:-9223372036854775808错误:0(0.00%)
    
    从代码中删除以下行:

    threadGroup.addTestElement(rc);
    testPlanTree.add("rc", rc);
    
    并将其替换为以下内容:

    testPlanTree.add(testPlanTree.getArray()[0], rc);
    
    还要注意,如果您离开此行:

    rc.setErrorLogging(true);
    
    在.jtl结果文件中只有错误

    我也没有看到任何执行的采样器,因此使用以下参考资料仔细检查您的测试计划:


    结果仍然相同。我实际上正在尝试运行javasampler,但我已经使用httpsampler进行了共享。我在这里没有正确使用HttpSampler吗?我已经用底部的完整输出更新了我的帖子。我想知道打印的错误或警告是否需要解决才能使jmeter正常工作?