简体   繁体   中英

Read response from JSR223 Assertion to Java Code

I want to read processed value from Java code. I have a jsr233 assertion in my jmx which processed values ({"timestamp": 23893865465, "diff": 34}) I want to read this in my java code after invoking run() command in my JMeterEngine instance.

I tried using SamplerListner but not working. how I can read that response.

JSR 233 Assertion Code:

 mport groovy.json.JsonBuilder

String totalIDs = vars.get("assetIdCount_matchNr");
Integer result = Integer.valueOf(totalIDs);
Date currentDate = new Date();
if (result > 10){
    vars.put("SEARCH_SUCCESS_TIMESTAMP",currentDate.getTime().toString());
    log.info("ASSET_CREATION_TIMESTAMP:" + props.get("ASSET_CREATION_TIMESTAMP"));
    log.info("ASSET_CREATION_TIMESTAMPAfter:" + props.get("ASSET_CREATION_TIMESTAMP"));
    log.info("SEARCH_SUCCESS_TIMESTAMP:" + vars.get("SEARCH_SUCCESS_TIMESTAMP"));
    def diff = Long.valueOf(vars.get("SEARCH_SUCCESS_TIMESTAMP").toString()) - Long.valueOf(props.get("ASSET_CREATION_TIMESTAMP").toString());
    def timestamp = vars.get("SEARCH_SUCCESS_TIMESTAMP");
    vars.putObject("Diff", diff);
    log.info(String.valueOf(diff));

    def json = new JsonBuilder()
    def root = json timestamp: "5241234134134", diff: "345"

    def file  = new File(vars.get("file_location").toString())
    file.write('\n'+json.toString());
    sleep 10

    AssertionResult.setFailure(true);
}

Java code

 StandardJMeterEngine jmeterSecondary = configJmeter(jmxFileName, argumentObject, jmeter, jMeterAssetECListnerService); //get the jmeter to run by configuring it

                jmeterSecondary.run();
              // I want to get the timestamp and diff here
                jmeterSecondary.reset();
                jmeterSecondary.exit();

Thanks in advance!

The easiest option I can think of is using System.setProperty() function to write the value into a Java System Property in the JSR223 Assertion.

System properties are global for the whole JVM therefore you will be able to access it using System.getProperty() function after you call jmeterSecondary.run();

Example usage:

在此处输入图像描述

More information on different JMeter Properties types and ways of working with them: Apache JMeter Properties Customization Guide

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM