Can I set timers to measure key transactions in my test script?
Most of the time, Loadster automatically measures the important response times, without you having to do anything. When you run a load test these show up on the Response Times graph.
With Protocol Bots, the Response Times graph shows one series for each URL. For Browser Bots, the Response Times graph shows one series for each navigation change, which is essentially any action that causes the browser’s URL bar to change.
But occasionally, you might want to measure and report how long it takes to do other things too.
For example, you might have a 3-step login flow and you want to track a single measurement that tells how long it takes to perform all 3 steps successfully. Or you might have a browser script that clicks on an element and then waits for a spinner overlay, representing some background action, to disappear.
In these cases, you can track them as custom transactions using timers.
Setting custom transaction timers
In such cases you can set a custom timer in a code block to set custom timers. The duration of time between starting the timer and stopping it is reported on the Response Times graph.
The first way to start and stop a timer is by calling startTimer
and stopTimer
with the transaction name you want
to show up on the report. As soon as the timer is stopped the custom transaction is reported.
bot.startTimer("My Checkout Flow");
// Do some things here, in the same code block or as separate steps
bot.stopTimer("My Checkout Flow");
The second way is to run all the timed actions in a callback. With this approach, anything that happens in the callback function will be timed and reported as a transaction.
bot.timer("My Checkout Flow", () => {
// Do some things
});
Either of these approaches will cause “My Checkout Flow” to appear in the Response Times graph when you run a load test.
Checking transaction timings in your script
You might have a requirement to enforce a maximum transaction time in your script. In other words, if a key transaction takes longer than expected, you can throw an error from the script and consider it a failure.
This is easy because the timer functions return timings, in milliseconds. Here’s an example of evaluating the result of a timer and throwing an error if it took more than 2500 milliseconds.
const elapsedTime = bot.timer("Login Sequence", () => {
// TODO - perform the login
});
if (elapsedTime > 2500) {
throw new Error(`The login sequence took ${elapsedTime} ms!`);
}
Here’s another way to do the same thing, using the checkTimer
function. This can be called multiple times if you need
to keep checking the timer. It works as long as the timer hasn’t been stopped yet.
bot.startTimer("Login Sequence");
// TODO - perform the login
if (bot.checkTimer("Login Sequence") > 2500) {
throw new Error(`The login sequence took ${elapsedTime} ms!`);
}
The stopTimer
function also returns the duration in milliseconds since the timer was started, so you could use that
if you prefer.
bot.startTimer("Login Sequence");
// TODO - perform the login
const elapsedTime = bot.stopTimer("Login Sequence");
if (elapsedTime > 2500) {
throw new Error(`The login sequence took ${elapsedTime} ms!`);
}
Keep in mind that Loadster automatically reports individual request or page navigation timings, so in most cases you’ll only need to use timers in your script if you have a specific sequence of steps that you want to time in total, or if you need to enforce a time limit in your script as shown in the examples above.