Jmeter Debug Sampler | How to save all variable values to CSV - jmeter

I am able to extract values for many variables and are showing in Debug Sampler.
Is there any way to save these all variable values to a CSV file?
I found a solution (using BeanShell script) to save multiple Jmeter variable to CSV but I want all variables values to a single CSV, so that I can use the CSV file for next thread run.
Here is the snapshot of one of the Debug Sampler:
enterCompanyname=APITENANT
CreateTenant_Status=Success
CreateTenant_Status_matchNr=1
Current_UTC_Time=2018-03-07T01:53:18.310Z
DB_DataSource=dev4574857
DB_Password=1234
DB_UserName=web
DeviceCount=19
DevicesPerUser=94
EXCELPATH=X:\QualityAssurance\XLSX_3 columns_1000 rows.xlsx
Email=apitenant#apitenant.com
EndDate=2018-12-31
Exist=false
Exist_matchNr=1
FirstName=API
JMeterThread.last_sample_ok=true
JMeterThread.pack=org.apache.jmeter.threads.SamplePackage#69ab73cf
LastName=TENANT
LicensePlan=Pro
LicenseType=Device
MaxUsers=11
Password=Password
Protocol=http
RandomNumber=10

Add JSR223 Sampler to your Test Plan (where you want variables to be saved)
Put the following code into "Script" area:
def csv = new File('vars.csv')
vars.entrySet().each {var ->
csv << var.key + '=' + var.value + System.getProperty('line.separator')
}
That's it, you will have vars.csv file created in JMeter's "bin" folder having all variables listed. You might also want to replace = with , for better CSV Data Set Config compatibility.
vars is a shorthand to JMeterVariables class instance, it provides read/write access to all JMeter Variables.
Also be aware that starting from JMeter 3.1 users are encouraged to switch to JSR223 Test Elements and Groovy language so consider migrating to Groovy as soon as it will be possible. See Apache Groovy - Why and How You Should Use It for more details.

Related

JMeter update user defined variable at run time

I am trying to update the user defined variable set at a test plan level from a thread group. This is my scenario:
Test plan> user defined variable (variable name/value: fBurst=0)
Test plan> Threadgroup1 > Once only controller> JSR223 test plan (inside the test plan I have the following code)
log.info("fBurst user defined value : " + ${fBurst})
vars.put("fBurst", Integer.toString(111))
log.info("fBurst user defined value vars.put' : " + ${fBurst})
props.put("fBurst", 222)
log.info("fBurst user defined value props.put' : " + ${fBurst})
What I am trying to do here is to update the fBurst user defined value from inside the once only controller sampler and so far not been able to do it using the vars.put or props.put. Is there another way to do this?
Take a look at JSR223 Sampler documentation:
The JSR223 test elements have a feature (compilation) that can significantly increase performance. To benefit from this feature:
Use Script files instead of inlining them. This will make JMeter compile them if this feature is available on ScriptEngine and cache them.
Or Use Script Text and check Cache compiled script if available property.
When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters.
So replace ${fBurst} with vars.get('fBurst") or props.get('fBurst') correspondingly and it should start working as expected
More information: Top 8 JMeter Java Classes You Should Be Using with Groovy

Jmeter - how to write specific variable to CSV file to specific row/column

My csv file looks like:
TC_name, username, password, excpecedCode
ad_test_master_successful_login,username,Test123!,200
What is the easiest way to so i can write to csv to specific row/column, and example to overwrite Test123! with variable fetched from user defined variables?
I know i can read value using JSR223 Pre/post processor with ex:
def line10 = new File('C:/Users/test/Desktop/testData/login.csv').readLines().get(1).split(",")[2]
log.warn("csv as-> " + line10);
There is no such concept as "cell" in CSV files, if you're looking for the code which will replace one string with another, you can do something like:
def file = new File('test.csv')
def text = file.text.replaceAll('Test123!', vars.get('foo'))
file.text = text
If you're looking for a better option you can consider using GroovyCSV library (you will need to download it and place in JMeter Classpath followed by JMeter restart) or consider switching to Excel file where you will have full control via Apache POI library like it's described in How to Implement Data Driven Testing in your JMeter Test article

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

Apache Jmeter read SQL variables from csv file

Need to load test a Oracle database the requirement is to fire sql queries concurrently to the database reading the varaibles from a CSV file is this feasible ?
Have a CSV file with values like
Name Email
Justin justin#beiber.com
George george#washington.com
...
Micheal micheal#jackson.com
And then have 10,20,30 users fire of queries like
select name,phone,city
from address
where name = <<feild1-from-csv>>
and email = <<feild2-from-csv>>
...
I'd suggest splitting your test logic into 2 separate pieces:
Load information from CSV and store it as JMeter variables
Execute SQL code against Oracle using variables from point 1
In regards to implementation I'd suggest to use 2 separate Thread Groups, the first one will be loading stuff from CSV, the second one will be doing actual testing.
Important: don't forget to check "Run Thread Groups Consecutively" box at TestPlan level to assure that second thread group runs after first one.
Example configuration of 1st thread group:
Counter
Start - 1
Increment - 1
Reference name - counter
CSV Data Set Config
Filename - path to your csv file
Variable names - name, email
Delimiter - if you're using TAB - "\t", if comma - "," without quotes
Allow quoted data - False
Recycle on EOF - False
Stop thread on EOF - True
Sharing mode - All Threads
Beanshell Sampler (this one is optional, JMeter 2.10 is smart enough to store variables for you but I prefer to control everything myself)
Code for Beanshell sampler shoud look as follows:
props.put("name" + vars.get("counter"), vars.get("name"));
props.put("email" + vars.get("counter"), vars.get("email"));
It fetches current "name" variable and stores it as name + counter property.
So given 3 lines is CSV file you'll have following properties:
name1=Justin
email1=justin#beiber.com
name2=George
email2=george#washington.com
name3=Micheal
email3=micheal#jackson.com
You can use Debug Sampler to see JMeter Variables and Properties values
After that in second thread group you can refer stored properties as:
${__P(name1,)}
or
${__property(name1,,)}
in your JDBC Request Sampler.
Both should work.

Resources