Unable to retrieve random DB values In JMeter - jmeter

In JMeter, I am able to connect to DB and retrieve DB values.
I can view the DB_Value using variable which I specified in JDBC request(Variable Names field).
Example: with index of column value as CustomerID_1
I used BeanShell Sampler with below code:
${__BeanShell(vars.put("p_CustomerID_New","${CustomerID_1}"))}
with this p_CustomerID_New i am able to get db value and that works.
Now i have another random variable to replace above "1" with random numbers, so that i will be able to use different customers from DB and substitute in my API_Request.
When i use beanshell script with ${CustomerID}_${randomNo}, it only stores random number and CustomerID is not retrieved.
Any help would be much appreciated.
Thanks in advance.

You don't need to use any scripting at all, just to for __V() and __Random() function combination:
${__V(CustomerID_${__Random(1,${CustomerID_#},)},)}
According to JDBC Request Sampler documentation the following variables are getting generated when you run a Select statement:
Customer_ID_# - number of rows returned
Customer_ID_1 - value from the first row
Customer_ID_2 - value from the second row
etc.
So
${__Random(1,${CustomerID_#},)} function returns a random number between 1 and the number of returned rows
__V() function evaluates the generated expression and returns the random value
More information on JMeter Functions concept: Apache JMeter Functions - An Introduction
Going forward be informed that since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting

First of all using of Beanshell is deprecated and it is not a recommended to use it in Testplan.
It is recommended to use JSR223 pre/post-processor according to your needs.
I used pre-processor for the sampler - it works well
import java.util.Random;
Random rand = new Random();
def varName= "CustomerID_";
int randBoundary = rand.nextInt(100);// this will generate values between 0-100
varName = varName + randBoundary;
vars.put("p_CustomerID_New", vars.get(varName));
You can use this sample code and modify according to your requirements.

Related

How to use "Random Variable" in JSON Extractor in Jmeter?

I am capturing bunch of available seat list using JSON Extractor (Please refer "1_JSON_Extractor" ) , I am using -1 to capture all the ordinals, it is working fine (Refer "2_Debug_Sampler").
I need to take the random value from the list, Instead of using 0 for Random Ordinal, I am trying to use Random Variable here,( Please refer "3_Random_variable"). But it is not working fine.
I am planning to use the the Random variable "C_Seat_1" in the place of JSON Extractor Match No and get the random value. (Please refer "4_Json_Extractor")1_JSON_Extractor .2_Debug_Sampler3_Random_variable4_Json_Extractor
Can you help ?
Take a look at JMeter Test Elements Execution Order
0. Configuration elements
Pre-Processors
Timers
Sampler
Post-Processors (unless SampleResult is null)
Assertions (unless SampleResult is null)
Listeners (unless SampleResult is null)
Random Variable is a Configuration Element
JSON Extractor is a Post-Processor
Hence when Random Variable is evaluated JSON Extractor hasn't been executed yet therefore ${C_Selected_Seat_All_matchNr} is not defined
You can just go for __Random() and __V() functions combination to pick a random seat directly where required without any extra configuratino elements, something like:
${__V(C_Selected_Seat_All_${__Random(1,${C_Selected_Seat_All_matchNr},)},)}
More information on JMeter Functions concept: Apache JMeter Functions - An Introduction

How do I get String-value from jdbc sampler to INT value in other jdbc sampler?

It appears that Jmeter's jdbc sampler only want to save stuff as String.
In a later step I need this value as an Int-value. But syntax gets wrong all the time for me... tried converting it in beanshell script, but got syntax errors there too... x)
Example:
Sampler 1
JDBC: SELECT number from table
variable names: number1
Handle resultset: String
Sampler 2
JDBC: Select * from table2 where number = '{$number1}'
ERROR SQLCODE=-420
Any ideas? :D
JMeter Variables are always Strings (unless you explicitly call vars.putObject() function in a script)
With regards to your question itself, when you define a Prepared Select Statement in the JDBC Request and properly populate "Parameter values" and "Parameter types" fields like:
More information:
JDBC - Data Types
Using Prepared Statements
Also with regards to your Beanshell approach, altough it's not applicable here at all, be informed that starting from JMeter 3.1 you should be using JSR223 test elements and Groovy language for scripting mainly for performance reasons, see Apache Groovy - Why and How You Should Use It article for more details.

jmeter - Looping based on DB query - use db data as variables

Based on this thread: jmeter - Looping based on DB query, i managed to get counter to the Loop controller, and and working fine.
Now i need updated version, where DB query returns 2 variables, so i can use them as parameters for the call.
url secret
https://test1.com/ 1234
https://test2.com/ 1234
https://test3.com/ 1234
And to be able to use them in:
As:
But, when i tried to use them as: ${url_#}, ${key_#} test is not working.
Is there other any way how can i use those 2 variables fetched from DB query, and the looping logic to be respected?
Any help is appreciated!
You need to use the following test elements combination:
${__jm__Loop Controller__idx} pre-defined variable to get the current Loop Controller's iteration number
__intSum() function to add 1 to the iteration number (it's zero-based)
__V() function to put everything together
The combination would be:
${__V(url_${__intSum(${__jm__Loop Controller__idx},1,)},)}
Demo:
More information: Here’s What to Do to Combine Multiple JMeter Variables
I found my answer. what help me was: ${__V(url_${__counter(,)})} variable together with counter function.

How to use value of variable (holding integer value) from JDBC request in while loop?

I have a JDBC request to fetch row count - 'rownum' of a table. Now I need to do some additional SQL operations in separate JDBC request in which I need to extract data of only 2 rows at a time which I am able to handle using counter. Now I need to decrease the rownum by 2 as I have already used two rows of the table. So I am trying to use while loop in which I would have while rownum>0. But I . have no idea how to use the output variable of first JDBC request which will hold the integer value in while loop. I also need to subtract 2 from this in every iteration.
The output variable name of first JDBC request is "count_num"
Then I used beanshell to parse this as integer using following code:
int i = Integer.parseInt(vars.get("{count_num}").trim());
This beanshell sampler is also failing. I even tried using vars.get("count_num"), but that also failed stating ::
Error invoking bsh method: eval Sourced file: inline evaluation of: ``int i = Integer.parseInt(vars.get("count_dcn").trim());'' : Typed variable declaration
Now in my while loop, I want to use this value of I (no idea how to do it, should I be using ${i}).
I also need to subtract 2 from the value of I probably at the end of while loop (after second forEach controller maybe), again not sure how to do it.
Structure of Test Plan:
${__javaScript(parseInt(${count_dcn_1})>0)}
this code worked

Using Extracted JSON Value in Another JMeter Thread

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!

Resources