The SDET Playbook

← All questions

How do you feed unique test data to k6 virtual users during performance runs without bottlenecks?

Asked Sep 28, 2026Viewed 0 times

1 Answer

Sign in to answer and to vote.

  • 0
    The SDET PlaybookSep 28, 2026

    Generate the data before the test, load it once, and have each iteration pick its own row. SharedArray loads a file once, in the init code, and shares one read-only copy between all VUs instead of giving every VU its own:

    import http from 'k6/http';
    import exec from 'k6/execution';
    import { SharedArray } from 'k6/data';
    
    // Init code: runs once, before the VUs start.
    const users = new SharedArray('users', () => JSON.parse(open('./users.json')));
    
    export default function () {
      // A different row for every iteration across the whole test, wrapping around.
      const user = users[exec.scenario.iterationInTest % users.length];
    
      http.post('https://api.example.com/v1/login', JSON.stringify({ email: user.email, password: user.password }), {
        headers: { 'Content-Type': 'application/json' },
      });
    }
    

    Use exec.vu.idInTest instead when each VU should keep one identity for the whole test. SharedArray can only be created in the init code, and you can only read from it afterward. Parse CSV files with the papaparse library inside the same callback.

    Small random values, such as a quantity or a search term, are fine to generate inside the iteration. Heavy work there, such as building large payloads or doing crypto, uses the CPU that should be generating load, which lowers throughput and distorts response times.

    Sources: k6: SharedArray, k6: k6/execution