问题描述
我正在尝试在k6中使用javascript生成器和 yield
.
I am trying to use javascript generator and yield
in k6.
当我尝试运行脚本时出现此错误:
When I try to run the script I get this error:
是否可以使用 yield 在k6中?
Is it possible to use yield in k6?
推荐答案
不幸的是,k6使用的JavaScript VM本身不支持此功能( goja ).根据此评论,最终可能会支持生成器,但目前尚无计划.
Unfortunately this is not natively supported in the JavaScript VM used by k6 (goja). According to this comment generators might eventually be supported, but there's no current plans for it.
也就是说,您可以通过使用 template-es6 项目进行转换来解决此问题将您的脚本转换为带有Babel的ES5变体,该变体可以填充对生成器的支持.
That said, you can workaround this by using the template-es6 project to transform your script to an ES5 variant with Babel, which can polyfill support for generators.
-
首先在本地克隆template-es6 Git存储库.
First clone the template-es6 Git repo locally.
使用 yarn add
或 npm install
安装所有依赖项.
Install all dependencies with yarn add
or npm install
.
将 @ babel/plugin-transform-runtime
添加到 .babelrc
中的插件列表中.它应该看起来像这样:
Add @babel/plugin-transform-runtime
to the plugins list in the .babelrc
. It should look like this:
{
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "usage",
"corejs": 3
}
]
],
"plugins": [
"@babel/plugin-transform-runtime"
]
}
-
使用
yarn add -D @ babel/plugin-transform-runtime
或npm install --save-dev @ babel/plugin-transform-runtime 安装插件代码>.
修改 main.js
脚本并安装所需的其他任何依赖项.
Modify the main.js
script and install whatever other dependencies you need.
运行 npm run-script webpack
捆绑所有内容.
最后用k6运行脚本,其中 k6 run --compatibility-mode = base build/app.bundle.js
.您也可以在没有 --compatibility-mode=base
的情况下运行它,但由于它已经转换为 ES5 脚本,因此您可以避免由 k6 完成的额外转换,从而提高性能和内存使用率.
Finally run the script with k6 with k6 run --compatibility-mode=base build/app.bundle.js
.You can also run it without --compatibility-mode=base
, but since it's already transformed to an ES5 script you can avoid the extra transformation done by k6, which improves performance and memory usage.
是的,这并不像我们希望的那么简单,但是JavaScript开发人员应该熟悉它,并且我们希望将来能改善对这些功能的支持.
Yeah, this is not as straightforward as we'd like it to be, but it should be familiar to JavaScript developers and we hope to improve support of these features in the future.
这篇关于在K6中使用yield时出现SyntaxError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!