Jmeter functions don't executing when calling

2020-05-09 23:08发布

问题:

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)}

<httpSample t="663" it="0" lt="631" ts="1492989803759" s="true" lb="User-getapikey   ${__time(yyyy-MM-dd HH:mm:ss)} " rc="200" rm="" tn="Thread Group 1-1" dt="text" de="UTF-8" by="528" ng="1" na="1">

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();

回答1:

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.