包阅导读总结
1. 关键词:Grafana k6、新功能、TypeScript 支持、浏览器 API、性能测试
2. 总结:Grafana k6 团队每两个月发布新版本,带来新功能提升用户体验。介绍了包括 TypeScript 支持、ES6+新特性、浏览器 API 异步化等更新,还提及 gRPC 模块、新的流模块等,k6 不断完善,支持更多输出方式。
3. 主要内容:
– TypeScript 支持
– v0.52 起无需 JavaScript 打包工具,可直接通过 CLI 运行 TypeScript 测试
– ES6+特性支持
– v0.52 引入缺失特性,v0.53 这些特性在默认兼容模式可用
– 浏览器 API 异步化
– v0.52 起支持异步操作和 async/await 关键字,浏览器模块稳定
– gRPC 流模块
– v0.51 起成为 k6 核心模块,支持非阻塞异步操作
– 新的流模块
– v0.51 起可高效处理大文件读取,减少内存占用
– 全局可用的定时器方法
– v0.51 起 setInterval 等方法全局可用
– 新的 Web Crypto 方法
– 持续添加对更多算法和格式的支持
– OpenTelemetry 后端存储结果
– v0.53 起添加为原生输出,可直接发送结果到后端
思维导图:
文章来源:grafana.com
作者:Pepe Cano
发布时间:2024/8/19 16:30
语言:英文
总字数:1116字
预计阅读时间:5分钟
评分:86分
标签:Grafana k6,TypeScript,异步 API,浏览器测试,gRPC 流
以下为原文内容
本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com
About every two months, the Grafana k6 team releases a new version of the open source load testing tool to deliver new features and further enhance the user experience.
In case you missed them, here’s a recap of recent k6 releases and some of the exciting updates they brought to our user base. Many of the features highlighted in this post relate to new web APIs that the community has been asking for, and that are widely used by JavaScript developers.
To get all the details about what’s new in Grafana k6, you can also check out our full k6 release notes.
TypeScript support
This was a big one for the k6 community: as of the Grafana k6 v0.52 release in June, you no longer need JavaScript bundlers like Rollup or Webpack to run your k6 tests in TypeScript. You can now run them directly from the k6 CLI by passing the compatibility-mode
option — just like any other k6 test.
k6 run script.ts --compatibility-mode=experimental_enhanced
Whether you prefer developing in TypeScript or want to reuse an existing TypeScript library in your k6 tests, this new mode makes working with TypeScript straightforward.
Native support for all ES6+ features
In addition to TypeScript, k6 v0.52 introduced support for previously missing features from ECMAScript 6 (ES6) and newer ECMAScript versions, such as optional chaining (?.), object spread, and private class fields.
As of k6 v0.53, released this month, these ES+ features are now available in the default compatibility mode. This means you can start using them without any additional setup, and makes k6 natively compliant with the ES6+ specification.
Browser APIs become fully async
When we initially released browser testing in 2021, k6 did not support asynchronous operations or the JavaScript’s async
and await
keywords. To align with the broader JavaScript ecosystem and ensure compatibility with Playwright, we made our browser APIs fully async with the k6 v0.52 release. This change reflects our ongoing effort to make the browser module more user-friendly and easier to use.
The trade-off with this update, however, was introducing breaking changes to your existing browser scripts. To help with this, we’ve provided a guide to migrate browser tests to k6 v0.52 that details the list of affected APIs and migration instructions.
For example:
import { browser } from 'k6/experimental/browser';export default async function () { const page = browser.newPage(); ...}
Should be updated to:
import { browser } from 'k6/browser';export default async function () { const page = await browser.newPage(); ...}
And, as part of this major change, the browser module has officially graduated 🎉🎓. This means it is now stable and considered a k6 core module, available as k6/browser
instead of k6/experimental/browser
.
gRPC streaming becomes a k6 core module
Speaking of graduations, grpc.Stream, which supports bi-directional gRPC streaming, is now fully integrated into the stable k6/net/grpc
module, as of the k6 v0.51 release in May. This means breaking changes are no longer expected, so you can now use the gRPC APIs and upgrade to future k6 versions without disruption.
Additionally, the gRPC module now supports non-blocking async operations with client.asyncInvoke, which returns a Promise. Here’s a basic example:
export default () => { client.connect('localhost:10000', { plaintext: true }); client.asyncInvoke('main.RouteGuide/GetFeature', data).then((res) => { console.log(res.message.name); client.close(); });};
New Stream module to consume large data efficiently
If you’ve ever had to run a load test that reads a large file, you may have run into Out of Memory (OOM) errors. While the SharedArray object helped us improve memory consumption, it still loads all the file content into memory.
For those needing to load large files (without experiencing those OOM issues), you can now read large files in small chunks with the Stream and fs modules, as of the k6 v0.51 release. The new Stream API can partially load large datasets, chunk by chunk, to significantly decrease memory usage.
To learn more, check out this example of reading a CSV file, line by line, with the Stream API.
Timer methods are now globally available
setInterval
, clearInterval
, setTimeout
, and clearTimeout
are commonly used functions to manage asynchronous operations in JavaScript. In k6, they work with the k6 event loop, and can be used to delay the execution of test code based on a set time or intervals.
In the Grafana k6 v0.51 release, these timer methods became globally available, as they are used in other libraries and implemented in other browser and JavaScript runtimes. This means it’s no longer necessary to import them from the k6/timers
or k6/experimental/timers
modules, as you had to before.
export default function () { setTimeout(...) setInterval(...) clearTimeout(...) clearInterval(...)}
New Web Crypto methods
In recent k6 releases, we’ve continued to add support for more Web Crypto algorithms and formats, making it easier to write scripts and test secure applications that use cryptographic operations. Some recent updates include:
const generatedKey = await crypto.subtle.generateKey({name: "AES-CBC", length: "256"}, true, [ "encrypt", "decrypt"]);const exportedKey = await crypto.subtle.exportKey("jwk", generatedKey);
The webcrypto module is still a work-in-progress. Refer to the xk6-webcrypto project for the list of supported APIs and to request new algorithms and key formats in k6.
Storing k6 results in OpenTelemetry backends
Since its early days, k6 has provided various options to send and store test results in real time, along with the flexibility to build your own k6 output.
OpenTelemetry (OTEL) has become an industry standard and one of the fastest-growing CNCF projects. In the k6 v0.53 OSS release, we’ve added OTEL as one of the native k6 outputs, enabling you to send k6 results directly to OpenTelemetry backends.
k6 run -o experimental-opentelemetry script.js
When you run this command, k6 sends test results to the default OpenTelemetry exporter endpoint (localhost:4318
). It converts k6 test metrics into their equivalent OTEL metrics and maps k6 tags to OTEL attributes.
For the full list of configuration options, refer to the OpenTelemetry k6 output documentation.
Every k6 release is a community effort, and we’d like to give a huge thank you to our users and contributors. We look forward to celebrating more contributor successes!
As always, we’d love to hear your feedback to shape the future of Grafana k6 together. Find k6 on GitHub and let us know what you think. Happy testing!
Download the latest version of Grafana k6 today, or get started with performance testing on Grafana Cloud, which has a generous forever-free tier that includes 500 Virtual User Hours (VUh) and plans for every use case. Sign up for free now!