k6.io引入util包TextDecoder读二进制文件报错及解决
k6测试中读取二进制文件的问题与解决
我在使用k6.io做性能测试时,想要通过导入util包的TextDecoder来读取二进制文件,编写的测试脚本如下:
import { sleep, check } from 'k6'; import { Options } from 'k6/options'; import http from 'k6/http'; /* @ts-ignore */ import { TextDecoder } from 'util'; export const options:Options = { vus: 1, iterations: 1, }; const file = open('uuids.txt', 'b'); export function setup() { console.log("File is " + file); let enc = new TextDecoder("utf-8") console.log("Decode: " + enc.decode(file)); } export default () => { .....post HTTP Request }
我已经执行npm install util --save-dev安装了util依赖,对应的package.json配置如下:
{ "name": "typescript", "version": "1.0.0", "main": "index.js", "repository": "ssh://git@github.com/k6io/example-typescript.git", "author": "Simon Aronsson <simme@k6.io>", "license": "MIT", "devDependencies": { "@babel/core": "7.13.16", "@babel/plugin-proposal-class-properties": "7.13.0", "@babel/plugin-proposal-object-rest-spread": "7.13.8", "@babel/preset-env": "7.13.15", "@babel/preset-typescript": "7.13.0", "@types/k6": "~0.41.0", "@types/node": "^20.4.5", "@types/webpack": "5.28.0", "babel-loader": "8.2.2", "clean-webpack-plugin": "4.0.0-alpha.0", "copy-webpack-plugin": "^9.0.1", "typescript": "4.2.4", "util": "^0.12.5", "webpack": "5.61.0", "webpack-cli": "4.6.0", "webpack-glob-entries": "^1.0.1" }, "scripts": { "start": "webpack" }, "browser": { "fs": false, "path": false, "os": false } }
但执行k6 run dist\post-201-create-node-test.js命令时,出现util包未解析的错误,报错信息如下:
k6 run dist\post-201-create-node-test.js /\ |‾‾| /‾‾/ /‾‾/ /\ / \ | |/ / / / / \/ \ | ( / ‾‾\ / \ | |\ \ | (‾) | / __________ \ |__| \__\ \_____/ .io ERRO[0000] ReferenceError: process is not defined at 539 (webpack://typescript/./node_modules/util/util.js:109:0(78)) at __webpack_require__ (webpack://typescript/webpack/bootstrap:19:0(26)) at webpack://typescript/external%20commonjs%20%22https://jslib.k6.io/k6-utils/1.4.0/index.js%22:1:34(47) at file:///C:/Dev//dist/post-201-create-node-test.js:2323:3(71) at file:///C:/Dev/dist/post-201-create-node-test.js:2328:12(3) hint="script exception"
最终我通过SharedArray读取JSON格式的测试数据解决了该问题,代码如下:
const data = new SharedArray('some data name', function () { return JSON.parse(open('./data.json')).uuids; });
内容的提问来源于stack exchange,提问作者27P
相关产品推荐
相关产品推荐

