Opens a file, reading all its contents into memory for use in the script.
Use SharedArray for CSV and JSON files
open() often consumes a large amount of memory because every VU keeps a separate copy of the file in memory. To reduce the memory consumption, we strongly advise the usage of SharedArray for CSV, JSON and other files intended for script parametrization.
warningFunction only available in "init context"
This is a function that can only be called from the init context (aka init code), code in the global context that is, outside of the main export default function { ... }.
By restricting it to the init context, we can easily determine what local files are needed to run the test and thus what we need to bundle up when distributing the test to multiple nodes in a clustered/distributed test.
See the example further down on this page. For a more in-depth description, see Running k6.
Breaking change in v0.36.0
Since k6 v0.36.0, VUs are now restricted to only open() files that were also opened in the init context of the first VU - the one that was initialized to get the exported options from the JS script (__VU==0). This means that the code like if (__VU > 0) { const arr = open("./arr.json"); } will result in an error.
Parameter | Type | Description |
---|---|---|
filePath | string | The path to the file, absolute or relative, that will be read into memory. The file will only be loaded once, even when running with several VUs. |
mode | string | By default, the contents of the file are read as text, but if you specify b, the file will be read as binary data instead. |
Returns
Type | Description |
---|---|
string / ArrayBuffer | The contents of the file, returned as string or ArrayBuffer (if b was specified as the mode). |
Breaking change in v0.32.0
Since k6 v0.32.0 open(..., 'b') returns an ArrayBuffer object instead of an array of numbers (bytes). If you need to manipulate the binary data, you'll need to wrap the ArrayBuffer object in a typed array view.