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.
Related
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
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"
}
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.
I am trying to create a jmx to test some database entries. The exact queries are not known beforehand and need to be picked from one of many sets of queries, that I have declared in separate pre-processor units.
For eg. one of the set of queries is:
String[][] animals = new String[][]{
{"VLS_CATS_ASSOC","CAT_ID_AS = '${CAT_ID_AS}'"},
{"VLS_DOGS_EXCH","DOG_ID_ST = '${DOG_ID_ST}' and DOG_TXT_REF = '${DOG_TXT_REF}'"},
};
The 2D array has two entries, the 'table name' and the 'where clause'
So, the jdbc request is select * from ${table_name} where ${where_clause}
I've set up a loop controller for iterating through the tables one by one, and as a child i have jdbc sampler, that has the csv table config which will contain data for CAT_ID_AS, DOG_ID_ST, DOG_TXT_REF.
Currently, when i see my requests through a results listener, i see that the queries sent are:
select * from VLS_CATS_ASSOC where DOG_ID_ST = '${DOG_ID_ST}' and DOG_TXT_REF = '${DOG_TXT_REF}'
It is clear from the output that the first level of substitution has worked, but not the second one.
Can anybody please help me on this?
Edit: Adding an image of the test plan. Jmeter Test Plan
Don't use ${CAT_ID_AS} notation in (I presume) the BeanShell strings: while it is generally working, that's an undocumented and error-prone feature.
The legit way is vars.get("CAT_ID_AS")
vars is a pre-defined object available in any BeanShell piece inside your test plan
So your code would be looking like
String[][] animals = new String[][]{ {"VLS_CATS_ASSOC","CAT_ID_AS = '" + vars.get("CAT_ID_AS") + "'"}, ... etc
I have a question on the visibility scope of variables. I have two thread groups, each with CSV file and controller containing http request steps, testing the application.
Thread Group1
CSV file1
Controller1
Test Steps1
Thread Group2
CSV file2
Controller2
Test Steps2
Both Controllers use the values in each Thread Group's CSV file just fine. However, I then want to make a Thread Group Combined containing both Controller1 and Controller2
Thread Group Combined
CSV file1
CSV file2
Controller1
Test Steps1
Controller2
Test Steps2
The problem is that the CSV files both use the same variable name, therefore I guess Controller1 and Controller2 don't know which CSV file to use. I still want Controller1 to use CSV file1 and Controller2 to use CSV2. Will a setup like this work?
Thread Group Combined
Controller1
CSV file1
Test Steps1
Controller2
CSV file2
Test Steps2
Thanks for any help!
If you use a 'Config Element' - It does not matter where you place it - they are like Global variables (Test Plan Scope). They get initialized before your test starts .
Lets assume i have a set up like this.
Thread Group 1
User Defined Variable (A=100)
Thread Group 2
User Defined Variable (A=150)
Now If i run this test - Under Thread Group 1 - If i try to print the value of A, it would be 150. This is because - Scope of these variables are not within Thread Groups - they are global variables.
For this setup,
Thread Group 1
Thread Group 2
User Defined Variable (A=150)
Now If i run this test - Under Thread Group 1 - If i try to print the value of A, it would still be 150 even if i do not have User Defined Variables under Thread Group 1.
However - variables you create with Beanshell etc have 'Thread scope'.
So, you might want to use unique variables accordingly.