File jmeterHome=new File(system.getproperty(“jmeter.home”));String slash=System.GetProperty(“file.separator”);
if (jmeterHome.exists()) {
File jmeterProperties = new File(jmeterHome.getPath() + slash + "bin" + slash + "jmeter.properties");
if (jmeterProperties.exists()) {
//JMeter Engine
StandardJMeterEngine jmeter = new StandardJMeterEngine();
//JMeter initialization (properties, log levels, locale, etc)
JMeterUtils.setJMeterHome(jmeterHome.getPath());
JMeterUtils.loadJMeterProperties(jmeterProperties.getPath());
JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
JMeterUtils.initLocale();
SaveService.loadProperties();
// JMeter Test Plan, basically JOrphan HashTree
HashTree testPlanTree = SaveService.loadTree(new File(JMX FILE PATH))
// Run Test Plan
jmeter.configure(testPlanTree);
jmeter.run();
}
}
如果你想“创建”一个测试计划,你真的应该“创建”它,听起来就像你想从在线资源复制和粘贴一个现成的解决方案,或者请别人为你写脚本。
我希望您展示您的代码并突出有问题的地方,而不是要求实现测试计划。
随便啦。
JSR223Sampler jsr223Sampler = new JSR223Sampler();
jsr223Sampler.setName("JSR223 Sampler");
jsr223Sampler.setProperty("cacheKey", "true");
jsr223Sampler.setProperty("script", "println('Hello')");
jsr223Sampler.setProperty("scriptLanguage", "groovy");
jsr223Sampler.setProperty(TestElement.TEST_CLASS, JSR223Sampler.class.getName());
jsr223Sampler.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.gui.LoopControlPanel;
import org.apache.jmeter.control.gui.TestPlanGui;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.java.sampler.JSR223Sampler;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testbeans.gui.TestBeanGUI;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import java.io.File;
import java.io.FileOutputStream;
public class JMeterFromScratch {
public static void main(String[] argv) throws Exception {
File jmeterHome = new File(System.getProperty("jmeter.home"));
String slash = System.getProperty("file.separator");
if (jmeterHome.exists()) {
File jmeterProperties = new File(jmeterHome.getPath() + slash + "bin" + slash + "jmeter.properties");
if (jmeterProperties.exists()) {
//JMeter Engine
StandardJMeterEngine jmeter = new StandardJMeterEngine();
//JMeter initialization (properties, log levels, locale, etc)
JMeterUtils.setJMeterHome(jmeterHome.getPath());
JMeterUtils.loadJMeterProperties(jmeterProperties.getPath());
JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
JMeterUtils.initLocale();
// JMeter Test Plan, basically JOrphan HashTree
HashTree testPlanTree = new HashTree();
// JSR223 Sampler
JSR223Sampler jsr223Sampler = new JSR223Sampler();
jsr223Sampler.setName("JSR223 Sampler");
jsr223Sampler.setProperty("cacheKey", "true");
jsr223Sampler.setProperty("script", "println('Hello')");
jsr223Sampler.setProperty("scriptLanguage", "groovy");
jsr223Sampler.setProperty(TestElement.TEST_CLASS, JSR223Sampler.class.getName());
jsr223Sampler.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());
// Loop Controller
LoopController loopController = new LoopController();
loopController.setLoops(1);
loopController.setFirst(true);
loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
loopController.initialize();
// Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Example Thread Group");
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopController);
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
// Test Plan
TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());
// Construct Test Plan from previously initialized elements
testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(jsr223Sampler);
// save generated test plan to JMeter's .jmx file format
SaveService.saveTree(testPlanTree, new FileOutputStream(jmeterHome + slash + "example.jmx"));
//add Summarizer output to get test progress in stdout like:
// summary = 2 in 1.3s = 1.5/s Avg: 631 Min: 290 Max: 973 Err: 0 (0.00%)
Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}
// Store execution results into a .jtl file
String logFile = jmeterHome + slash + "example.jtl";
ResultCollector logger = new ResultCollector(summer);
logger.setFilename(logFile);
testPlanTree.add(testPlanTree.getArray()[0], logger);
// Run Test Plan
jmeter.configure(testPlanTree);
jmeter.run();
System.out.println("Test completed. See " + jmeterHome + slash + "example.jtl file for results");
System.out.println("JMeter .jmx script is available at " + jmeterHome + slash + "example.jmx");
System.exit(0);
}
}
System.err.println("jmeter.home property is not set or pointing to incorrect location");
System.exit(1);
}
}
我试图使用JSR223采样器模拟并行ajax请求,如这里提到的https://www.blazemeter.com/blog/how-load-test-ajaxxhr-enabled-sites-jmeter ,我在测试计划中启用了cookie管理器。 任何关于我做错什么的帮助都是很好的。
我正在找到在JSR223采样器、JSR223预处理程序、JSR223后处理程序和所有其他JSR223脚本中使用变量的方法。 还有一些其他变量,如、、、。我可以很容易地使用它们。 :vars.get(“variable_name”)、vars.put(“variable_name”、“value”)、... :props.get,props.put,... :prev.gettime()、prev.
我曾经用过 在BSF采样器中。现在我使用带有groovy的JSR223采样器,出现错误 响应消息:javax.script.ScriptException:org.codehaus.groovy.control.MultipleCompilationErrorsException:启动失败: Script2.groovy: 10:无法解析类Packages.ml.bh.security.CUplo
我有下面的JSR223采样器,它读取图像,稍微修改它,并发送一个POST multipart/form-data请求。与HTTP采样器相比,我发现它广泛使用了CPU,但我不能使用HTTP采样器,因为它不支持在不保存到文件系统的情况下更改映像。 如果任何人有任何输入来优化JSR223采样器中的脚本,这样它就不会占用大量的CPU,我将很感激。
根据答案,应该将velocity添加到JMeter类路径中,以便发现可用的脚本引擎。 如何在JMeter中添加velocity作为脚本引擎?
对于需要在jmeter上进行必要更改的任何帮助,我们都非常感谢