Scripting for load tests

Applies to ReadyAPI 3.56, last modified on November 20, 2024

When ReadyAPI Performance runs a load test, it generates many virtual users that simultaneously go through the targets of the load test. In other words, generated virtual users execute steps of target test cases, including scripts in them. Running a script in a load test differs from running a script in a functional test. This topic covers these differences and provides a few tips on how to create such scripts.

Optimize scripts

When you create scripts for load tests, you should keep in mind that during a load test, the script may behave differently from a functional test run. This script is executed multiple times in parallel. Besides that, the set of scripting objects is different from what you can use in a functional test. That is why, you must prepare your scripts before using them in load testing.

  • Do not write a file from Groovy scripts.

    ReadyAPI runs each virtual user in a separate thread. Many parallel threads may handle writing to a file incorrectly that will cause errors.

  • When you get the test case object in a script, use the testRunner.getTestCase() method.

    Each virtual user works with its own copy of the test case and its properties. The testRunner.getTestCase() method returns the test case instance specific to this particular virtual user.

    If you obtain the test case object in another way (for example, as a child object of the project object), you will get a test case instance stored in the project. Property and field values of this instance may differ from the values you expect to get.

  • Copy script libraries to agents.

    If you run a distributed load test, you need to copy additional libraries to the agent machines. To do it:

    • Copy the external Java libraries to the <LoadAgent>\bin\ext folder.

    • Copy the Groovy scripting libraries to the <LoadAgent>\bin\script folder.

Virtual User Context

Each virtual user has a unique context object. This object is created when the user starts simulating tests and is removed at the end.

Virtual User ID

By default, the virtual user context has the VirtualUserId property. It contains the ID of the virtual user that is unique among other existing virtual users:

Groovy

// Get a virtual user context
def vucontext = context.get("VirtualUserContext");

if (vucontext != null) {

    // Get a virtual user ID
    def userId = vucontext.get("VirtualUserId");

}

Pass data between load test targets

You can use the virtual user context to pass data between test targets. To save the data, use the following code:

Groovy

// Get a virtual user context
def vucontext = context.get("VirtualUserContext");

if (vucontext != null) {

    // Save data to the context
    vucontext.put("foo", data);

}

Later, you can get that data by using the following code:

Groovy

// Get a virtual user context
def vucontext = context.get("VirtualUserContext");

if (vucontext != null) {

    // Get data from the context
    vucontext.get("foo");

}

See Also

Preparing Functional Test Cases for Load Test
Scripting

Highlight search results