运行 HttpSampler 的 JMeter API 代码不起作用 [英] JMeter API Code to run HttpSampler doesnt work

查看:24
本文介绍了运行 HttpSampler 的 JMeter API 代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我根据我发现的一些示例编写的使用 JMeter API 运行 HTTPRequest 的类.

This is the class that I have written to run HTTPRequest using JMeter API based on some samples I found.

import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.engine.StandardJMeterEngine;
//import org.apache.jmeter.protocol.java.sampler.JUnitSampler;
import org.apache.jmeter.protocol.http.sampler.*;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.SetupThreadGroup;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;

public class Jmeter {


public static void main(String[] args){

    // Engine
    StandardJMeterEngine jm = new StandardJMeterEngine();
    // jmeter.properties
    JMeterUtils.loadJMeterProperties("D:/apache-jmeter-2.13/bin/jmeter.properties");

    HashTree hashTree = new HashTree();     

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

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

    // Thread Group
    SetupThreadGroup threadGroup = new SetupThreadGroup();
    threadGroup.setNumThreads(1);
    threadGroup.setRampUp(1);
    threadGroup.setSamplerController((LoopController)loopCtrl);

    // Test plan
    TestPlan testPlan = new TestPlan("MY TEST PLAN");

    hashTree.add("testPlan", testPlan);
    hashTree.add("threadGroup", threadGroup);
    hashTree.add("httpSampler", httpSampler);       

    jm.configure(hashTree);
    System.out.println(jm.isActive());
    jm.run();
}
}

我正在 Eclipse 中尝试相同的代码,但收到以下警告和错误.有人能帮我解决吗?

I'm trying the same code in Eclipse but I'm getting the following warnings and errors. Can anyone help me resolve it?

提前致谢!

根据您的建议.我已经用循环控制器更新了代码.请在下面找到 JMeter 日志和 Eclipse 输出.

Based on your suggestion. I have updated the code with loop controller. Please find the JMeter Log and Eclipse Output below.

JMeter 日志

2015/07/22 11:05:18 INFO  - jmeter.util.JMeterUtils: Setting Locale to en_US 
2015/07/22 11:05:18 INFO  - jmeter.protocol.http.sampler.HTTPSamplerBase: Cannot find .className property for htmlParser, using default 
2015/07/22 11:05:18 INFO  - jmeter.protocol.http.sampler.HTTPSamplerBase: Parser for text/html is  
2015/07/22 11:05:18 INFO  - jmeter.protocol.http.sampler.HTTPSamplerBase: Parser for application/xhtml+xml is  
2015/07/22 11:05:18 INFO  - jmeter.protocol.http.sampler.HTTPSamplerBase: Parser for application/xml is  
2015/07/22 11:05:18 INFO  - jmeter.protocol.http.sampler.HTTPSamplerBase: Parser for text/xml is  
2015/07/22 11:05:18 INFO  - jmeter.protocol.http.sampler.HTTPSamplerBase: Parser for text/vnd.wap.wml is   org.apache.jmeter.protocol.http.parser.RegexpHTMLParser 
2015/07/22 11:05:18 INFO  - jmeter.protocol.http.sampler.HTTPJavaImpl: Maximum connection retries = 10 
2015/07/22 11:05:18 INFO  - jmeter.engine.StandardJMeterEngine: Running the test! 
2015/07/22 11:05:18 INFO  - jmeter.samplers.SampleEvent: List of sample_variables: [] 
2015/07/22 11:05:18 INFO  - jmeter.samplers.SampleEvent: List of sample_variables: [] 
2015/07/22 11:05:18 INFO  - jmeter.engine.util.CompoundVariable: Note: Function class names must contain the string: '.functions.' 
2015/07/22 11:05:18 INFO  - jmeter.engine.util.CompoundVariable: Note: Function class names must not contain the string: '.gui.' 
2015/07/22 11:05:18 WARN  - jmeter.engine.util.CompoundVariable: Did not find any functions 
2015/07/22 11:05:18 INFO  - jmeter.engine.StandardJMeterEngine: Starting ThreadGroup: 1 :  
2015/07/22 11:05:18 INFO  - jmeter.engine.StandardJMeterEngine: Starting 1 threads for group . 
2015/07/22 11:05:18 INFO  - jmeter.engine.StandardJMeterEngine: Thread will continue on error 
2015/07/22 11:05:18 INFO  - jmeter.threads.ThreadGroup: Starting thread group number 1 threads 1 ramp-up 1 perThread 1000.0 delayedStart=false 
2015/07/22 11:05:18 INFO  - jmeter.threads.JMeterThread: jmeterthread.startearlier=true (see jmeter.properties) 
2015/07/22 11:05:18 INFO  - jmeter.threads.JMeterThread: Running PostProcessors in forward order 
2015/07/22 11:05:18 INFO  - jmeter.threads.ThreadGroup: Started thread group number 1 
2015/07/22 11:05:18 INFO  - jmeter.engine.StandardJMeterEngine: All thread groups have been started 
2015/07/22 11:05:18 INFO  - jmeter.threads.JMeterThread: Thread started:  1-1 
2015/07/22 11:05:18 INFO  - jmeter.threads.JMeterThread: Thread is done:  1-1 
2015/07/22 11:05:18 INFO  - jmeter.threads.JMeterThread: Thread finished:  1-1 
2015/07/22 11:05:18 INFO  - jmeter.engine.StandardJMeterEngine: Notifying test listeners of end of test 
2015/07/22 11:05:18 INFO  - jmeter.services.FileServer: Default base='D:\JMeter_Performance\JmeterSeleniumTest' 

Eclipse 输出

WARN    2015-07-22 11:05:18.374 [jmeter.u] (): Unexpected value set for boolean property:'server.exitaftertest', defaulting to:false
WARN    2015-07-22 11:05:18.407 [jmeter.u] (): Unexpected value set for boolean property:'jmeterengine.startlistenerslater', defaulting to:true
INFO    2015-07-22 11:05:18.407 [jmeter.e] (): Listeners will be started after enabling running version
INFO    2015-07-22 11:05:18.407 [jmeter.e] (): To revert to the earlier behaviour, define jmeterengine.startlistenerslater=false
WARN    2015-07-22 11:05:18.407 [jmeter.u] (): Unexpected value set for boolean property:'jmeterengine.remote.system.exit', defaulting to:false
WARN    2015-07-22 11:05:18.407 [jmeter.u] (): Unexpected value set for boolean property:'jmeterengine.stopfail.system.exit', defaulting to:true
WARN    2015-07-22 11:05:18.408 [jmeter.u] (): Unexpected value set for boolean property:'jmeterengine.force.system.exit', defaulting to:false
Exception in thread "main" java.lang.NoClassDefFoundError: com/thoughtworks/xstream/converters/ConversionException
at org.apache.jmeter.engine.StandardJMeterEngine.run(StandardJMeterEngine.java:450)
at JmeterClass.JMeterFromScratch.main(JMeterFromScratch.java:56)
Caused by: java.lang.ClassNotFoundException: com.thoughtworks.xstream.converters.ConversionException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more

请帮我解决这个问题.谢谢!

Please help me resolve this issue.Thanks!

推荐答案

我相信你需要添加以下行:

I believe that you need to add the following line:

hashTree.add("loopController", loopCtrl);

before jm.configure(hashTree);

如果它没有帮助 - 用 Eclipse 输出和 jmeter.log 文件内容更新你的问题(应该出现在你的项目根目录中)

If it doesn't help - update your question with Eclipse output and jmeter.log file contents (should appear in your project root)

请参阅以下材料以供参考:

See the following material for reference:

  • 5 种无需使用 JMeter GUI 即可启动 JMeter 测试的方法
  • 示例项目演示使用 Java API 创建 JMeter 测试
    • 生成 .jtl 结果文件
    • 产生汇总器输出
    • 生成可以在 JMeter 中打开的 .jmx 文件
    • "Creating a brand new JMeter test purely in Java" chapter of the 5 Ways To Launch a JMeter Test without Using the JMeter GUI
    • Sample project demonstrating creation of JMeter test using Java API which
      • produces .jtl result file
      • produces summarizer output
      • generates .jmx file which can be opened in JMeter

      这篇关于运行 HttpSampler 的 JMeter API 代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆