JSR223 脚本、JSR223 后处理器中的问题:javax.script.ScriptException

Problem in JSR223 script, JSR223 PostProcessor : javax.script.ScriptException

我正在使用 Jmeter 5.0,我在 JSR223 后处理器中编写了一段 java 代码。代码如下-

import java.util.Map;
import java.util.HashMap;


Map gamePlayHistoryMap = new HashMap();
gamePlayHistoryMap.put(${playerId}, ${GameplayHistoryId});
props.put("GamePlayHistoryMap", gamePlayHistoryMap);

Map payLevelDetailsMap = new HashMap();
payLevelDetailsMap.put(${playerId}, ${PayLevelDetails});
props.put("PayLevelDetailsMap", payLevelDetailsMap);

然而,当我执行测试计划时,在控制台中出现以下错误 -

javax.script.ScriptException: In file: inline evaluation of: import java.util.Map; import java.util.HashMap; Map gamePlayHistoryMap = new H . . . '' Encountered "( 107 , )" at line 6, column 23. in inline evaluation of:import java.util.Map; import java.util.HashMap; Map gamePlayHistoryMap = new H . . . '' at line number 6

谁能帮我指出我可能哪里出错了?

不要在 JSR223 脚本中使用 ${},而是使用 vars.get("") 来获取变量

gamePlayHistoryMap.put(vars.get("playerId"), vars.get("GameplayHistoryId"));

似乎GameplayHistoryId是空的,在这种情况下在JSONExtractor中添加默认值或失败测试

查看 JMeter 的 JSR223 脚本最佳实践:

In this case, ensure the script does not use any variable using ${varName} as caching would take only first value of ${varName}. Instead use : vars.get("varName")

  1. Since JMeter 3.1 you should be using groovy language for scripting, looking into your exception details it appears you're using java which is not real Java, it's Beanshell interpreter which has worse performance comparing to Groovy 并且您必须坚持使用 Java 5 语法。
  2. 不要内联 JMeter Functions and/or Variables into scripts as they might be resolved into something causing script failures and in case of Groovy they conflict with GString templates and compilation caching feature. Use vars shorthand for JMeterVariables class 以读取现有变量值并创建新值,即替换此行:

    gamePlayHistoryMap.put(${playerId}, ${GameplayHistoryId});
    

    这个:

    gamePlayHistoryMap.put(vars.get('playerId'), vars.get('GameplayHistoryId'));
    

您缺少地图 key/value 定义。

Map <String, String> gamePlayHistoryMap = new HashMap<>();
gamePlayHistoryMap.put(${playerId}, ${GameplayHistoryId});

不确定以下答案:

Don't use ${} in JSR223 scripts, use instead vars.get("")

不确定这与它有什么关系。