I'm using some jmeter functions in my jmx file.
When I run it from command line with jmeter tool, its running normally, executing the function and return a value.
When I run the java code , which load the same jmx file its taking a function as a string and don't return a value.
I have tested with several functions, Results are the same.
In jtl file I see something like this- ${__time(yyyy-MM-dd HH:mm:ss)}
Its my java code, its working normally when I don't have a functions:
StandardJMeterEngine jmeter = new StandardJMeterEngine();
JMeterUtils.loadJMeterProperties(jmeterPropertiesPath);
JMeterUtils.setJMeterHome(jmeterHomePath);
JMeterUtils.initLogging();
JMeterUtils.initLocale();
SaveService.loadProperties();
HashTree testPlanTree = null;
try{
log.info("loading testPlanTree");
testPlanTree = SaveService.loadTree(jmxFile);
JMeter.convertSubTree( testPlanTree );
}catch(IOException ex){
log.info("error loading file as tree");
log.info(ex.getMessage());
return;
}
jmeter.configure(testPlanTree);
jmeter.run();
解决方案
Your code looks good, just double check you have ApacheJMeter_functions.jar in your project classpath. The file is located under "lib/ext" folder of your JMeter installation.
You might also want to add ResultCollector to store your test execution results into .jtl file like:
Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}
ResultCollector logger = new ResultCollector(summer);
logger.setFilename("result.jtl");
testPlanTree.add(testPlanTree.getArray()[0], logger);
Check out Five Ways To Launch a JMeter Test without Using the JMeter GUI for more information on different approaches to JMeter test execution including using Java API to kick off and develop tests.