我正在开发一个Java应用程序,它运行JMeter脚本,然后从报告中获取用于插入到数据库中的信息。JMeter脚本具有模块配置和JSR 233采样器,用于从外部Excel读取数据,以驱动测试和API调用。
如果我从JMeter GUI运行脚本,我没有问题,但如果我从Java代码运行脚本,我在执行用于读取Excel的JSR 233采样器期间遇到错误。
按照我的代码片段调用JMeter和来自Java代码的脚本:
package jmeter;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.report.dashboard.ReportGenerator;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import org.apache.poi.ss.usermodel.Sheet;
import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Properties;
public class OpenJMX {
public static String reportPath;
public static String getReportPath() {
return reportPath;
}
@SuppressWarnings("unused")
public static String RunJmeter(Properties myPropertiesFile) throws Exception {
try {
// Jmeter location
File jmeterHome = new File(System.getProperty("jmeter.home", myPropertiesFile.getProperty("JMETER_PROPERTIES_PATH")));
String slash = System.getProperty("file.separator");
// Ready to start JMX scenario location
File testPlan = new File(System.getProperty("testPlan.location", myPropertiesFile.getProperty("SCRIPT_PATH") + myPropertiesFile.getProperty("SCRIPT_NAME")));
if (jmeterHome.exists()) {
if (testPlan.exists()) {
File jmeterProperties = new File(jmeterHome.getPath() + slash + "bin" + slash + "jmeter.properties");
if (jmeterProperties.exists()) {
// JMeter Engine
StandardJMeterEngine jmeter = new StandardJMeterEngine();
// Initialize Properties, locale, etc.
JMeterUtils.setJMeterHome(jmeterHome.getPath());
System.setProperty("javax.net.ssl.keyStore", jmeterHome.getPath() + slash + "bin" + slash +"OracoloCertificate.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
JMeterUtils.loadJMeterProperties(jmeterProperties.getPath());
JMeterUtils.findClassesThatExtend(Sheet.class);
JMeterUtils.initLocale();
// Set directory for HTML report
String repDir = jmeterHome.getPath() + slash + "HTMLReport";
JMeterUtils.setProperty("jmeter.reportgenerator.exporter.html.property.output_dir", repDir);
// Initialize JMeter SaveService
SaveService.loadProperties();
// Load existing .jmx Test Plan
HashTree testPlanTree = SaveService.loadTree(testPlan);
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
if (summariserName.length() > 0) {
}
// Store execution results into a .csv file
File logFile = setReportPath(myPropertiesFile.getProperty("REPORT_PATH"), jmeterHome);
//Summiraser summer = null;
ResultCollector logger = new ResultCollector();
ReportGenerator reportGenerator = new ReportGenerator(logFile.getPath(), logger); //creating ReportGenerator for creating HTML report
logger.setFilename(logFile.getPath());
testPlanTree.add(testPlanTree.getArray()[0], logger);
// Run JMeter Test
jmeter.configure(testPlanTree);
jmeter.run();
}
}
}
String executionId = "OK";
return executionId;
}
catch(Exception e) {
String exc = ExceptionUtils.getStackTrace(e);
System.err.println(exc);
System.exit(-1);
return null;
}
}
private static File setReportPath(String reportPathProperties, File jmeterHome) {
try {
String slash = System.getProperty("file.separator");
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm");
LocalDateTime now = CurrentDateTime.getTimeNow();
File logFile;
if(reportPathProperties.isEmpty()) {
logFile = new File(jmeterHome + slash + "Report " + dtf.format(now) + ".jtl");
}
else {
logFile = new File(reportPathProperties + slash + "Report " + dtf.format(now) + ".jtl");
}
reportPath = logFile.getPath();
return logFile;
}
catch(Exception e) {
String exc = ExceptionUtils.getStackTrace(e);
System.err.println(exc);
System.exit(-1);
return null;
}
}
}
在JSR 233采样器中读取Excel所用的代码如下:
import org.apache.jmeter.threads.JMeterVariables;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
String foglio = "${foglio}";
String webService = "${webService}";
String testCase = "${testCase}";
String path = "${pathProjectFiles}" + "/DATA INPUTS.xlsx";
InputStream in = new FileInputStream(new File(path));
Workbook wb = new XSSFWorkbook(in);
in.close();
Sheet sheet = wb.getSheet(foglio);
int nrRows = sheet.getLastRowNum();
int nrColumns = sheet.getRow(0).getLastCellNum();
for (int i = 1; i <= nrRows; i++) {
if(sheet.getRow(i).getCell(0).getStringCellValue().equals(testCase)) {
for (int j = 1; j < nrColumns; j++) {
vars.put(webService + "_" + sheet.getRow(0).getCell(j).getStringCellValue(), sheet.getRow(i).getCell(j).getStringCellValue());
}
break;
}
}
我在Java应用程序执行中遇到的错误是:
javax.script.ScriptException: Sourced file: inline evaluation of: ``import org.apache.jmeter.threads.JMeterVariables; import org.apache.poi.ss.userm . . . '' : Typed variable declaration : Attempt to resolve method: getLastRowNum() on undefined variable or class name: sheet : at Line: 21 : in file: inline evaluation of: ``import org.apache.jmeter.threads.JMeterVariables; import org.apache.poi.ss.userm . . . '' : sheet .getLastRowNum ( )
in inline evaluation of: ``import org.apache.jmeter.threads.JMeterVariables; import org.apache.poi.ss.userm . . . '' at line number 21
at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:93) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:231) ~[java.scripting:na]
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:219) ~[ApacheJMeter_core-5.4.3.jar:5.4.3]
at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:72) ~[ApacheJMeter_java-5.4.3.jar:5.4.3]
at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:638) ~[ApacheJMeter_core-5.4.3.jar:5.4.3]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:558) ~[ApacheJMeter_core-5.4.3.jar:5.4.3]
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) ~[ApacheJMeter_core-5.4.3.jar:5.4.3]
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) ~[ApacheJMeter_core-5.4.3.jar:5.4.3]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
尽管库已正确导入,但似乎无法识别Sheet对象。
你知道这个错误吗?
感谢支持!
1条答案
按热度按时间x0fgdtte1#
我看不出你在哪里调用
getLastRowNum()
函数,所以你提供的代码与这个问题无关,检查你的其他脚本,确保你在那里正确地声明/示例化sheet
。还要注意,由于JMeter3.1you should be using Groovy for scripting,所以我建议将语言切换为
groovy
根据JSR 223采样器文档:
JSR 223测试元素有一个功能(编译),可以显著提高性能。要从该功能中获益,请执行以下操作:
使用脚本文件而不是内联它们。这将使JMeter编译它们(如果ScriptEngine上有此功能)并缓存它们。
或者使用脚本文本并选中缓存编译脚本(如果可用)属性。
使用此功能时,**请确保您的脚本代码不直接在脚本代码中使用JMeter变量或JMeter函数调用,因为缓存只会缓存第一个替换。**请使用脚本参数。
因此,您需要将
String foglio = "${foglio}";
更改为String foglio = vars.get("foglio");
,依此类推。