Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

Learn how to initialize database variables and assign them values when you're working with multiple thread groups as opposed to just one.

Free Resource

Learn how to create flexible schemas in a relational database using SQL for JSON.

Creating a variable and assigning a value to it, is important for writing test cases that use database queries. In our last blog post, 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:

Payment table:

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 ThreadGroup (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:

Add two ThreadGroups to your script.

Configure the connection to the database as indicated in the previous article.

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

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

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

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

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.

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.

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); 
}

Get IDClient_1 and IDClient_2 in Thread Group #2.

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”);

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 BeanShellPreProcessor element.

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”);

Example code:

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

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.

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

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

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

Create flexible schemas using dynamic columns for semi-structured data. Learn how.

Topics:
database ,database performance ,thread groups ,tutorial ,database tables

Published at DZone with permission of Aleksey Merzlyakov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}