Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document k6/experimental/fs module #1419

Merged
merged 11 commits into from
Nov 29, 2023
126 changes: 63 additions & 63 deletions docs/sources/next/javascript-api/_index.md

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions docs/sources/next/javascript-api/k6-experimental/_index.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
---
title: "k6/experimental"
excerpt: "k6 experimental APIs"
title: 'k6/experimental'
description: 'k6 experimental APIs'
weight: 07
---

# k6/experimental

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

| Modules | Description |
| --------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
| Modules | Description |
| ------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ |
| [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. |
| [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. |
| [grpc](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/grpc) | Extends `k6/net/grpc` with the streaming capabilities. |
| [redis](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis) | Functionality to interact with [Redis](https://redis.io/). |
| [timers](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/timers) | `setTimeout`, `clearTimeout`, `setInterval`, `clearInterval` |
| [tracing](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/tracing) | Support for instrumenting HTTP requests with tracing information. |
| [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). |
| [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). |
| [grpc](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/grpc) | Extends `k6/net/grpc` with the streaming capabilities. |
39 changes: 39 additions & 0 deletions docs/sources/next/javascript-api/k6-experimental/fs/FileInfo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
title: 'FileInfo'
description: 'FileInfo represents information about a file.'
weight: 30
---

# FileInfo

The `FileInfo` class represents information about a [file](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/fs/file).

## Properties

| Property | Type | Description |
| :------- | :----- | :----------------------------- |
| name | string | The name of the file. |
| size | number | The size of the file in bytes. |

## Example

{{< code >}}

```javascript
import { open, SeekMode } from 'k6/experimental/fs';

let file;
(async function () {
file = await open('bonjour.txt');
})();

export default async function () {
// Retrieve information about the file
const fileinfo = await file.stat();
if (fileinfo.name != 'bonjour.txt') {
throw new Error('Unexpected file name');
}
}
```

{{< /code >}}
43 changes: 43 additions & 0 deletions docs/sources/next/javascript-api/k6-experimental/fs/SeekMode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
title: 'SeekMode'
description: 'SeekMode is used to specify the position from which to seek in a file.'
weight: 40
---

# SeekMode

The `SeekMode` enum specifies the position from which to [seek](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/fs/file/seek) in a file.

## Members

| Member | Value | Description |
| :------ | :---- | :------------------------------------------ |
| Start | 0 | Seek from the start of the file. |
| Current | 1 | Seek from the current position in the file. |
| End | 2 | Seek from the end of the file. |

## Example

{{< code >}}

```javascript
import { open, SeekMode } from 'k6/experimental/fs';

let file;
(async function () {
file = await open('bonjour.txt');
})();

export default async function () {
// Seek 6 bytes from the start of the file
await file.seek(6, SeekMode.Start);

// Seek 2 more bytes from the current position
await file.seek(2, SeekMode.Current);

// Seek backwards 2 bytes from the end of the file
await file.seek(-2, SeekMode.End);
}
```

{{< /code >}}
84 changes: 84 additions & 0 deletions docs/sources/next/javascript-api/k6-experimental/fs/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
title: 'fs'
description: 'k6 fs experimental API'
weight: 10
---

# fs

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

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.

### Memory efficiency

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.

### Notes on usage

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.

## API Overview

The module exports functions and objects to interact with the file system:

| Function/Object | Description |
| ----------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| [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. |
| [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. |
| [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`. |

## Example

{{< code >}}

```javascript
import { open, SeekMode } from 'k6/experimental/fs';

// k6 doesn't support async in the init context. We use a top-level async function for `await`.
//
// Each Virtual User gets its own `file` copy.
// So, operations like `seek` or `read` won't impact other VUs.
let file;
(async function () {
file = await open('bonjour.txt');
})();

export default async function () {
// About information about the file
const fileinfo = await file.stat();
if (fileinfo.name != 'bonjour.txt') {
throw new Error('Unexpected file name');
}

const buffer = new Uint8Array(4);

let totalBytesRead = 0;
while (true) {
// Read into the buffer
const bytesRead = await file.read(buffer);
if (bytesRead == null) {
// EOF
break;
}

// Do something useful with the content of the buffer
totalBytesRead += bytesRead;

// If bytesRead is less than the buffer size, we've read the whole file
if (bytesRead < buffer.byteLength) {
break;
}
}

// Check that we read the expected number of bytes
if (totalBytesRead != fileinfo.size) {
throw new Error('Unexpected number of bytes read');
}

// Seek back to the beginning of the file
await file.seek(0, SeekMode.Start);
}
```

{{< /code >}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
title: 'File'
description: 'File represents a file with methods for reading, seeking, and obtaining file stats.'
weight: 10
---

# File

The `File` class represents a file with methods for reading, seeking, and obtaining file stats. It's returned by the [open](https://grafana.com/docs/k6/latest/javascript-api/init-context/open/) function.

## Properties

| Property | Type | Description |
| :------- | :----- | :----------------------------- |
| path | string | The absolute path to the file. |

## Methods

| Method | Description |
| :------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------- |
| [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. |
| [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. |
| [stat](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/fs/file/stat) | Returns a promise resolving to a [FileInfo](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/fs/fileinfo/) object with information about the file. |

## Example

{{< code >}}

```javascript
import { open, SeekMode } from 'k6/experimental/fs';

let file;
(async function () {
file = await open('bonjour.txt');
})();

export default async function () {
// About information about the file
const fileinfo = await file.stat();
if (fileinfo.name != 'bonjour.txt') {
throw new Error('Unexpected file name');
}

const buffer = new Uint8Array(4);

let totalBytesRead = 0;
while (true) {
// Read into the buffer
const bytesRead = await file.read(buffer);
if (bytesRead == null) {
// EOF
break;
}

// Do something useful with the content of the buffer
totalBytesRead += bytesRead;

// If bytesRead is less than the buffer size, we've read the whole file
if (bytesRead < buffer.byteLength) {
break;
}
}

// Check that we read the expected number of bytes
if (totalBytesRead != fileinfo.size) {
throw new Error('Unexpected number of bytes read');
}

// Seek back to the beginning of the file
await file.seek(0, SeekMode.Start);
}
```

{{< /code >}}
oleiade marked this conversation as resolved.
Show resolved Hide resolved
109 changes: 109 additions & 0 deletions docs/sources/next/javascript-api/k6-experimental/fs/file/read.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
---
title: 'read'
description: 'the read method is used to read a chunk of the file.'
weight: 20
---

# read

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.

It resolves to either the number of bytes read during the operation, or `null` if there was nothing more to read.

## Parameters

| Parameter | Type | Description |
| :-------- | :-------------------------------------------------------------------------------------------------------- | :-------------------------------- |
| buffer | [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) | The buffer to read the data into. |

## Returns

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.

## Example

### Reading a file

In the following example, we open a file and read it in chunks of 128 bytes until we reach the end of the file.

{{< code >}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just as a comment, the {{< code >}} shortcode main purpose is to support multiple code snippets (how to do X in JavaScript, and in Ruby, for example). It still works if you have a single code snippet inside of it, but we don't necessarily need it here. 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's good to know! I think it still makes sense to have it even with a single example? Makes it more explicit too as it will display "Javascript" above the code right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still get the "JavaScript" title even without the code shortcode. (I wasn't sure about it so I tested it out 😆). The only UI difference seems to be the underline in the shortcode version.

Screenshot 2023-11-28 at 2 29 02 PM


```javascript
import { open, SeekMode } from 'k6/experimental/fs';

let file;
(async function () {
file = await open('bonjour.txt');
})();

export default async function () {
const buffer = new Uint8Array(128);

let totalBytesRead = 0;
while (true) {
// Read into the buffer
const bytesRead = await file.read(buffer);
if (bytesRead == null) {
// EOF
break;
}

// Do something useful with the content of the buffer
totalBytesRead += bytesRead;

// If bytesRead is less than the buffer size, we've read the whole file
if (bytesRead < buffer.byteLength) {
break;
}
}

// Seek back to the beginning of the file
await file.seek(0, SeekMode.Start);
}
```

{{< /code >}}

### `readAll` helper function

The following helper function can be used to read the entire contents of a file into a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) buffer.

{{< code >}}

```javascript
import { open, SeekMode } from 'k6/experimental/fs';

let file;
(async function () {
file = await open('bonjour.txt');
})();

async function readAll(file) {
const fileInfo = await file.stat();
const buffer = new Uint8Array(fileInfo.size);

const bytesRead = await file.read(buffer);
if (bytesRead !== fileInfo.size) {
throw new Error(
'unexpected number of bytes read; expected ' +
fileInfo.size +
' but got ' +
bytesRead +
' bytes'
);
}

oleiade marked this conversation as resolved.
Show resolved Hide resolved
return buffer;
}

export default async function () {
// Read the whole file
const fileContent = await readAll(file);
console.log(JSON.stringify(fileContent));

// Seek back to the beginning of the file
await file.seek(0, SeekMode.Start);
oleiade marked this conversation as resolved.
Show resolved Hide resolved
}
```

{{< /code >}}
Loading
Loading