Skip to content

Commit 6f4e675

Browse files
committed
Document k6/experimental/fs module
1 parent cba8df8 commit 6f4e675

File tree

10 files changed

+562
-67
lines changed

10 files changed

+562
-67
lines changed

docs/sources/next/javascript-api/_index.md

Lines changed: 63 additions & 63 deletions
Large diffs are not rendered by default.
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
---
2-
title: "k6/experimental"
3-
excerpt: "k6 experimental APIs"
2+
title: 'k6/experimental'
3+
excerpt: 'k6 experimental APIs'
44
weight: 07
55
---
66

77
# k6/experimental
88

99
{{< docs/shared source="k6" lookup="experimental-module.md" version="<K6_VERSION>" >}}
1010

11-
| Modules | Description |
12-
| --------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
11+
| Modules | Description |
12+
| ------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ |
1313
| [browser](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser) | Provides browser-level APIs to interact with browsers and collect frontend performance metrics as part of your k6 tests. |
1414
| [redis](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis) | Functionality to interact with [Redis](https://redis.io/). |
1515
| [timers](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/timers) | `setTimeout`, `clearTimeout`, `setInterval`, `clearInterval` |
1616
| [tracing](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/tracing) | Support for instrumenting HTTP requests with tracing information. |
1717
| [webcrypto](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/webcrypto) | Implements the [WebCrypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API). |
1818
| [websockets](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/websockets) | Implements the browser's [WebSocket API](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket). |
1919
| [grpc](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/grpc) | Extends `k6/net/grpc` with the streaming capabilities. |
20+
| [fs](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/fs) | Provides a memory-efficient way to handle file interactions within your test scripts. |
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
title: 'FileInfo'
3+
excerpt: 'FileInfo represents information about a file.'
4+
weight: 30
5+
---
6+
7+
# FileInfo
8+
9+
The `FileInfo` class represents information about a [file](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/fs/file).
10+
11+
## Properties
12+
13+
| Property | Type | Description |
14+
| :------- | :----- | :----------------------------- |
15+
| name | string | The name of the file. |
16+
| size | number | The size of the file in bytes. |
17+
18+
## Example
19+
20+
{{< code >}}
21+
22+
```javascript
23+
import { open, SeekMode } from 'k6/experimental/fs';
24+
25+
let file;
26+
(async function () {
27+
file = await open('bonjour.txt');
28+
})();
29+
30+
export default async function () {
31+
// About information about the file
32+
const fileinfo = await file.stat();
33+
if (fileinfo.name != 'bonjour.txt') {
34+
throw new Error('Unexpected file name');
35+
}
36+
37+
const buffer = new Uint8Array(4);
38+
39+
let totalBytesRead = 0;
40+
while (true) {
41+
// Read into the buffer
42+
const bytesRead = await file.read(buffer);
43+
if (bytesRead == null) {
44+
// EOF
45+
break;
46+
}
47+
48+
// Do something useful with the content of the buffer
49+
totalBytesRead += bytesRead;
50+
51+
// If bytesRead is less than the buffer size, we've read the whole file
52+
if (bytesRead < buffer.byteLength) {
53+
break;
54+
}
55+
}
56+
57+
// Check that we read the expected number of bytes
58+
if (totalBytesRead != fileinfo.size) {
59+
throw new Error('Unexpected number of bytes read');
60+
}
61+
62+
// Seek back to the beginning of the file
63+
await file.seek(0, SeekMode.Start);
64+
}
65+
```
66+
67+
{{< /code >}}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: 'SeekMode'
3+
excerpt: 'SeekMode is used to specify the position from which to seek in a file.'
4+
weight: 40
5+
---
6+
7+
# SeekMode
8+
9+
The `SeekMode` enum is used to specify the position from which to [seek](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/fs/file/seek) in a file.
10+
11+
## Members
12+
13+
| Member | Value | Description |
14+
| :------ | :---- | :------------------------------------------ |
15+
| Start | 0 | Seek from the start of the file. |
16+
| Current | 1 | Seek from the current position in the file. |
17+
| End | 2 | Seek from the end of the file. |
18+
19+
## Example
20+
21+
{{< code >}}
22+
23+
```javascript
24+
import { open, SeekMode } from 'k6/experimental/fs';
25+
26+
let file;
27+
(async function () {
28+
file = await open('bonjour.txt');
29+
})();
30+
31+
export default async function () {
32+
// Seek 6 bytes from the start of the file
33+
await file.seek(6, SeekMode.Start);
34+
35+
// Seek 2 more bytes from the current position
36+
await file.seek(2, SeekMode.Current);
37+
38+
// Seek backwards 2 bytes from the end of the file
39+
await file.seek(-2, SeekMode.End);
40+
}
41+
```
42+
43+
{{< /code >}}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
title: 'fs'
3+
excerpt: '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, and 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]() function, which loads a file multiple times into memory, the filesystem module reduces memory usage by loading the file as little possible, and sharing the same memory space between all VUs. This approach signally reduces the memory footprint of your test script, and lets you load and process large files without running out of memory.
16+
17+
### Notes on usage
18+
19+
An important consideration when using the filesystem module is its handling of external file modifications. Once a file is loaded in k6, it behaves like a "view" over the content of the file. This means that if the underlying file is modified externally during a test, those changes won't be reflected in the loaded [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 >}}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
title: 'File'
3+
excerpt: 'File represents a file with methods for reading, seeking, and obtaining file stats.'
4+
weight: 10
5+
weight: 10
6+
---
7+
8+
# File
9+
10+
The `File` class represents a file with methods for reading, seeking, and obtaining file stats. It's returned by the [open](link-to-open-doc) function.
11+
12+
## Properties
13+
14+
| Property | Type | Description |
15+
| :------- | :----- | :----------------------------- |
16+
| path | string | The absolute path to the file. |
17+
18+
## Methods
19+
20+
| Method | Description |
21+
| :------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------- |
22+
| [read](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/fs/file/read) | Reads up to `buffer.byteLength` bytes from the file into the passed `buffer`. Returns a promise resolving to the number of bytes read. |
23+
| [seek](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/fs/file/seek) | Sets the file position indicator for the file to the passed `offset` bytes. Returns a promise resolving to the new offset. |
24+
| [stat](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/fs/file/stat) | Returns a promise resolving to a [FileInfo](link-to-fileinfo-doc) object with information about the file. |
25+
26+
## Example
27+
28+
{{< code >}}
29+
30+
```javascript
31+
import { open, SeekMode } from 'k6/experimental/fs';
32+
33+
let file;
34+
(async function () {
35+
file = await open('bonjour.txt');
36+
})();
37+
38+
export default async function () {
39+
// About information about the file
40+
const fileinfo = await file.stat();
41+
if (fileinfo.name != 'bonjour.txt') {
42+
throw new Error('Unexpected file name');
43+
}
44+
45+
const buffer = new Uint8Array(4);
46+
47+
let totalBytesRead = 0;
48+
while (true) {
49+
// Read into the buffer
50+
const bytesRead = await file.read(buffer);
51+
if (bytesRead == null) {
52+
// EOF
53+
break;
54+
}
55+
56+
// Do something useful with the content of the buffer
57+
totalBytesRead += bytesRead;
58+
59+
// If bytesRead is less than the buffer size, we've read the whole file
60+
if (bytesRead < buffer.byteLength) {
61+
break;
62+
}
63+
}
64+
65+
// Check that we read the expected number of bytes
66+
if (totalBytesRead != fileinfo.size) {
67+
throw new Error('Unexpected number of bytes read');
68+
}
69+
70+
// Seek back to the beginning of the file
71+
await file.seek(0, SeekMode.Start);
72+
}
73+
```
74+
75+
{{< /code >}}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
title: 'read'
3+
excerpt: 'the read method is used to read a chunk of the file.'
4+
weight: 20
5+
---
6+
7+
# read
8+
9+
The `read` method is used to read a chunk of the file into an [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) buffer.
10+
11+
It resolves to either the number of bytes read during the operation, or `null` if there was nothing more to read.
12+
13+
## Parameters
14+
15+
| Parameter | Type | Description |
16+
| :-------- | :-------------------------------------------------------------------------------------------------------- | :-------------------------------- |
17+
| buffer | [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) | The buffer to read the data into. |
18+
19+
## Returns
20+
21+
A [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) resolving to the number of bytes read or `null` if the end of the file has been reached.
22+
23+
## Example
24+
25+
{{< code >}}
26+
27+
```javascript
28+
import { open, SeekMode } from 'k6/experimental/fs';
29+
30+
let file;
31+
(async function () {
32+
file = await open('bonjour.txt');
33+
})();
34+
35+
export default async function () {
36+
// About information about the file
37+
const fileinfo = await file.stat();
38+
if (fileinfo.name != 'bonjour.txt') {
39+
throw new Error('Unexpected file name');
40+
}
41+
42+
const buffer = new Uint8Array(4);
43+
44+
let totalBytesRead = 0;
45+
while (true) {
46+
// Read into the buffer
47+
const bytesRead = await file.read(buffer);
48+
if (bytesRead == null) {
49+
// EOF
50+
break;
51+
}
52+
53+
// Do something useful with the content of the buffer
54+
totalBytesRead += bytesRead;
55+
56+
// If bytesRead is less than the buffer size, we've read the whole file
57+
if (bytesRead < buffer.byteLength) {
58+
break;
59+
}
60+
}
61+
62+
// Check that we read the expected number of bytes
63+
if (totalBytesRead != fileinfo.size) {
64+
throw new Error('Unexpected number of bytes read');
65+
}
66+
67+
// Seek back to the beginning of the file
68+
await file.seek(0, SeekMode.Start);
69+
}
70+
```
71+
72+
{{< /code >}}

0 commit comments

Comments
 (0)