jmeter 在后处理器中处理空值时出错

qfe3c7zg  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(63)

我从foreach控制器得到以下三个响应数据,
回应1:

{
    "studies": [
        {
            "last_access": "2022-08-24 21:49:04.181343-07",
            "phi_namespace_name": "All Sites",
            "uuid": "0fcd22ae-87a2-48dc-acd9-8814c0e226a0",
            "primary_created": "2022-08-24 21:48:52.166124-07",
            "study_uid": "1.2.826.0.1.3680043.2.1329.1.1.38.18413",
            "created": "2022-08-24 21:49:04.181343-07",
            "created_at": "2022-08-24 21:49:04.181343-07",
            "account_name": "D_Anonymization",
            "patient_name": "350129",
            "first_name": "350129",
            "created_by_mobile_phone": "+19999999999",
            "created_for_type": "User"
        }
    ],
    "page": {
        "number": 1,
        "manual_limit": 1,
        "more": 0,
        "rows": 100
    },
    "status": "OK"
}

回应二:

{
    "studies": [
    ],
    "page": {
        "number": 1,
        "manual_limit": 1,
        "more": 0,
        "rows": 100
    },
    "status": "OK"
}

我使用JSON提取器获取帐户名的UUID,在本例中为$.studies[?(@.account_name=='${studyNameOfTheAccount}')].uuid$.studies[?(@.account_name=='D_Anonymization')].uuid,并使用后处理器将其传递给另一个控制器。

对于前两个,它工作正常,但对于最后一个,它失败了,因为没有UUID。
后处理器:用香草盒子试过

import groovy.json.JsonSlurper;

def imagesWithStudy = [];
    1. upto(vars.get('studiesWithImages_matchNr') as int, {
        indexImage ->
            imagesWithStudy.add(vars.get('studiesWithImages_' + indexImage));
    });

vars.putObject("completeStudiesWithImagesList", completeStudiesWithImages);
log.info('Generated array for study with images: ' + imagesWithStudy);

已尝试使用if-else

import groovy.json.JsonSlurper;

def imagesWithStudy = [];
    1. upto(vars.get('studiesWithImages_matchNr') as int, {
        indexImage ->
        //if (indexImage == "NO IMAGES WITH STUDY") {
        //if (indexImage == null ) {
        if (indexImage == 0 ) {
            log.info('THERE ARE NO IMAGES WITH THIS !!!');
        }
        else {
            log.info('There are images with this  !!!!!');
            imagesWithStudy.add(vars.get('studiesWithImages_' + indexImage));
        }
    });

vars.putObject("completeStudiesWithImagesList", completeStudiesWithImages);
log.info('Generated array for study with images: ' + imagesWithStudy);

我得到下面的错误,

2023-06-22 11:59:57,805 ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, Process_UUID
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.GroovyRuntimeException: The argument (0) to upto() cannot be less than the value (1) it's called on.
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:158) ~[groovy-jsr223-3.0.11.jar:3.0.11]
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:231) ~[java.scripting:?]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:219) ~[ApacheJMeter_core.jar:5.5]
    at org.apache.jmeter.extractor.JSR223PostProcessor.process(JSR223PostProcessor.java:45) ~[ApacheJMeter_components.jar:5.5]
    at org.apache.jmeter.threads.JMeterThread.runPostProcessors(JMeterThread.java:968) ~[ApacheJMeter_core.jar:5.5]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:585) ~[ApacheJMeter_core.jar:5.5]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:501) ~[ApacheJMeter_core.jar:5.5]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) ~[ApacheJMeter_core.jar:5.5]
eoigrqb6

eoigrqb61#

发生错误的原因是,如果缺少“研究”,则studiesWithImages_matchNr变量的值变为0
从Groovy的Angular 来看,1.upto(0)结构是无效的,就像documentation一样
从这个数字开始迭代到给定的数字,每次递增1。
我认为你需要替换这个:

1.upto(vars.get('studiesWithImages_matchNr') as int, {
    indexImage ->
        imagesWithStudy.add(vars.get('studiesWithImages_' + indexImage));
});

用这个:

vars.entrySet().findAll { it.getKey().matches('studiesWithImages_\\d+') }.each { imagesWithStudy.add(it.getValue()) }

此外:

  • 这一行:import groovy.json.JsonSlurper;是不需要的,因为JsonSlurper没有在任何地方使用
  • 这行:vars.putObject("completeStudiesWithImagesList", completeStudiesWithImages);将导致错误,因为completeStudiesWithImages对象未在任何地方定义

有关JMeter中Groovy脚本的更多信息:Apache Groovy: What Is Groovy Used For?

相关问题