Menu
Choose a product
Viewing: v0.48.x
Find another version
Scroll for more
Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.
Open source
RSS
utils
The utils module contains number of small utility functions useful in every day load testing.
⭐️ Source code available on GitHub. Please request features and report bugs through GitHub issues.
| Function | Description |
|---|---|
| randomIntBetween(min, max) | Random integer in a given range |
| randomItem(array) | Random item from a given array |
| randomString(length, [charset]) | Random string of a given length, optionally selected from a custom character set |
| uuidv4() | Random UUID v4 in a string representation |
| findBetween(content, left, right, [repeat]) | Extract a string between two surrounding strings |
| normalDistributionStages(maxVUs, durationSeconds, [numberOfStages]) | Creates stages which will produce a normal distribution (bell-curve) of VUs for a test |
| getCurrentStageIndex | Get the index of the running stage as defined in the stages array options. It can be used only with the executors that support the stages option as
ramping-vus or
ramping-arrival-rate. |
| tagWithCurrentStageIndex | Tag all the generated metrics in the iteration with the index of the current running stage. |
| tagWithCurrentStageProfile | Tag all the generated metrics in the iteration with the computed profile for the current running stage. |
Simple example
JavaScript
JavaScript
import { sleep } from 'k6';
import http from 'k6/http';
import {
randomIntBetween,
randomString,
randomItem,
uuidv4,
findBetween,
} from 'https://jslib.k6.io/k6-utils/1.4.0/index.js';
export default function () {
const res = http.post(`https://test-api.k6.io/user/register/`, {
first_name: randomItem(['Joe', 'Jane']), // random name
last_name: `Jon${randomString(1, 'aeiou')}s`, //random character from given list
username: `user_${randomString(10)}@example.com`, // random email address,
password: uuidv4(), // random password in form of uuid
});
// find a string between two strings to grab the username:
const username = findBetween(res.body, '"username":"', '"');
console.log('username from response: ' + username);
sleep(randomIntBetween(1, 5)); // sleep between 1 and 5 seconds.
}Was this page helpful?
Related resources from Grafana Labs
Additional helpful documentation, links, and articles:
Video

Performance testing and observability in Grafana Cloud
Optimize user experiences with Grafana Cloud. Learn real-time insights, performance testing with k6, and continuous validation with Synthetic Monitoring.
Events

User-centered observability: load testing, real user monitoring, and synthetics
Learn how to use load testing, synthetic monitoring, and real user monitoring (RUM) to understand end users' experience of your apps. Watch on demand.