如何从Java代码运行JMeter测试用例?
我遵循了这里来自Blazemeter.com的例子
我的代码如下:
public class BasicSampler {
public static void main(String[] argv) throws Exception {
// JMeter Engine
StandardJMeterEngine jmeter = new StandardJMeterEngine();
// Initialize Properties, logging, locale, etc.
JMeterUtils.loadJMeterProperties("/home/stone/Workbench/automated-testing/apache-jmeter-2.11/bin/jmeter.properties");
JMeterUtils.setJMeterHome("/home/stone/Workbench/automated-testing/apache-jmeter-2.11");
JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
JMeterUtils.initLocale();
// Initialize JMeter SaveService
SaveService.loadProperties();
// Load existing .jmx Test Plan
FileInputStream in = new FileInputStream("/home/stone/Workbench/automated-testing/apache-jmeter-2.11/bin/examples/CSVSample.jmx");
HashTree testPlanTree = SaveService.loadTree(in);
in.close();
// Run JMeter Test
jmeter.configure(testPlanTree);
jmeter.run();
}}
但是我一直在控制台中得到以下消息,而且我的测试永远不会执行。
信息2014-09-23 12:04:40.492 jmeter.e:侦听器将在启用运行版本INFO 2014-09-23 12:04:40.511后启动,jmeter.e:要恢复到早期行为,请定义jmeterengine.startlistenerslater=false
我还尝试了来自jmeter.properties文件的未注释的jmeter.properties文件
发布于 2014-09-23 15:13:32
jmeter.log文件中的内容(应该在项目的根目录中)。或者注释JMeterUtils.initLogging()行,以查看STDOUT中的完整输出CSVSample_user.csv中更改了相对路径使用CSV数据集配置,因为它可能会按照使用CSV数据集配置中的建议将其解析为不同的位置。CSVSample.jtl文件是否在任何地方生成(默认情况下,它应该位于项目的根中)?里面有什么?代码看起来很好,我很确定问题在于CSVSample_user.csv文件的路径,日志中有类似于java.io.FileNotFoundException的内容。请再次检查CSVSample.jmx文件是否包含有效的完整路径到CSVSample_user.csv。
更新以回答注释中的问题
jmeter.log文件应该位于您的Eclipse工作区文件夹下CSVSample.jmx,有一个View Resulst in Table侦听器,它被配置为在~/CSVSample.jtl下存储结果jmeter.configure(testPlanTree);节之前添加以下几行
概括器summariserName = null;字符串JMeterUtils.getPropDefault= JMeterUtils.getPropDefault("summariser.name",“汇总”);if (summariserName.length() > 0) {summariser.name=新摘要器(SummariserName);} String logFile =“/path/to/jtl/JMeterUtils.getPropDefault/file.jtl”;ResultCollector记录器=新ResultCollector(夏季);logger.setFilename(logFile);testPlanTree.add(testPlanTree.getArray(),记录器);发布于 2021-01-22 05:26:02
尝试使用库- https://github.com/abstracta/jmeter-java-dsl。
它支持将JMeter测试实现为java代码。
下面的示例显示了如何为REST实现和执行测试。同样的方法也适用于其他类型的测试。
@Test
public void testPerformance() throws IOException {
TestPlanStats stats = testPlan(
threadGroup(2, 10,
httpSampler("http://my.service")
.post("{\"name\": \"test\"}", Type.APPLICATION_JSON)
),
//this is just to log details of each request stats
jtlWriter("test" + Instant.now().toString().replace(":", "-") + ".jtl")
).run();
assertThat(stats.overall().elapsedTimePercentile99()).isLessThan(Duration.ofSeconds(5));
}https://stackoverflow.com/questions/25993210
复制相似问题