Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hey Ninja!!🥷Did you remember how you prepared for the entrance exams for university? The strategies you applied for revision and the actual were slightly different. The same goes for API testing as well. When we are in the testing phase, we deploy a different strategy than the actual working of the API. In this blog, you will learn how to write scripts for load testing and its optimization. ✨
Scripting for Load Tests
When we perform a load test, ReadyAPI generates many virtual users. These virtual users execute the target test cases simultaneously. The target test cases may contain scripts which then run in parallel threads. Running a script in a functional test differs from running it in a load test. In this blog, we will learn the differences and some strategies to build those scripts.
Optimise Scripts
Remember that running a load test script might behave differently than running a functional test. This script is executed several times simultaneously. The scripting object set differs from the one used in the functional test. Thus it is crucial to prepare our scripts beforehand to avoid unwanted behaviour. Let's go through some tips to optimize our load test scripts:
✅Dont perform the write operation from Groovy Scripts
ReadyAPI has the concept of virtual users who execute their tests in parallel threads. It might lead to inconsistency and errors while the writing operation takes place.
✅Copy Script Libraries to Agents
Suppose you are planning to perform a distributed load test, running these tests simultaneously from multiple machines. In that case, you need to copy the java and groovy libraries to the agent directory.
Copy the desired java libraries to <LoadAgent>/bin/ext folder.
Copy the desired groovy script libraries to <LoadAgent>/bin/script folder.
✅Use the testRunner.getTestCase() method to deal with the test case object in a script
This method returns the test case instance belonging to a specific virtual user. If you get the instance any other way, it is stored in the project, which might be different from what you expect.
Virtual User Context
All the virtual users have their context object—this object is created when the tests start executing and are flushed at the end. Let's learn about the context user ID and data transfer between load test targets.
Virtual User ID
The virtual user context contains the unique ID referencing the individual virtual user. The following groovy code snippet will find the user ID.
// Get the context of virtual user
def virtualUserContext = context.get("VirtualUserContext");
if (virtualUserContext != null) {
//Get the user ID
def userId = virtualUserContext.get("VirtualUserId");
}
Pass the data between load test targets
To pass the data between targets, we must first save it at the home location and then fetch it from the desired location. The following groovy code snippet will save the data:
// Get a virtual user context
def virtualUserContext = context.get("VirtualUserContext");
if (virtualUserContext != null) {
// Save data to the context
virtualUserContext.put("ninja", data);
}
The following code snippet will fetch the saved data:
// Get a virtual user context
def virtualUserContext = context.get("VirtualUserContext");
if (virtualUserContext != null) {
// Get data from the context
virtualUserContext.get("ninja");
}
Frequently Asked Questions
What is Groovy?
It is a java syntax following object-oriented language intended to be run on the java platform. It has features of both static and dynamic typed languages. Groovy is mainly used for script writing.
What is a Load Test?
A load Test is performed to simulate the real-world performance of the API when vast numbers of users access the API. All the scenarios that can come up are covered in the load test.
What is a Virtual User?
A virtual user is a temporary user account created for testing purposes by ReadyAPI. It simulates the behaviours of a real user for testing the API.
Conclusion
In this blog, you learned about scripting for load tests in ReadyAPI. All the important topics related to scripting for load tests in ReadyAPI were covered in detail.
Also, do refer to other API testing-related articles: