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

Modify fs/streams/csv docs to reflect top-level await support #1798

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@ The `Options` object describes the configuration available for the operation of
import { open } from 'k6/experimental/fs';
import csv from 'k6/experimental/csv';

let file;
let parser;
(async function () {
file = await open('data.csv');
parser = new csv.Parser(file, {
delimiter: ',',
skipFirstLine: true,
fromLine: 2,
toLine: 8,
});
})();
const file = await open('data.csv');
const parser = new csv.Parser(file, {
delimiter: ',',
skipFirstLine: true,
fromLine: 2,
toLine: 8,
});

export default async function () {
// The `next` method attempts to read the next row from the CSV file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,8 @@ export const options = {
iterations: 10,
};

let file;
let parser;
(async function () {
file = await open('data.csv');
parser = new csv.Parser(file, { skipFirstLine: true });
})();
const file = await open('data.csv');
const parser = new csv.Parser(file, { skipFirstLine: true });

export default async function () {
// The `next` method attempts to read the next row from the CSV file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,11 @@ export const options = {
iterations: 10,
};

let file;
let csvRecords;
(async function () {
file = await open('data.csv');
const file = await open('data.csv');

// The `csv.parse` function consumes the entire file at once and returns
// the parsed records as a `SharedArray` object.
csvRecords = await csv.parse(file, { delimiter: ',' });
})();
// The `csv.parse` function consumes the entire file at once and returns
// the parsed records as a `SharedArray` object.
const csvRecords = await csv.parse(file, { delimiter: ',' });

export default async function () {
// `csvRecords` is a `SharedArray`. Each element is a record from the CSV file, represented as an array
Expand All @@ -85,12 +81,11 @@ export const options = {
iterations: 10,
};

let file;
let parser;
(async function () {
file = await open('data.csv');
parser = new csv.Parser(file);
})();
const file = await open('data.csv');

// The `csv.parse` function consumes the entire file at once and returns
// the parsed records as a `SharedArray` object.
const parser = new csv.Parser(file);

export default async function () {
// The parser `next` method attempts to read the next row from the CSV file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,11 @@ export const options = {
iterations: 10,
};

let file;
let csvRecords;
(async function () {
file = await open('data.csv');
const file = await open('data.csv');

// The `csv.parse` function consumes the entire file at once and returns
// the parsed records as a `SharedArray` object.
csvRecords = await csv.parse(file, { skipFirstLine: true });
})();
// The `csv.parse` function consumes the entire file at once and returns
// the parsed records as a `SharedArray` object.
const csvRecords = await csv.parse(file, { skipFirstLine: true });

export default async function () {
// `csvRecords` is a `SharedArray`. Each element is a record from the CSV file, represented as an array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ The `FileInfo` class represents information about a [file](https://grafana.com/d
```javascript
import { open, SeekMode } from 'k6/experimental/fs';

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

export default async function () {
// Retrieve information about the file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ The `SeekMode` enum specifies the position from which to [seek](https://grafana.
```javascript
import { open, SeekMode } from 'k6/experimental/fs';

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

export default async function () {
// Seek 6 bytes from the start of the file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,7 @@ The module exports functions and objects to interact with the file system:
```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');
})();
const file = await open('bonjour.txt');

export default async function () {
// Seek to the beginning of the file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ The `File` class represents a file with methods for reading, seeking, and obtain
```javascript
import { open, SeekMode } from 'k6/experimental/fs';

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

export default async function () {
// Seek to the beginning of the file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ In the following example, we open a file and read it in chunks of 128 bytes unti
```javascript
import { open, SeekMode } from 'k6/experimental/fs';

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

export default async function () {
// Seek to the beginning of the file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ A [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl
```javascript
import { open, SeekMode } from 'k6/experimental/fs';

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

export default async function () {
// Seek 6 bytes from the start of the file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ A [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Gl
```javascript
import { open, SeekMode } from 'k6/experimental/fs';

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

export default async function () {
// About information about the file
Expand Down
20 changes: 1 addition & 19 deletions docs/sources/k6/next/javascript-api/k6-experimental/fs/open.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,6 @@ weight: 20

The `open` function opens a file and returns a promise that resolves to a [File](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/fs/file) instance. Unlike the traditional [open](https://grafana.com/docs/k6/<K6_VERSION>/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 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.

### Asynchronous nature

It's important to note that `open` is asynchronous and returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). Due to k6's current limitation with the Init context (which doesn't support asynchronous functions directly), you need to use an asynchronous wrapper like this:

{{< code >}}

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

{{< /code >}}

## Parameters

| Parameter | Type | Description |
Expand All @@ -44,10 +29,7 @@ import { open } from 'k6/experimental/fs';
//
// 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');
})();
const file = await open('bonjour.txt');

export default async function () {
// About information about the file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ With the Streams API support in k6, you can start processing raw data with Javas

## API Overview

| Class | Description |
| Class | Description |
| ---------------------------------------------------------------------------------------------------------------- | ------------------------------------- |
| [ReadableStream](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/streams/readablestream) | Represents a readable stream of data. |

Expand All @@ -31,10 +31,7 @@ import { open } from 'k6/experimental/fs';
import { ReadableStream } from 'k6/experimental/streams';

// Open a csv file containing the data to be read
let file;
(async function () {
file = await open('./data.csv');
})();
const file = await open('./data.csv');

export default async function () {
let lineReaderState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ import { open } from 'k6/experimental/fs';
import { ReadableStream } from 'k6/experimental/streams';

// Open a csv file containing the data to be read
let file;
(async function () {
file = await open('./data.csv');
})();
const file = await open('./data.csv');

export default async function () {
let lineReaderState;
Expand Down
Loading