open
open
函数打开一个文件并返回一个 Promise,该 Promise 解析为一个 File 实例。与将文件多次加载到内存中的传统 open 函数不同,filesystem 模块通过尽可能少地加载文件并与所有 VU 共享相同的内存空间来减少内存使用。这种方法降低了遇到内存不足错误的风险,尤其是在涉及大文件的负载测试中。
参数
参数 | 类型 | 描述 |
---|---|---|
path | string | 要打开的文件的路径。相对路径相对于 k6 脚本解析。 |
返回值
一个解析为 File 实例的 Promise。
示例
import { open } 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.
const 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');
}
console.log(JSON.stringify(fileinfo));
}