No results for

Powered byAlgolia

Importing modules

It's common to import modules, or parts of modules, to use in your test scripts. In k6, you can import three different kinds of modules:

Built-in modules

k6 provides many built-in modules for core functionalities. For example, the http client make requests against the system under test. For the full list of built-in modules, refer to the API documentation.

import http from 'k6/http';

Local filesystem modules

These modules are stored on the local filesystem, and accessed either through relative or absolute filesystem paths. To make local filesystem modules compatible with k6, the module itself may use only relative or absolute filesystem imports to access its dependencies.

//helpers.js
export function someHelper() {
// ...
}
//my-test.js
import { someHelper } from './helpers.js';
export default function () {
someHelper();
}

Remote HTTP(S) modules

These modules are accessed over HTTP(S), from a source like the k6 JSLib or from any publicly accessible web server. The imported modules are downloaded and executed at runtime, making it extremely important to make sure you trust the code before including it in a test script.

import { randomItem } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';
export default function () {
randomItem();
}

The JSLib repository

JSLib is a set of libraries known to work well with k6. It is available at https://jslib.k6.io/.

These libraries can either be downloaded and included with the test project or loaded directly using HTTP imports.

Bundling node modules

attention

k6 is not NodeJS, nor is it a browser. Packages that rely on APIs provided by NodeJS, for instance the os and fs modules, will not work in k6. The same goes for browser-specific APIs like the window object.

The steps of this tutorial are as follows:

  1. Setting up the bundler
    1. Installing packages
    2. Configuring Webpack
    3. Adding a bundle command
  2. Running the bundling
  3. Running the tests

k6-es6 starter

A ready-to-use Webpack and Babel starter is avaialble in a repository.

In a JavaScript project running NodeJS, modules are imported using either import or require(), using the node module resolution algorithm. This means that the developer can import modules by name, without providing the full filesystem path to the module. For instance:

import { ClassInAModule } from 'cool-module';

would be automatically resolved by the node resolution algorithm by searching:

  • The current directory
  • Any node_modules folder in the directory
  • Any node_modules folder in a parent directory, up to the closest package.json file.

As the implementation of import in k6 lacks support for the node module resolution algorithm, node modules that resolve external dependencies will first need to be transformed into a self-contained, isolated, bundle.

This is done with the help of a bundling tool, like Webpack, which analyses the test script, identifies all external dependencies, and then continues to create a self-contained bundle including everything necessary to run the script.

If the test script has no external dependencies, already has them vendored in a k6 compatible way, or only uses ES5.1+ features, using a bundler will not be necessary.

Picking a bundler

It is possible to use any bundler that supports transpilation. Popular ones include, but are not limited to, webpack, parcel, rollup and browserify.

Due to its flexibility, ease of use, relatively low resource consumption, and known compatibility with k6, it is recommended to use webpack unless you have a specific reason to choose something else.

Things to consider

In general, all external modules added to a test project have a negative impact on performance, as they further increase the memory footprint and CPU usage.

Usually, this is not a big problem as each application only allocates these resources once. In k6, however, every VU has a separate JavaScript virtual machine (VM), duplicating the resource usage once each.

By running code requiring additional features on top of ES5.1, we also need additional extensions to the JavaScript VM, further boosting the resource usage. This is the default mode of k6.

When bundling using the configuration described in this article, babel and corejs automatically adds the features needed, thus allowing us to run our script without these extensions, using --compatibility-mode=base. For more details on the performance benefits of running in the base compatibility mode, see this article.

Setting up the bundler

Setting up a Babel and Webpack project from scratch might sound like a big undertaking, but is usually accomplished within minutes. Start by creating a project folder and initializing npm:

$ mkdir ./example-project && \
cd "$_" && \
npm init -y

Installing packages

Then, install the packages needed:

$ npm install --save-dev \
webpack \
webpack-cli \
@types/k6 \
babel-loader \
@babel/core \
@babel/preset-env \
core-js
PackageUsage
webpackThe bundler part of Webpack
webpack-cliThe CLI part of Webpack, which allows us to use it from the terminal
@types/k6k6 Typescript definition
babel-loaderA loader used by Webpack to leverage babel functionality while bundling
@babel/coreThe core functionality of Babel
@babel/preset-envA smart preset using browserlist, compat-table and electron-to-chromium to determine what code to transpile and polyfill.
core-jsA modular standard library for JS including polyfills

Configuring Webpack

Once these packages have been added, the next step will be to set up a webpack.config.js file:

1const path = require('path');
2
3module.exports = {
4 mode: 'production',
5 entry: {
6 login: './src/login.test.js',
7 signup: './src/signup.test.js',
8 },
9 output: {
10 path: path.resolve(__dirname, 'dist'), // eslint-disable-line
11 libraryTarget: 'commonjs',
12 filename: '[name].bundle.js',
13 },
14 module: {
15 rules: [{ test: /\.js$/, use: 'babel-loader' }],
16 },
17 target: 'web',
18 externals: /k6(\/.*)?/,
19};

Mode

Tells Webpack to automatically use the optimizations associated with the mode. Additional details available in the webpack docs.

Entry

The files Webpack will use as its entry points while performing the bundling. From these points, Webpack will automatically traverse all imports recursively until every possible dependency path has been exhausted. For instance:

1// login.test.js
2
3import { SomeService } from './some.service.js';
4
5const svc = new SomeService();

and

1// some.service.js
2
3import * as lodash from 'lodash';
4
5export class SomeService {
6 constructor() {
7 this._ = lodash;
8 }
9}

would result in Webpack bundling login.test.js, some.service.js and all upstream dependencies utilized by lodash.

Output

The path key takes an absolute path which is where the finished bundle will be placed. In this example, path.resolve is used to concatenate __dirname and 'dist' into an absolute path.

The libraryTarget key configures how the library will be exposed. Setting it to commonjs will result in it being exported using module.exports. Additional details available in the Webpack docs.

The filename key, as the name suggests, configures the name of the finished bundles. In this example, the template string [name] is used to add a dynamic part to the output filename.

Adding a bundle command

Open the package.json file and add a new script entry, used for running the bundling process.

{
"name": "bundling-example",
"description": "",
"version": "0.1.0",
"private": true,
"scripts": {
+ "bundle": "webpack"
}
...
}

Running the bundling

Running webpack will now output two different test bundles, that may be executed independently:

$ npm run bundle
# ...
$ tree dist
dist
├── login.bundle.js
└── signup.bundle.js
0 directories, 2 files

Running the tests

$ npm run bundle
# ...
$ k6 run dist/login.bundle.js
# ...
$ npm run bundle
# ...
$ k6 run dist/signup.bundle.js \
--vus 10 \
--duration 10s
# ...

Using local modules with Docker

When running k6 in a Docker container you must make sure to mount the necessary folders from the host into the container, using Docker volumes, so that k6 can see all the JS modules it needs to import.

For example, say you have the following structure on your host machine:

  • /home/k6/example/src/index.js
  • /home/k6/example/src/modules/module.js
index.js
1import { hello_world } from './modules/module.js';
2
3export default function () {
4 hello_world();
5}
./modules/module.js
1export function hello_world() {
2 console.log('Hello world');
3}

To run index.js and make the modules available for import we execute the following Docker command with the /home/k6/example/src host folder mounted at /src in the container:

$ docker run --rm -v /home/k6/example/src:/src -i grafana/k6 run /src/index.js

Note that on Windows, you also need to make sure that your drive in question, say C:\, has been marked for sharing in the Docker settings:

Running k6 in docker on Windows

Read more