JMeter Pro Tip – Extracting AWS Meta Data For your JMeter Test

JMeter Pro Tip – Extracting AWS Meta Data For your JMeter Test

In this example we will show how to extract AWS instances parameters using JSR223 Sampler on JMeter. In addition, you will learn how to run bash command scripts on remote linux servers using JMeter. This is a JMeter Pro Tip that can save you time.

In some cases it is necessary for the load test to send a linux command to  the remote linux server. For example, after completing a load test and before retesting again you might need to connect to the servers to delete some folder or content. To do this you need to use putty program to connect via ssh protocol. For instance, one load test may download large images. Before beginning the next test you may need to delete the data. If using many servers this might require extra effort. Using this method you can access the servers and easily delete or modify the data.

Let’s get started.

JMeter Pro Tip: Step by Step

Firstly we will add a Thread Group to our Test Plan.

Second step is to add the JSR223 Sampler to our Thread Group and add the script to execute a Linux command on an EC2 instance to get the instance type.

Now we are ready to configure our JSR223Sampler. To execute any query on AWS instances “execute()” method is using on JSR223Sampler. To get the output message after execution we declared a parameter named “string”.

def string="/bin/bash -c EC2metadata --instance-type".execute().text

vars.put("output", newTest)

When executing the script, the important point is that we need to save the AWS instance name as a variable. Our variable name is “output”.

Now we add a BeanShell Sampler on our Thread Group. The crucial part of this section is to give the sampler a distinguishing name. Our distinguishing name is the AWS instance name which we extracted from our previous sampler “output”. So we are naming this sampler as BeanShell ${output}. ${output} refers to AWS EC2 instance type name such as a t2.smalllocal.

After completing the script, we run the script through RedLine13. The result are:

To extract just the instance type we need to split the whole info using “-” character. To perform this we just add 2 lines of code and modify the “output” value with the new one.

String[] test = string.split("-");

String newTest = test[17];

vars.put("output", newTest)

When making a remote connection through ssh protocol on an AWS instance and running this command ec2metadata  –instance-type it gives only the instance name as a result. However, when you try this command (ec2metadata  –instance-type) on JMeter it instead gives you the full result as “ec2metadata” (see picture above).  This does not cause a problem. Adding the 2 lines of code above we can extract the variable to whatever we want.

JMeter Pro Tip – Running Your Script

After completing the script we are now ready to run the script on RedLine13. Adding an EC2 server is extremely easy and flexible. Click Servers->Server Mgnmt->Add Server and then choose the instance.

After launching the server, let’s run our script on RedLine13.

After completing the test, the results are shown below.

What is your JMeter Pro Tip?