-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update example using the read and seek methods
- Loading branch information
Showing
1 changed file
with
35 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |