简体   繁体   English

从 JSR223 断言读取响应到 Java 代码

[英]Read response from JSR223 Assertion to Java Code

I want to read processed value from Java code.我想从 Java 代码中读取处理后的值。 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.我的 jmx 中有一个 jsr233 断言,它处理了值 ({"timestamp": 23893865465, "diff": 34}) 我想在我的 JMeterEngine 实例中调用 run() 命令后在我的 java 代码中读取这个。

I tried using SamplerListner but not working.我尝试使用 SamplerListner 但不工作。 how I can read that response.我如何阅读该回复。

JSR 233 Assertion Code: JSR 233 断言代码:

 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 Java码

 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.setProperty() function 将值写入 JSR223 断言中的 Java 系统属性。

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();整个 JVM 的系统属性是全局的,因此您可以在调用jmeterSecondary.run();后使用System.getProperty() function 访问它;

Example usage:示例用法:

在此处输入图像描述

More information on different JMeter Properties types and ways of working with them: Apache JMeter Properties Customization Guide有关不同 JMeter 属性类型和使用它们的方式的更多信息: Apache JMeter 属性自定义指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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