Jmeter没有运行HttpSamplerProxy。

huangapple go评论120阅读模式
英文:

Jmeter is not running the HttpSamplerProxy

问题

  1. StandardJMeterEngine jmeter = new StandardJMeterEngine();
  2. // JMeter initialization (properties, log levels, locale, etc)
  3. JMeterUtils.loadJMeterProperties(System.getenv("JMETER_HOME") + "/bin/jmeter.properties");
  4. JMeterUtils.initLogging(); // you can comment this line out to see extra log messages of i.e. DEBUG level
  5. JMeterUtils.initLocale();
  6. // JMeter Test Plan, basic structure using JOrphan HashTree
  7. HashTree testPlanTree = new HashTree();
  8. // HTTP Sampler
  9. HTTPSampler httpSampler = new HTTPSampler();
  10. httpSampler.setDomain("example.com");
  11. httpSampler.setPort(80);
  12. httpSampler.setPath("/");
  13. httpSampler.setMethod("GET");
  14. // Loop Controller
  15. LoopController loopController = new LoopController();
  16. loopController.setLoops(1);
  17. loopController.addTestElement(httpSampler);
  18. loopController.setFirst(true);
  19. loopController.initialize();
  20. // Thread Group
  21. ThreadGroup threadGroup = new ThreadGroup();
  22. threadGroup.setNumThreads(1);
  23. threadGroup.setRampUp(1);
  24. threadGroup.setSamplerController(loopController);
  25. // Test Plan
  26. TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
  27. // Construct Test Plan using previously initialized elements
  28. testPlanTree.add("testPlan", testPlan);
  29. testPlanTree.add("loopController", loopController);
  30. testPlanTree.add("threadGroup", threadGroup);
  31. testPlanTree.add("httpSampler", httpSampler);
  32. // Run Test Plan
  33. jmeter.configure(testPlanTree);
  34. jmeter.run();
英文:

I am running this with jmeter core and jmeter http both at version 5.3. I am confused to why the sampler will not fire off. I am new to using jmeter in this fashion and am not quite sure how this all works. When the code is ran i end up with no request data in the jtl file.

  1. //JMeter initialization (properties, log levels, locale, etc)
  2. JMeterUtils.loadJMeterProperties(System.getenv("JMETER_HOME") + "/bin/jmeter.properties");
  3. JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
  4. JMeterUtils.initLocale();
  5. // JMeter Test Plan, basic all u JOrphan HashTree
  6. HashTree testPlanTree = new HashTree();
  7. // HTTP Sampler
  8. HTTPSampler httpSampler = new HTTPSampler();
  9. httpSampler.setDomain("example.com");
  10. httpSampler.setPort(80);
  11. httpSampler.setPath("/");
  12. httpSampler.setMethod("GET");
  13. // Loop Controller
  14. LoopController loopController = new LoopController();
  15. loopController.setLoops(1);
  16. loopController.addTestElement(httpSampler);
  17. loopController.setFirst(true);
  18. loopController.initialize();
  19. // Thread Group
  20. ThreadGroup threadGroup = new ThreadGroup();
  21. threadGroup.setNumThreads(1);
  22. threadGroup.setRampUp(1);
  23. threadGroup.setSamplerController(loopController);
  24. // Test Plan
  25. TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
  26. // Construct Test Plan from previously initialized elements
  27. testPlanTree.add("testPlan", testPlan);
  28. testPlanTree.add("loopController", loopController);
  29. testPlanTree.add("threadGroup", threadGroup);
  30. testPlanTree.add("httpSampler", httpSampler);
  31. // Run Test Plan
  32. jmeter.configure(testPlanTree);
  33. jmeter.run();

答案1

得分: 5

首先,这段代码:

  1. testPlanTree.add("testPlan", testPlan);
  2. testPlanTree.add("loopController", loopController);
  3. testPlanTree.add("threadGroup", threadGroup);
  4. testPlanTree.add("httpSampler", httpSampler);

需要被替换为:

  1. testPlanTree.add(testPlan);
  2. HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
  3. threadGroupHashTree.add(httpSampler);

另外,我也没有看到你在将结果保存到 .jtl 文件中,你需要像这样添加 ResultCollector

  1. Summariser summer = null;
  2. String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
  3. if (summariserName.length() > 0) {
  4. summer = new Summariser(summariserName);
  5. }
  6. String logFile = "/path/to/result.jtl";
  7. ResultCollector logger = new ResultCollector(summer);
  8. logger.setFilename(logFile);
  9. testPlanTree.add(testPlanTree.getArray()[0], logger);

jmeter.configure(testPlanTree); 行之前。

总的来说,使用 API 创建 JMeter 测试并不是得到很好支持的方式,也不能保证代码在未来的 JMeter 版本中能正常工作,因此我更建议使用第三方选项,比如 Taurus 或者 jmeter-java-dsl

然而,如果出于某种原因你想继续,可以在 Five Ways To Launch a JMeter Test without Using the JMeter GUI 文章和 jmeter-from-code 仓库 中找到一些示例。

英文:

First of all, this code:

  1. testPlanTree.add("testPlan", testPlan);
  2. testPlanTree.add("loopController", loopController);
  3. testPlanTree.add("threadGroup", threadGroup);
  4. testPlanTree.add("httpSampler", httpSampler);

needs to be replaced by

  1. testPlanTree.add(testPlan);
  2. HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
  3. threadGroupHashTree.add(httpSampler);

I also fail to see where you're saving the results into the .jtl, you need to add ResultCollector like this:

  1. Summariser summer = null;
  2. String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
  3. if (summariserName.length() > 0) {
  4. summer = new Summariser(summariserName);
  5. }
  6. String logFile = "/path/to/result.jtl";
  7. ResultCollector logger = new ResultCollector(summer);
  8. logger.setFilename(logFile);
  9. testPlanTree.add(testPlanTree.getArray()[0], logger);

before jmeter.configure(testPlanTree); line

In general creating JMeter tests using API is not very supported and there is no any guarantee that the code will work after next JMeter release so I would rather rely on 3rd-party options like Taurus or jmeter-java-dsl

However if for some reason you want to continue you can get some examples in Five Ways To Launch a JMeter Test without Using the JMeter GUI article and in jmeter-from-code repo

huangapple
  • 本文由 发表于 2020年8月27日 00:27:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63601847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定