Help build the future of open source observability software Open positions

Check out the open source projects we support Downloads

We cannot remember your choice unless you click the consent notice at the bottom.

New Grafana k6 features: TypeScript support, async APIs for browser, and more

New Grafana k6 features: TypeScript support, async APIs for browser, and more

2024-08-19 6 min

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:

JavaScript
import { browser } from 'k6/experimental/browser';

export default async function () {
    const page = browser.newPage();    
    ...
}

Should be updated to:

JavaScript
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:

JavaScript
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.

JavaScript
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:

JavaScript
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.

Thank you to the Grafana k6 community!

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!