3

I am using Jmeter 5.0 where i have piece of java code written inside a JSR223 PostProcessor. The code is as follows -

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

However when i execute the test plan, in the console i get the following error -

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

Can someone help me in pointing where i might have gone wrong ?

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
Aritro Sen
  • 357
  • 7
  • 14

3 Answers3

3

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

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

It seems that GameplayHistoryId is empty, in such case add default value in JSONExtractor or fail test

See JMeter's best practices for JSR223 scripting:

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

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • Don't use ${} in JSR223 scripts, use instead vars.get("") to get varibles - why ? – Aritro Sen Mar 04 '19 at 07:06
  • @AritroSen see JMeter's best practices for JSR223: https://jmeter.apache.org/usermanual/best-practices.html#jsr223 – Ori Marko Mar 04 '19 at 07:10
  • Okay. 1 More Question - I have like 3 JsonExtractor in a single thread. How do i use the value extracted in the first to the third ? – Aritro Sen Mar 04 '19 at 07:11
  • @AritroSen Please ask a new question with relevant details and example – Ori Marko Mar 04 '19 at 07:13
  • I understood the JSR223 scripting part. How do i use the props to extract the values of this map in my subsequent thread - I have coded like this - import java.util.Map; import java.util.HashMap; Map gameTemplateIdMap = props.get("GamePlayHistoryMap"); Map payLevelDetailsMap = props.get("PayLevelDetailsMap"); is this correct way ? – Aritro Sen Mar 04 '19 at 07:27
  • @AritroSen for supporting different thread you will have to update JMeter property as `props.put`, but you should ask another specific question for specific answer – Ori Marko Mar 04 '19 at 07:31
1
  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 and you have to stick to Java 5 syntax.
  2. Don't inline 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 to read existing variables values and create new ones, i.e. replace this line:

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

    with this one:

    gamePlayHistoryMap.put(vars.get('playerId'), vars.get('GameplayHistoryId'));
    
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

You are missing the Map key/value definition.

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

Not sure about the answer of:

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

not sure it has anything to do with it.

Leon Proskurov
  • 135
  • 1
  • 14