k6 Data Parameterization Not Reusing Values Fix
In this tutorial, you'll learn about k6 data parameterization not reusing values fix. We cover key concepts, practical examples, and best practices.
Your k6 test runs out of unique data values midway — every VU reuses the same credentials, or the script throws RangeError: Invalid array index when accessing test data. SharedArray and open() must be used correctly for data parameterization.
The Problem
import http from 'k6/http';
const users = [
{ username: 'alice', password: 'pass1' },
{ username: 'bob', password: 'pass2' },
];
export default function () {
const user = users[__VU % users.length];
const res = http.post('https://api.example.com/login', JSON.stringify(user));
console.log(`VU ${__VU}: ${user.username} → ${res.status}`);
}
WARN[0002] Not enough VUs — 100 VUs share only 2 users.
Each user averages 50 logins. Credential reuse causes conflicts.
With only 2 users for 100 VUs, the same credentials are used 50 times each, causing rate limits or session conflicts.
Step-by-Step Fix
1. Use JSON file for data
[
{"username": "alice", "password": "pass1"},
{"username": "bob", "password": "pass2"},
{"username": "charlie", "password": "pass3"},
{"username": "diana", "password": "pass4"}
]
2. Load data with SharedArray
import http from 'k6/http';
import { SharedArray } from 'k6/data';
const users = new SharedArray('users', function () {
return JSON.parse(open('./users.json'));
});
export default function () {
const user = users[__VU % users.length];
const res = http.post('https://api.example.com/login', JSON.stringify(user));
console.log(`VU ${__VU}: ${user.username} → ${res.status}`);
}
SharedArray loads the file once and shares it across all VUs, saving memory.
3. Load CSV data
import http from 'k6/http';
import { SharedArray } from 'k6/data';
const csvData = new SharedArray('csv_data', function () {
const raw = open('./users.csv');
const lines = raw.split('\n').slice(1); // Skip header
return lines.map((line) => {
const [username, password] = line.split(',');
return { username: username.trim(), password: password.trim() };
});
});
export default function () {
const user = csvData[__VU % csvData.length];
const res = http.post('https://api.example.com/login', JSON.stringify(user));
}
4. Use unique values per iteration
import http from 'k6/http';
import { SharedArray } from 'k6/data';
const tokens = new SharedArray('tokens', function () {
return JSON.parse(open('./tokens.json'));
});
export default function () {
// Use __ITER for iteration-unique selection
const token = tokens[__ITER % tokens.length];
const res = http.get('https://api.example.com/protected', {
headers: { Authorization: `Bearer ${token}` },
});
}
5. Generate data dynamically with helper functions
import http from 'k6/http';
function generateUser(vu, iter) {
return {
username: `user_${vu}_${iter}`,
email: `user_${vu}_${iter}@example.com`,
};
}
export default function () {
const user = generateUser(__VU, __ITER);
const res = http.post('https://api.example.com/signup', JSON.stringify(user));
}
Expected output:
✓ status is 201
VU 1: user_1_0 → 201
VU 1: user_1_1 → 201
VU 2: user_2_0 → 201
Prevention Tips
- Use
SharedArrayfor all shared test data to avoid per-VU memory duplication - Use CSV or JSON files for data loading instead of inline arrays
- Ensure data set size is at least 2x the number of VUs
- Use unique values per VU+iter combination with
__VUand__ITER - Test data loading with
--dry-runbefore full execution
Common Mistakes with data parameterization
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
These mistakes appear frequently in real-world K6 code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro