What happens to the state (cache, cookies, and variables) between different iterations of the same bot?
Whether or not cookies and variables remain in scope between iterations depends on a setting called Clear cache, cookies, and variables after each iteration.
You can enable or disable this setting on your Settings page.
Enabling the setting
This setting is enabled by default, so each bot gets a fresh state for every iteration. All cookies and variables from the previous iteration disappear. From your site’s perspective, it’s like a whole new visitor is arriving, even though on the Loadster side it’s the same bot returning with a clean state.
In the case of Browser Bots, the browser also starts a new “incognito” session with each iteration, so none of the cache or session state is left over.
In most cases this default setting is desirable, because the total number of iterations in your load test will correlate with the total number of unique user sessions, and you’ll avoid the potential confusion of leftover state from the previous iteration of your script.
Disabling the setting to clear cache and cookies
Occasionally you’ll want to do some fancy looping or something, and will want the bot’s state preserved between iterations of the same bot.
For example, you might have a situation where you want each bot to log in on their first iteration, and then repeat some task over and over throughout the duration of the test, without logging in again.
You could accomplish that by disabling the Clear cookies and variables after each iteration setting, and adding a code block to the beginning of your script that contains a bit of logic to only log in to your application if it’s the first iteration.
// Submit the login form but only if it's the first iteration of this bot
if (user.getIteration() === 0) {
// Grab the next row from the Account dataset
const account = user.getVariable("Account");
// Send a POST to the login endpoint
http.post(
"https://petstore.loadster.app/api/login",
{
username: account[0],
password: account[1]
},
{
validators: [
function (response) {
const body = JSON.parse(response.string());
// Capture the resulting API token
// and store for later use by this bot
user.setVariable("ApiToken", body.token);
}
]
}
);
}
The important part here is user.getIteration() === 0
which checks if we’re currently on the first iteration for this
bot. In this example, we’re only logging in on the first iteration, and then saving the API token for use by
subsequent steps in this iteration as well as subsequent iterations of this same bot.
Cookies are also kept in scope automatically if there are any.
In the case of Browser Bots, the cache is retained between iterations when this setting is disabled, which can have an effect on page load times for subsequent iterations of the same bot.