k6

A modern load testing tool, using Go and JavaScript

"like unit testing, for performance"

k6 is a modern load testing tool, building on Load Impact's years of experience. It provides a clean, approachable JavaScript scripting API, distributed and cloud execution, and orchestration via a REST API.

Get Started    Documentation

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.loadimpact.com/");
  		print();
	}
// this is an ES6 module
export default function() {
  console.log('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.loadimpact.com/");
  		console.log(moment().format());
	}

k6 does not run in a browser environment. Libraries depending on browser APIs will fail.

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://loadimpact.com/");

  const $ = cheerio.load(res.body);

  const title = $('head title').text();
  check(title, {
    "has correct title": () => title == 'Performance testing for DevOps | Load Impact'
  });


  var xmlString = '<?xml version="1.0" ?>' +
   	'<items xmlns="http://foo.com">' +
   ' <item>Foo</item>' +
   ' <item color="green">Bar</item>' +
   '</items>'

  xml2js.parseString(xmlString, function (err, 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.loadimpact.com/");
	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.loadimpact.com/");
	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

Modules