I'm very new to Jmeter and I'd like to know if there is some way to store the result of a query in a global variable to use in a different thread.
In other words, I need a set-up thread that sets a start-date and end-date (2 values) from the DB.
Then, in a second thread (the main thread), I have to use the start-date and end-date as parameters for the tests.
Is this possible?
Thanks in advance!,
Nahuel
Use the following elements:
JDBC_Connection_Configuration
JDBC Request
BeanShell Sampler
setUp Thread Group
Organize them as following:
It will work as following:
JDBC Connection Configuration will setup the connection to DB, name Variable name so that it matches Variable name of JDBC Request, in my case I name it conn
Setup Thread Group will run query through JDBC Request and store result in variables
Beanshell sampler with use the value and store it as a property so it can be shared by all threads.
Note the following:
The variable names of JDBC Request must match the number of columns returned by your SQL Query, note in example I have 3 columns, I put 3 variables, and will use clt_nom_1 name as I ensure there is only row returned by query
In Bean Shell sampler I put the following code:
props.put("toto",vars.get("clt_nom_1"));
clt_nom_1 is named like this because it's the first row value
Finally in Thread Group I can use property toto through:
${__P(toto)}
You could also replace BeanShell sampler by a debug sampler named:
${__setProperty(toto,${clt_nom_1})};
which would store variable in property
I have done it differently:
I created a BSF PostProcesser use 'Javascript' as the language:
var strData = prev.getResponseDataAsString(); //This is a string delimited with character return
var listData = strData.split('\n');
Then you can do all sort of things like from your list data, such as vars.putObject.
NOTE:It works with SELECT query on JDBC request.
Related
Based on this thread Jmeter - Use Loop controller based on array (created from from multiple variables) I managed to use Loop controller based on the array.
Now I need to pass the each value from the array into JDBC, so I can perform select statement based on every single member of the array.
What I try is:
But I got error as:
When I try the same statement with Dammy sampler is working fine.
How to pass member from the array into JDBC inside the loop controller?
JMeter log file explicitly states Cannot invoke method length() on null object
The only place where you're invoking length() function is vars.get('array').length() which means that your ${array} variable is null (not defined), you can double check it using Debug Sampler and View Results Tree listener combination.
If the same statement works elsewhere - the only explanation I can think of is variable scope, see JMeter Scoping Rules user manual chapter for more details.
I’ve got a call to my database working as a SQL select statement. But I am working to call a stored procedure using JMeter for further testing. I’m strictly working off of the JMX files and do not have JMETER integrated into our main Java project at this time.
I’ve setup the JMETER GUI with the JDBC Connection Configuration and the JDBC Request. I’ve made a successful call to my database with my callable statement with my string INPUT and get the string OUTPUT parameter string.
The OUTPUT parameter string only contains information about the call (user,system, success, etc…), but none of the values/data from the table -- which are found in the ResultSet/MetaData. But I cannot figure out how to get the ResultSet or the Metadata using the JDBC Request in JMETER.
In Java, I know I use the statement and just call statement.getResultSet() and perform a loop while resultSet.next() exists. How do I do this in JMETER?
I've tried adding an additional out parameter but then my statement rejects the call, because there is only one in-parameter. I've tried a variety of JMeter Assertions - but because the main call is only returning the out parameter, I cannot grab additional data.
Query: call XXXXX.readUser(?)
Parameter Values: ${inputJSONString}
Parameter Types: INOUT VARCHAR
Variable Names: ouputJSONString
Result Variable Name: ouputJSONString
View Results Tree: Response code: 200, Response message: OK, Output variables by position: Contains the whole JSON out parameter string with user, system, and success. Returns the table column headers but no values.
I do not have errors - the call is being made successfully. I just cannot figure out how to access the Result Set from JMETER.
Don't use the same reference name for the Variable Names and the Result Variable Name as the latter one will be overwritten.
So
Change ouputJSONString to i.e. ouputJSONStringObject
Add JSR223 PostProcessor as the child of the request
You will be able to access the JMeter's representation of the ResultSet as vars.getObject('ouputJSONStringObject') (basically ArrayList of HashMaps
See Debugging JDBC Sampler Results in JMeter article for more details.
Unfortunately you cannot access the normal ResultSet as it is not exposed anywhere and being converted via private function
Looping test occurrence based on the data count retrieved from the JDBC request and also as input data for the HTTP request
I have test scenario where i need to use the DB output as the input criteria for the HTTP request. Based on the DB output count( from the first request) i need to loop the HTTP request and it data accordingly
I tried the logical Loop Count by passing the count variable from run time as ${TEST_ID_#}, still its not working.
I tried the logical Loop Count by passing the count variable from run time as ${TEST_ID_#}, still its not working.
Debug Sampler Output
You can extract the counter using Post Processor [either Regular Expression Extractor or JSON Extractor etc.]
Once you have extracted that count, now place a Loop controller as a parent of HTTP request.
For example. I am using User Defined Variable for loop Count:
Any reason for using ${TEST_ID_#} variable? If your Debug Sampler screenshot is full and correct you should be using ${KEY_ID_#} instead.
Also it might be a better idea to use ForEach Controller instead of the Loop Controller, the relevant configuration would be something like:
References:
How to Use ForEach Controller in JMeter
Using Regular Expressions in JMeter
I've been reading through the forums, the Apache JMeter guide, and BlazeMeter's The Real Secret to Building a Database Test Plan With JMeter and Using JDBC Sampler In JMeter, but I'm still kind of lost.
I need to issue a query to extract an Obj ID value from a table and pass that value into an HTTP READ Request. I've done the following setup:
JDBC Request
Variable Name: Pool-1
Query Type: Select Statement
Query: select distinct ObjId from dbo.CommonRuleSet where Name like '%ABC%';
Param. values:
Param. types:
Variable names: abcObjId
Result variable name: abcCommonRule = vars.getObject("resultObject").get(0).get("ObjId");
Query Timeout (s): 5000
Handle Result Set: Store as Object
When executed results in:
ObjId
1136682203
I'm trying to figure out how to pass in this ObjId value as a variable to append the URL.
HTTP REQUEST
database:port/applicationServer/../../crud/CommonRuleSet/????
I've tried appending using:
the Variable names value: ${abcObjId}
the Result Variable Name: ${abcResult}
Each time, JMeter is not translating the variable resulting in a parser error. (e.g., http://database:port/.../.../.../crud/CommonRuleSet/${abcResult})
I'm just not understanding how to take the results of my successful query and pass it as a variable to an HTTP Request. Any insight/enlightenment is much appreciated!
The solution was:
//database:port/.../.../.../crud/CommonRuleSet/${abcObjId_1}).
I guess I didn't go through enough BlazeMeter pages before posting my question. I found the solution on Debugging JDBC Sampler Results in JMeter.
The section explaining the difference and usage between Variable Names and Result Variable Name made all the difference. I now understand the Result Variable Name is an ArrayList of HashMaps, which I don't need in this scenario.
So I changed the Result Variable Name field from: abcCommonRule = vars.getObject("resultObject").get(0).get("ObjId"); to: result,
Next, since I want to directly access the Variable Name 'abcObjId', I modified the variable that appends my URLfrom: ${abcObjId}to: ${abcObjId_1}. I was so close... so close....
Thanks to dmitri-t and the folks at Blazemeter.
First, let me preface this question that I've only been using JMeter for 36 hours.
I've been able to successfully create a thread that performs a POST (json body) to generate a new record.
{
"id":1257697771,
"displayName":"TERM2",
"functionName":"f_1257697771",
"displayableSourceExpression":"TRUE",
"typeId":200,
"groupId":300,
"clobObjId":1257697772,
"typeCode":5,
..........
}
I need to take the new record's ID (1257697771) value returned in order to perform updates, get by ID, delete, etc. on this record in other threads.
After much reading, I've created a Regular Expression Extractor where:
Apply to: Main Sample Only
Field to Check: Body as Document
Reference Name: newRecord
Regular Expression: "id":(.+?)\,"displayName"
Template: $1$
Match No: 1
Default Value: NONE
At this point, I'm not sure if my Regular Expression is formatted correctly where (.+?) is valid.
Also, I'm confused if I can either just specify the new reference (newRecord) in another thread's HTTP request's Parameters or use a BeanShell Post-Processor, or a Response Assertion, etc....
There a lot of answers for the same function of "Passing". Not being a programmer, I've tried to follow the discussion "how to extract json response data in jmeter using regular expression extractor?", but I'm still not clear.
Any insight is appreciated. Thanks.
JMeter Variables are local to Thread Group, you need to convert your variable to JMeter Property.
Use:
__setProperty() function in the Thread Group where you define your newRecord variable like:
${__setProperty(newRecord,${newRecord},)}
__P() function to access property value like:
${__P(newRecord,)}
See Knit One Pearl Two: How to Use Variables in Different Thread Groups article for more detailed explanation.
Also be aware of the Function Helper Dialog as it looks like JMeter functions syntax was developed by aliens.
To pass a value between threads you need to use the jmeter property function.
In a jsr223 postprocessor using groovy the code to get the value is as follows:
def userProperty = props.get('propertyToGet')
vars.put('userProperty', String.valueOf(userProperty))
You would then access the variable in your thread using:
${userProperty}
Or you can use shorthand directly:
${__P('propertyToGet')}
Variables in jmeter are thread specific.
Thanks everyone. I was able to resolve it with your help!
In the first thread:
set the Reg Expression Extractor Regular Expression = "id":(.+?)\,"displayName"
added a Bean Assertion where Parameters = ${__setProperty(newRecord,${newRecord},)}
In the second thread:
appended the Path url with ${__P(newRecord,)}
Executing the first thread (POST) resulted an new record with a unique ID. (1257698108)
Executing the 2nd thread (GET) shows
GET http://server/.../.../.../.../1257698108
And returns the exact data generated in the first thread.
Thanks everyone for your help!