|
| 1 | +--- |
| 2 | +title: 'fs' |
| 3 | +description: 'k6 fs experimental API' |
| 4 | +weight: 10 |
| 5 | +--- |
| 6 | + |
| 7 | +# fs |
| 8 | + |
| 9 | +{{< docs/shared source="k6" lookup="experimental-module.md" version="<K6_VERSION>" >}} |
| 10 | + |
| 11 | +The k6 filesystem experimental module provides a memory-efficient way to handle file interactions within your test scripts. It currently offers support for opening files, reading their content, seeking through their content, and retrieving metadata about them. |
| 12 | + |
| 13 | +### Memory efficiency |
| 14 | + |
| 15 | +One of the key advantages of the filesystem module is its memory efficiency. Unlike the traditional [open](https://grafana.com/docs/k6/latest/javascript-api/init-context/open/) function, which loads a file multiple times into memory, the filesystem module reduces memory usage by loading the file as little as possible and sharing the same memory space between all VUs. This approach reduces the risk of encountering out-of-memory errors, especially in load tests involving large files. |
| 16 | + |
| 17 | +### Notes on usage |
| 18 | + |
| 19 | +An important consideration when using the filesystem module is its handling of external file modifications. Once k6 loads a file, it behaves like a "view" over its contents. If you modify the underlying file during a test, k6 will not reflect those changes in the loaded [File](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/fs/file/) instance. |
| 20 | + |
| 21 | +## API Overview |
| 22 | + |
| 23 | +The module exports functions and objects to interact with the file system: |
| 24 | + |
| 25 | +| Function/Object | Description | |
| 26 | +| ----------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | |
| 27 | +| [open](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/fs/open) | Opens a file and returns a promise resolving to a `File` instance. | |
| 28 | +| [File](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/fs/file) | Represents a file with methods for reading, seeking, and obtaining file stats. | |
| 29 | +| [SeekMode](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/fs/seekmode) | Enum for specifying the reference point for seek operations. Includes `Start`, `Current`, and `End`. | |
| 30 | + |
| 31 | +## Example |
| 32 | + |
| 33 | +{{< code >}} |
| 34 | + |
| 35 | +```javascript |
| 36 | +import { open, SeekMode } from 'k6/experimental/fs'; |
| 37 | + |
| 38 | +// k6 doesn't support async in the init context. We use a top-level async function for `await`. |
| 39 | +// |
| 40 | +// Each Virtual User gets its own `file` copy. |
| 41 | +// So, operations like `seek` or `read` won't impact other VUs. |
| 42 | +let file; |
| 43 | +(async function () { |
| 44 | + file = await open('bonjour.txt'); |
| 45 | +})(); |
| 46 | + |
| 47 | +export default async function () { |
| 48 | + // About information about the file |
| 49 | + const fileinfo = await file.stat(); |
| 50 | + if (fileinfo.name != 'bonjour.txt') { |
| 51 | + throw new Error('Unexpected file name'); |
| 52 | + } |
| 53 | + |
| 54 | + const buffer = new Uint8Array(4); |
| 55 | + |
| 56 | + let totalBytesRead = 0; |
| 57 | + while (true) { |
| 58 | + // Read into the buffer |
| 59 | + const bytesRead = await file.read(buffer); |
| 60 | + if (bytesRead == null) { |
| 61 | + // EOF |
| 62 | + break; |
| 63 | + } |
| 64 | + |
| 65 | + // Do something useful with the content of the buffer |
| 66 | + totalBytesRead += bytesRead; |
| 67 | + |
| 68 | + // If bytesRead is less than the buffer size, we've read the whole file |
| 69 | + if (bytesRead < buffer.byteLength) { |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Check that we read the expected number of bytes |
| 75 | + if (totalBytesRead != fileinfo.size) { |
| 76 | + throw new Error('Unexpected number of bytes read'); |
| 77 | + } |
| 78 | + |
| 79 | + // Seek back to the beginning of the file |
| 80 | + await file.seek(0, SeekMode.Start); |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +{{< /code >}} |
0 commit comments