Aleksey Merzlyakov is an experienced QA Engineer with 5+ years of experience in Web and Software QA testing.

Sergey Horban (pictured) is a QA Engineer with 3+ years of experience in automation and manual testing, bug-tracking and test management.

Learn JMeter in 5 Hours

Start Learning
Slack

Test Your Website Performance NOW!

Oct 08 2017

How to Initialize Database Variables and Assign Them Values - For Multiple Thread Groups

Creating  a variable and assigning a value to it, is important for writing test cases that use database queries. In our last blog post “How to Initialize Database Variables and Assign Them Values”, we learned to initialize variables and assign values to them, in a database, for one Thread Group. In this blog post we will do it for multiple thread groups.

 

Let’s get started.

 

We will use the same API example we used in the previous blog post. This API uses input parameters (for example, the "address" parameter). It then takes the unique value (the client's address) and returns data (about the payments made by it). The API takes the information from the database (the Payment Table).

 

API requirements:

  • The verification of the existence of a unique client address value is checked in the Customer Table, where address = address_id
  • The unique value of the customer address, in the Customer Table, defines a unique customer number (customer_id)
  • The value of the customer_id in the Payment Table shows the payments made by the customer
  • If the payment exists, the API displays all the data from the Payment Table
  • If there are no payments, the API returns an empty response

 

Customer Table:

 

How to Initialize Database Variables and Assign Them Values - For Multiple Thread Groups

 

Payment Table:

 

variables, jmeter, open source

 

We will perform test cases for two clients (instead of one like last time). We will receive payment data for each customer in a separate Thread Group. Thus, each test case is a separate Thread Group.

 

Before launching each test case, incoming data should be checked to ensure that it corresponds to the purposes of the test case. To verify the incoming data in each Thread Group (we will assume that each Thread Group is a separate test case), you need to perform a client search to determine the availability of the payment data, and only then perform a request to the API.

 

In other words, each Thread Group will contain repeated actions to search for incoming data to perform a test case (in our example, repeated actions are a client search). To avoid these repetitive actions, you can search for clients in a separate Thread Group. This will reduce the time for the execution of the test case and avoid duplication of requests to the database, which can be done once.

 

To perform the test cases, you need to do the following:

 

1. Add two Thread Groups, to your script.

 

2. Configure the connection to the database as indicated in the previous article "MySQL Database and JMeter - How to Test Your Connection"

 

3. In each Thread Group element, add one JDBC Request element:

  • Thread Group # 1. JDBC Request # 1 will perform a client search in the "Customer" table
  • Thread Group # 2. JDBC Request # 2 and JDBC Request # 3 will search for payments in the "Payment" table for each customer

 

4. In the Test Plan element, add the View Results Tree element, which allows you to display the responses received from the database, as shown below.

 

using variables in jmeter

 

5. For JDBC Request # 1 from Thread Group # 1, add the BeanShell Assertion element.

 

6. Search for 2 customers in the Customer table through an SQL Query.

 

mysql database, variables, jmeter

 

Example code:

 

select customer_id, address_id  
from customer 
where address_id is not null
and customer_id is not null
limit 2

 

7. Run the tests and view the results for Thread Group #1 - we can see the customers in the Customer table.

 

assigning values to variables in jmeter

 

8. Assign customer_id values to variables and assign address_id values to variables in the BeanShell Assertion. The purpose is to get incoming data for several test cases (thread groups) but in one thread group, and then use them in several thread groups.

 

variables, values, jmeter

 

Note: The props.put method from the JMeterVariables class is used to create a variable available in any JMeter element in any Thread Group.

 

Example code:

 

if(ResponseCode.equals("200")) {

	if(vars.getObject("customer").size() != 0) {

		String IDClient_1 = vars.getObject("customer").get(0).get("customer_id") + "";
		props.put("IDClient_1",IDClient_1);

		String IDClient_2 = vars.getObject("customer").get(1).get("customer_id") + "";
		props.put("IDClient_2",IDClient_2);

		String address_1 = vars.getObject("customer").get(0).get("address_id") + "";
		props.put("address_1",address_1);

		String address_2 = vars.getObject("customer").get(1).get("address_id") + "";
		props.put("address_2",address_2);

		log.info(" Unique value of customer №1 address:  " + address_1);
		log.info(" Unique value of customer №2 address:  " + address_2);
		
		log.info(" ID client №1:  " + IDClient_1);
		log.info(" ID client №1:  " + IDClient_2);	
	} else {
	
		FailureMessage = "!!!!!!!!!!!! The response is empty !!!!!!!!!!!!!";
		Failure = true;	
		prev.setStopThread(true); 
	}		
} else {
	
	FailureMessage = "!!!!!!!!!!!! No connection to the database !!!!!!!!!!!!!";
	Failure = true;	
	prev.setStopThread(true); 
}

 

9. Get IDClient_1 and IDClient_2 in Thread Group # 2.

 

Since the variables IDClient_1 and IDClient_2 were not created in the Thread Group in which they will be used, it is necessary to get these variables before getting to the Thread Group in which they will be used.

 

To do this, in Thread Group # 2, add the BeanShell PreProcessor element.

 

assigning variables to thread groups, jmeter

 

variables in jmeter

 

10. Get the IDClient payment information from the Payment Table by passing the IDClient_1 and IDClient_2 variables through the SQL query.

 

Example code:

 

String IDClient_1 = props.get(“IDClient_1”);
vars.put(IDClient_1, “IDClient_1”);

String IDClient_2 = props.get(“IDClient_2”);
vars.put(IDClient_2 , “IDClient_2”);

 

sql query, jmeter, variables

 

Example code:

 

select *
from payment 
where customer_id = "${IDClient_1}"

 

variables in jmeter

 

Example code:

 

select *
from payment 
where customer_id = "${IDClient_2}"

 

The IDClient_1 variable is converted to a value at the time the request is submitted to the Payment Table. A similar conversion is performed for the variable IDClient_2.

 

variable conversion, jmeter

 

The results of receiving all payments by IDClient_1 = customer_id from the payment table:

 

jmeter, variables, assigning values

 

The results of receiving all payments by IDClient_2 = customer_id from the payment table:

 

variables results in jmeter

 

That’s it! You now know how to initialize database variables and assign them values, when working with multiple thread groups.

 

You can learn more JMeter for free through our JMeter academy.
 

Click here to subscribe to our newsletter.

 

To check out BlazeMeter, which enhances JMeter, request a demo or just put your URL or JMX file in the box below, and your test will start in minutes.

     
arrow Please enter a valid URL

Interested in writing for our Blog?Send us a pitch!

We're working on starting your first test

Testing 20 Virtual Users

Starting your test in around 2-4 minutes. Your report will appear once we've gathered the data.

0

Status: Preparing Your Test

Your email is required to complete the test. If you proceed, your test will be aborted.