Skip to content

Commit

Permalink
Update example using the read and seek methods
Browse files Browse the repository at this point in the history
  • Loading branch information
oleiade committed Sep 27, 2023
1 parent c8c3626 commit 6d8dbb3
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions examples/experimental/fs/fs.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,52 @@
import { open } from "k6/experimental/fs";
import { open, SeekMode } from "k6/experimental/fs";

export const options = {
vus: 100,
iterations: 1000,
};

// As k6 does not support asynchronous code in the init context, yet, we need to
// use a top-level async function to be able to use the `await` keyword.
// 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);
}

0 comments on commit 6d8dbb3

Please sign in to comment.