how to pass filewriter object between beanshell processor - jmeter

I am using beanshell processor in jmeter where i am defining FileWriter object in one beanshell processor and passing the object(fstream) to another beanshell processor.
String filename = "test.csv";
FileWriter fstream = new FileWriter(filename , true);
props.putObject("fstream", fstream);
Now i am trying to get the fstream object to another beanshell processor
fstream = props.getObject("fstream");
When i am running jmeter script, i am getting following error message:
Error in method invocation: Method putObject( java.lang.String,
java.io.FileWriter ) not found in java.util.Properties'
I know why i am getting this error because i m trying to pass a filewriter object but this type of function not found in properties class.
Then how should i pass filewriter object between beanshell processor, please explain and provide the sample code.

If it is within the bounds of one Thread Group you can go for vars.putObject("fstream", fstream);
If the scenario assumes different thread groups you can go for just props.put("fstream", fstream); function as it assumes Object already
There is bsh.shared namespace so you can share an arbitrary object via it like:
in 1st scripting element:
bsh.shared.fstream = fstream
in 2nd scripting element:
fstream = bsh.shared.fstream
Consider switching to JSR223 Test Elements and Groovy language as this approach has much better performance comparing to Beanshell

Related

Jmeter JSR223 Unable to read data from CSV file

Requirement is ItemID should be read from external CSV file and passed in the JSR223 preprocessor script. This is the groovy code in JSR223 and it is working as expected in the grrovy executer.
def items = []
List<String> lines = new File("C:\\Users\\854986\\itemid.csv").readLines()
def itemNo = RandomUtils.nextInt(5, 10)
1.upto(itemNo) { index ->
def item = [:]
def lineFromCsv = lines.get(index as int)
item.put('itemId', lineFromCsv)
But it jmeter it is throwing error and below is the error message.
"2022-11-15 12:34:14,398 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 PreProcessor 3
javax.script.ScriptException: org.apache.commons.jexl2.JexlException$Parsing: JsonBuilder#1:20 parsing error near '... ment, miss ...'
at org.apache.commons.jexl2.scripting.JexlScriptEngine.compile(JexlScriptEngine.java:237)"./
I have java 19.0.1, Jmeter 5.4.3 and groovy 4.0.2.
You're talking about "groovy code". Any reason to use jexl2 language in the JSR223 PreProcessor?
Make sure to choose groovy as the language and at least this error should go away. Groovy is the recommended scripting option since JMeter 3.1 mainly because Groovy provides maximum performance comparing to other engines

How to pass a 2-D array from one thread to another thread in Jmeter

So I am using a bean shell post processor and towards the end I am storing the result in a 2-D array. Result is stored this way.
testArray[0][0] = "1"
testArray[0][1] = "Test"
testArray[1][0] = "2"
testArray[1][1] = "STG"
My requirement is that I need to pass this 2-D array to the next thread. How am I supposed to proceed ?
First of all since JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting so consider migrating to Groovy.
Coming back to your question just use props shorthand like:
In first thread:
props.put("testArray", testArray);
in the other thread:
testArray = props.get("testArray");
you might also want to add the current thread number as the prefix/postfix so different threads could have different arrays with separate values like:
props.put("testArray_" + ctx.getThreadNum(), testArray);
where ctx stand for JMeterContext class instance
More information on these ctx, props and other JMeter API shortcuts available for JSR223 Test elements: Top 8 JMeter Java Classes You Should Be Using with Groovy

Store extracted value in csv file using jsr223 postprocessor in jmeter

How can i store extracted value of a variable in a csv/text file using JSR223 post processor
If this is something you really need to do in the JSR223 PostProcessor the minimal code would be:
new File('/path/to/your/file.csv') << vars.get('YOUR_VARIABLE_NAME_HERE') << System.getProperty('line.separator')
However if there will be a minimal concurrency you will run into the race condition when 2 or more threads (virtual users) will be writing into the same file resulting in data corruption
The approach which I would recommend is using:
Declare the variable you want to store via Sample Variables JMeter Property by adding the next line to user.properties file (lives in "bin" folder of your JMeter installation):
sample_variables=YOUR_VARIABLE_NAME_HERE
Once done you will be able to write the values using Flexible File Writer configured like:
You basically need to write the code to write into file.
Something like:
import org.apache.commons.io.FilenameUtils;
attr1 = vars.get("attr1");
attr2 = vars.get("attr2");
f = new FileOutputStream(locationOfCSVOutputfile, true);
p = new PrintStream(f);
p.println(attr2+","+attr2);
p.close();
f.close();
Like wise get the values you need and write into the file by comma separated.
Beware that in multiple threads scenario, Many threads will be accessing same file. therefore the file output may not be what you expected. To overcome this I used a critical section controller.
Hope this helps.
1/ Consider for example a node in your test plan with your request :
A regular expression extractor and a JR223 post processor component as child of your request.
2/ If you extract for example a multiple variable named "blabla" positioning the match number to "-1"
3/ Here's the piece of Groovy code that you can use in your post processor component to write your variable in a file :
import org.apache.jmeter.*;
File outputFile = new File("MY_FILE.csv")
int max = Integer.parseInt(vars.get("blabla_matchNr"));
for (i=1;i<max;i++) {
def word = vars.get("blabla_"+i);
outputFile << word << "\r\n"
}

Listing every one of the variables stored in JMeterVariables

I'm making extensive use of JMeterVariables/vars in my current JMeter code- in particular I'm generating variable names dynamically, based on JSON input.
Is there any way of getting the program to kick out a list of every single variable that is stored in vars??
Thanks!
You can do one of the following:
Add Debug Sampler in your script, with JMeter Variables = true
Similarly, add Debug Post-Processor under the sampler which creates dynamic variables
Since vars is just a regular Map, you can iterate it using one of the scripting languages (BeanShll, Groovy, etc):
for(java.util.Map.Entry<java.lang.String,java.lang.Object> var in vars.entrySet())
{
log.info(var.getKey() + "=" + var.getValue().toString());
}
Note that some variables are objects, so their representation will depend on how toString() function is implemented for them.
Add JSR223 Sampler to your Test Plan
Put the following code into "Script" area:
vars.entrySet().each { var ->
log.info(var.getKey() + '=' + var.getValue())
}
All the variable names and values will be printed to jmeter.log file
More information: Apache Groovy - Why and How You Should Use It

What is the code to get the Processor Name and Processor Group Name

Is there a way in Groovy Code to get the Processor Group Name the ExecuteScript Processor is in and Processor Name of the ExecuteScript Processor the Groovy Code is in. If so what would the code be. Any help would be greatly appreciated.
To get the processor name, use ProcessContext#getName(). The ProcessContext class is referenceable from ExecuteScript via the provided variable context, so the code would be String processorName = context.getName().
To get the process group name, I am not aware of an easy way through the framework code. You can, of course, use the Apache NiFi REST API to request the list of process groups and iterate through, checking to see if the process group contains a processor with the identifier of the current processor.
to get the name of all the processors and process groups name, you can use
the following code.
final EventAccess access = context.getEventAccess();
final ProcessGroupStatus procGroupStatus = access.getControllerStatus();
procGroupStatus.getProcessGroupStatus();
final ProcessorStatus processorstatus = procGroupStatus.getProcessorStatus()
ProcessorStatus class contains getName method, which can be used to get the name other processor.
Below is the source code of the same class for your reference.
https://github.com/apache/nifi/blob/master/nifi-api/src/main/java/org/apache/nifi/controller/status/ProcessorStatus.java

Resources