JS Libraries

Useful utility libs for k6 scripts

import { check } from "k6";
import { randomIntBetween, randomItem } from "https://jslib.k6.io/k6-utils/1.4.0/index.js";

export const options = {
  "duration": "10s",
  "vus": 1
};

export default function() {
  console.log(randomItem([1,2,3,4]));
  sleep(randomIntBetween(1,5)); // sleep between 1 and 5 seconds
}
 

Available libs

Name Version(s) Docs
k6-utils 1.0.0, 1.1.0, 1.2.0, 1.3.0, 1.4.0, 1.5.0, 1.6.0 https://grafana.com/docs/k6/latest/javascript-api/jslib/utils
k6-summary 0.0.1, 0.0.2, 0.0.3, 0.0.4, 0.1.0 https://github.com/grafana/k6-jslib-summary
jsonpath 1.0.2 https://github.com/grafana/k6-jslib-JSONPath
formdata 0.0.1, 0.0.2 https://github.com/grafana/k6-jslib-formdata
form-urlencoded 3.0.0 https://github.com/grafana/k6-jslib-form-urlencoded
papaparse 5.1.1 https://www.papaparse.com/docs
ajv 6.12.5 https://ajv.js.org/api.html
httpx 0.0.1, 0.0.2, 0.0.3, 0.0.4, 0.0.5, 0.0.6, 0.1.0 https://grafana.com/docs/k6/latest/javascript-api/jslib/httpx
expect 0.0.4, 0.0.5 https://k6.io/docs/javascript-api/jslib/expect
k6chaijs 4.3.4.0, 4.3.4.1, 4.3.4.2, 4.3.4.3, 4.5.0.0, 4.5.0.1 https://github.com/grafana/k6-jslib-k6chaijs
k6chaijs-contracts 4.3.4.0, 4.3.4.1 https://github.com/grafana/k6-jslib-k6chaijs-contracts
url 1.0.0 https://github.com/grafana/k6-jslib-url
kahwah 0.1.6 https://www.npmjs.com/package/kahwah
aws 0.1.0, 0.3.0, 0.4.0, 0.5.0, 0.6.0, 0.7.0, 0.7.1, 0.7.2, 0.8.0, 0.8.1, 0.9.0, 0.10.0, 0.11.0, 0.12.0, 0.12.1, 0.12.2, 0.12.3, 0.13.0 https://grafana.com/docs/k6/latest/javascript-api/jslib/aws
http-instrumentation-pyroscope 1.0.0, 1.0.1 https://grafana.com/docs/k6/latest/javascript-api/jslib/http-instrumentation-pyroscope/
http-instrumentation-tempo 1.0.0 https://grafana.com/docs/k6/latest/javascript-api/jslib/http-instrumentation-tempo/

Examples

Importing from jslib.k6.io

// Default import
import jsonpath from 'https://jslib.k6.io/jsonpath/1.0.2/index.js'

// Non default import
import { nonDefaultExport } from 'https://jslib.k6.io/some-js-pkg/1.0.0/index.js'

Full script showcase

import { check, sleep } from 'k6'
import jsonpath from 'https://jslib.k6.io/jsonpath/1.0.2/index.js'
import { randomIntBetween, randomItem, uuidv4 } from 'https://jslib.k6.io/k6-utils/1.4.0/index.js'

export const options = {
  duration: '10s',
  vus: 1,
}

const testData = {
  user: {
    name: 'Batman',
  },
}

export default function () {
  check(testData, {
    'JSON path works': () => jsonpath.value(testData, 'user.name') === 'Batman',
  })

  console.log(uuidv4())
  console.log(randomItem([1, 2, 3, 4]))

  sleep(randomIntBetween(1, 5)) // sleep between 1 and 5 seconds
}

Resources