No results for ""

Powered byAlgolia

Modules

Because we believe that load testing sophisticated applications requires advanced scripting capabilities, k6 supports powerful modules feature allowing you to:

  • modularize your code.
  • reuse existing JavaScript libraries.

Besides built-in modules, k6 can load ES6 modules and ES5 libraries.

ES6 Modules

With support for ECMAScript 2015 features, k6 allows you to create your own modules using the ES6 modules syntax.

import http from 'k6/http';
import print from './lib/print.js';

export default function() {
  http.get('http://test.k6.io/');
  print();
}

Importing JavaScript libraries

The k6 import statement automatically loads the module.exports object of the required module. Therefore, your k6 tests can import bundled JavaScript libraries via module.exports.

import http from 'k6/http';
import moment from './vendor/moment.js';

export default function() {
  http.get('http://test.k6.io/');
  console.log(moment().format());
}

⚠️ Info

k6 does not run in a browser environment. Due to this, libraries depending on browser APIs won't work.

NPM Modules

Even though k6 does not run in Node.js, it can also load bundled npm modules with browserify.

Run browserify with the standalone option to generate a Universal Module Definition bundle. It will create a package containing all the dependencies, and make the module available via module.exports. The new package could be imported in a k6 test.

-- bundle `cheerio` npm module
$ git clone git@github.com:cheeriojs/cheerio.git
$ npm install
$ browserify index.js -s cheerio > cheerio.js

-- bundle a xml converter
$ git clone git@github.com:Leonidas-from-XIV/node-xml2js.git
$ npm install
$ browserify lib/xml2js.js -s xml2js > xml2js.js
import http from 'k6/http';
import { check } from 'k6';
import cheerio from './vendor/cheerio.js';
import xml2js from './vendor/xml2js.js';

export default function() {
  const res = http.get('https://k6.io/');
  const $ = cheerio.load(res.body);
  const title = $('head title').text();
  check(title, {
    'has correct title': () =>
      title == 'Load testing for dev teams | k6',
  });
  var xmlString =
    '<?xml version="1.0" ?>' +
    '<items xmlns="http://foo.com">' +
    ' <item>Foo</item>' +
    ' <item color="green">Bar</item>' +
    '</items>';
  xml2js.parseString(xmlString, (_, result) => {
    console.log(JSON.stringify(result));
  });
}

If Browserify does not work for a particular npm package, check out the Browserify compatibility information or the npm author/s.

Remote modules

k6 can also import modules hosted remotely from anywhere.

import http from 'k6/http';
import moment from 's3.amazonaws.com/k6samples/moment.js';

export default function() {
  http.get('http://test.k6.io/');
  console.log(moment().format());
}

Additionally, it can load modules from hosting services like Github and CDNJS.

import http from 'k6/http';
import moment from 'cdnjs.com/libraries/moment.js/2.18.1';

export default function() {
  http.get('http://test.k6.io/');
  console.log(moment().format());
}

Limitations

Be aware that each Virtual User loads the required modules into memory during test execution. RAM usage is directly related to the number of running Virtual Users and the size of your JS objects. An excessive use of RAM can negatively impact the machine performance and the test execution.

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
import { hello_world } from './modules/module.js';

export default function() {
  hello_world();
}
export function hello_world() {
  console.log('Hello world');
}

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 -v /home/k6/example/src:/src -i loadimpact/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