javascriptjsonload-testingk6

How to run a k6 script with multiple payloads using fs module?


I'm trying to run a k6 script that uses multiple payloads stored in JSON files. I want to read these files using the fs module and send them as part of my HTTP requests. However, it seems that the fs module is not supported in k6.

I understand that k6 does not support the fs module. How can I achieve the same functionality in k6? Is there an alternative way to load multiple payloads from JSON files in k6?


Solution

  • You can use the open function in the init context. It's recommended to use open in combination with SharedArray to reduce memory consumption.

    import { SharedArray } from 'k6/data';
    
    const data = new SharedArray('users', function () {
      // this function *MUST* return an array:
      return JSON.parse(open('./users.json'));
    });
    
    export default function() {
      doSomething(data[0]);
    }
    

    The SharedArray can only handle arrays, as the name suggests. If your JSON file does not contain a top-level array, but something else, you have to wrap it in a singleton array (an array with a single item): return [JSON.parse(open(…))];.

    There's also an expiremental fs module