diff --git a/docs/sources/next/extensions/build-k6-binary-using-docker.md b/docs/sources/next/extensions/build-k6-binary-using-docker.md index a6bb55ec7d..e3d6aefeac 100644 --- a/docs/sources/next/extensions/build-k6-binary-using-docker.md +++ b/docs/sources/next/extensions/build-k6-binary-using-docker.md @@ -8,6 +8,12 @@ weight: 03 Using the [xk6 Docker image](https://hub.docker.com/r/grafana/xk6/) can simplify the process of creating a custom k6 binary. It avoids having to setup a local Go environment, and install xk6 manually. +{{% admonition type="note" %}} + +This tutorial is about creating a custom k6 binary (using Docker). If you want to create a Docker image with a custom k6 binary, refer instead to [Using modules with Docker](https://grafana.com/docs/k6//using-k6/modules/#using-modules-with-docker). + +{{% /admonition %}} + ## Building your first extension For example, to build a custom k6 binary with the latest versions of k6 and the [`xk6-kafka`](https://github.com/mostafa/xk6-kafka) and [`xk6-output-influxdb`](https://github.com/grafana/xk6-output-influxdb) extensions, run one of the commands below, depending on your operating system: diff --git a/docs/sources/next/using-k6/javascript-compatibility-mode.md b/docs/sources/next/using-k6/javascript-compatibility-mode.md index 792fc58049..fa0d8ac68e 100644 --- a/docs/sources/next/using-k6/javascript-compatibility-mode.md +++ b/docs/sources/next/using-k6/javascript-compatibility-mode.md @@ -1,44 +1,71 @@ --- -title: JavaScript Compatibility Mode -description: 'k6 supports running test scripts with different ECMAScript compatibility modes using --compatibility-mode' +title: JavaScript compatibility mode +menuTitle: JavaScript mode +excerpt: 'k6 supports running test scripts with different ECMAScript compatibility modes using --compatibility-mode' _build: list: false weight: 19 --- -# JavaScript Compatibility Mode +# JavaScript compatibility mode -You can run test scripts with different ECMAScript compatibility modes with the -`run --compatibility-mode` CLI option or `K6_COMPATIBILITY_MODE` environment variable. +You can write k6 scripts in various ECMAScript versions: -Currently two modes are available: +- ES6+ JavaScript with ES modules (ESM). +- ES6+ JavaScript with CommonJS modules. -## Base +k6 supports both module types and most ES6+ features in all k6 execution modes: local, distributed, and cloud. + +To enable ES module support, k6 uses [Babel](https://babeljs.io/) internally to transform ESM to CommonJS. The process is as follows: + +![Babel transformation in k6](/media/docs/k6-oss/diagram-grafana-k6-babel-pipeline.png) + +Some users prefer to bundle their test code outside k6. For this reason, k6 offers two JavaScript compatibility modes: + +- [Extended mode](#extended-mode): The default option, supporting ESM and most ES6+ features. +- [Base mode](#base-mode): Limited to CommonJS, excluding the Babel step. + +When running tests, you can change the mode by using the `--compatibility-mode` option: + +| Env | CLI | Code / Config file | Default | +| ----------------------- | ---------------------- | ------------------ | ------------ | +| `K6_COMPATIBILITY_MODE` | `--compatibility-mode` | N/A | `"extended"` | + +## Extended mode + +By default, k6 uses the `--compatibility-mode=extended` mode: {{< code >}} ```bash +$ k6 run script.js +``` + +{{< /code >}} + +As illustrated in the previous diagram, if k6 detects unsupported ES+ features while parsing the test script, it then transforms the script with Babel to polyfill the unsupported features. + +Currently, the k6 Babel transformation only adds ESM support and sets `global` (node's global variable) with the value of `globalThis`. + +## Base mode + +{{< code >}} + +```cli $ k6 run --compatibility-mode=base script.js ``` -```bash +```env $ K6_COMPATIBILITY_MODE=base k6 run script.js ``` {{< /code >}} -Pure Golang JavaScript VM supporting ES5.1+. Use this mode if your scripts are already written -using only ES5.1 features, or were previously transformed by [Babel](https://babeljs.io/), -to reduce startup time, RAM usage and improve performance. See the [k6-es6 project](https://github.com/k6io/k6-es6) -for an example [Webpack](https://webpack.js.org/) setup that does this -transformation outside of k6. +The base mode omits the Babel transformation step, supporting only ES5.1+ code. You may want to enable this mode if your scripts are already written using only ES5.1 features or were previously transformed by Babel. -> ### ⚠️ Disclaimer -> -> Your mileage may vary while running `--compatibility-mode=base` and also importing external dependencies. For instance, -> `xml2js` and `cheerio` currently do not work, while `lodash` does. +Generally, this mode is not recommended as it offers minor benefits in reducing startup time. -### Basic Example +### CommonJS Example {{< code >}} @@ -69,133 +96,16 @@ module.exports.default = function () { > but it does _not_ support the > [Node.js module resolution algorithm](https://nodejs.org/api/modules.html#modules_all_together). -### Advanced Example +## Bundling with Babel outside of k6 -{{< code >}} - -```javascript -const http = require('k6/http'); -const metrics = require('k6/metrics'); -const k6 = require('k6'); +The examples below demonstrate the use of Babel with bundlers like [Webpack](https://webpack.js.org/) and [Rollup](https://rollupjs.org/): -module.exports.options = { - stages: [ - { duration: '30s', target: 20 }, - { duration: '1m30s', target: 10 }, - { duration: '20s', target: 0 }, - ], - thresholds: { - 'failed requests': ['rate<0.1'], - }, -}; +- [k6-template-es6](https://github.com/grafana/k6-template-es6): Template using Webpack and Babel to bundle k6 tests. +- [k6-rollup-example](https://github.com/grafana/k6-rollup-example): Example using Rollup and Babel to bundle a testing project. -const myFailRate = new metrics.Rate('failed requests'); +## Read more -module.exports.default = function () { - const res = http.get('https://httpbin.test.k6.io/'); - const checkRes = k6.check(res, { - 'status was 200': function (r) { - return r.status == 200; - }, - }); - if (!checkRes) { - myFailRate.add(1); - } - k6.sleep(1); -}; -``` - -{{< /code >}} - -## Extended - -{{< code >}} - -```bash -$ k6 run --compatibility-mode=extended script.js -``` - -```bash -$ K6_COMPATIBILITY_MODE=extended k6 run script.js -``` - -{{< /code >}} - -In case of syntax/parsing errors, the script will be transformed using Babel with specific plugins bringing the compatibility to ES2015(ES6)+. This means that features such as classes and arrow functions can be used. This does take some time to transpile and the produced code has slightly different line/column numbers. - -Before v0.31.0, k6 included [core.js](https://github.com/zloirock/core-js) v2 and even more Babel plugins in extended mode. -This added around 2MB extra memory usage per VU and some of the transformations (generators, async/await) of Babel were still insufficient to get k6 working with these features. - -## Performance Comparison - -There's a substantial difference in performance between both modes, as shown by -[GNU time](https://www.gnu.org/software/time/) below, especially when running tests with a large -number of VUs: - -{{< code >}} - -```bash -$ /usr/bin/time -v k6 run \ - --compatibility-mode=base \ - --vus 3500 \ - --duration=60s \ - script.js - -[...] -User time (seconds): 15.10 -System time (seconds): 10.02 -Percent of CPU this job got: 40% -Elapsed (wall clock) time (h:mm:ss or m:ss): 1:01.88 -Average shared text size (kbytes): 0 -Average unshared data size (kbytes): 0 -Average stack size (kbytes): 0 -Average total size (kbytes): 0 -Maximum resident set size (kbytes): 903612 -Average resident set size (kbytes): 0 -Major (requiring I/O) page faults: 1 -Minor (reclaiming a frame) page faults: 352090 -Voluntary context switches: 558479 -Involuntary context switches: 4689 -Swaps: 0 -File system inputs: 0 -File system outputs: 78856 -Socket messages sent: 0 -Socket messages received: 0 -Signals delivered: 0 -Page size (bytes): 4096 -Exit status: 0 -``` - -```bash -$ /usr/bin/time -v k6 run \ - --compatibility-mode=extended \ - --vus 3500 \ - --duration=60s \ - script.js - -[...] -User time (seconds): 104.44 -System time (seconds): 6.96 -Percent of CPU this job got: 101% -Elapsed (wall clock) time (h:mm:ss or m:ss): 1:49.49 -Average shared text size (kbytes): 0 -Average unshared data size (kbytes): 0 -Average stack size (kbytes): 0 -Average total size (kbytes): 0 -Maximum resident set size (kbytes): 7972316 -Average resident set size (kbytes): 0 -Major (requiring I/O) page faults: 1 -Minor (reclaiming a frame) page faults: 2595676 -Voluntary context switches: 535511 -Involuntary context switches: 9306 -Swaps: 0 -File system inputs: 0 -File system outputs: 78856 -Socket messages sent: 0 -Socket messages received: 0 -Signals delivered: 0 -Page size (bytes): 4096 -Exit status: 0 -``` - -{{< /code >}} +- [Native ESM support](https://github.com/grafana/k6/issues/3265): GitHub issue for native ESM support in k6. This feature aims to eliminate the Babel transformation step within k6. +- [Running large tests](https://grafana.com/docs/k6//testing-guides/running-large-tests): Optimize k6 for better performance. +- [k6 Modules](https://grafana.com/docs/k6//using-k6/modules): Different options to import modules in k6. +- [k6 Archive Command](https://grafana.com/docs/k6//misc/archive): The `k6 archive` command bundles all k6 test dependencies into a `tar` file, which can then be used for execution. It may also reduce the execution startup time. diff --git a/docs/sources/next/using-k6/modules.md b/docs/sources/next/using-k6/modules.md index 8f13aaf8fc..c7f7b6b1d1 100644 --- a/docs/sources/next/using-k6/modules.md +++ b/docs/sources/next/using-k6/modules.md @@ -10,11 +10,12 @@ weight: 07 ## 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: +In k6, you can import different kinds of modules: - [Built-in modules](#built-in-modules) - [Local filesystem modules](#local-filesystem-modules) - [Remote HTTP(S) modules](#remote-https-modules) +- [Extension modules](#extension-modules) ### Built-in modules @@ -27,19 +28,11 @@ For the full list of built-in modules, refer to the [API documentation](https:// import http from 'k6/http'; ``` -### Local filesystem modules +### Local 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. +These modules are stored on the local filesystem, and accessed either through relative or absolute filesystem paths. -```javascript -//helpers.js -export function someHelper() { - // ... -} -``` +k6 adopts a **browser-like module resolution** and doesn't support [Node.js module resolution](https://nodejs.org/api/modules.html#modules_all_together). File names for `imports` must be fully specified, such as `./helpers.js`. ```javascript //my-test.js @@ -50,13 +43,22 @@ export default function () { } ``` -### Remote HTTP(S) modules +```javascript +//helpers.js +export function someHelper() { + // ... +} +``` + +### Remote modules -These modules are accessed over HTTP(S), from a source like [the k6 JSLib](#the-jslib-repository) or +These modules are accessed over HTTP(S), from a public source like GitHub, any CDN, 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**. +For example, [jslib](https://grafana.com/docs/k6//javascript-api/jslib) is a set of k6 JavaScript libraries available as remote HTTPS modules. They can be downloaded and imported as local modules or directly imported as remote modules. + ```javascript import { randomItem } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; @@ -65,282 +67,105 @@ export default function () { } ``` -### The JSLib repository +You can also build your custom Javascript libraries and distribute them via a public web hosting. For reference, [k6-jslib-aws](https://github.com/grafana/k6-jslib-aws) and [k6-rollup-example](https://github.com/grafana/k6-rollup-example) host their modules as GitHub release assets. -**JSLib** is a set of libraries known to work well with k6. It is available at https://jslib.k6.io/. +### Extension modules -These libraries can either be downloaded and included with the test project or loaded directly using HTTP imports. +Like the [k6 APIs](https://grafana.com/docs/k6//javascript-api), you can build custom modules in Go code and expose them as JavaScript modules. These custom Go-to-JS modules are known as [k6 extensions](https://grafana.com/docs/k6//extensions). -## Bundling node modules - -{{% admonition type="caution" %}} - -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. - -{{% /admonition %}} - -The steps of this tutorial are as follows: - -1. [Setting up the bundler](#setting-up-the-bundler) - 1. [Installing packages](#installing-packages) - 1. [Configuring Webpack](#configuring-webpack) - 1. [Adding a bundle command](#adding-a-bundle-command) -2. [Running the bundling](#running-the-bundling) -3. [Running the tests](#running-the-tests) - -{{% admonition type="note" %}} - -A [ready-to-use Webpack and Babel starter](https://github.com/k6io/k6-es6) is avaialble in a repository. - -{{% /admonition %}} - -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: +Below is an example that imports the `k6/x/kubernetes` module from the [xk6-kubernetes](https://github.com/grafana/xk6-kubernetes) extension. ```javascript -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](https://github.com/webpack/webpack), -[parcel](https://github.com/parcel-bundler/parcel), [rollup](https://github.com/rollup/rollup) -and [browserify](https://github.com/browserify/browserify). - -Due to its flexibility, ease of use, relatively low resource consumption, and known compatibility -with k6, it is recommended to use [webpack](https://github.com/webpack/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](https://grafana.com/docs/k6//using-k6/javascript-compatibility-mode#performance-comparison). - -### 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: - -{{< code >}} - -```bash -$ mkdir ./example-project && \ - cd "$_" && \ - npm init -y -``` - -{{< /code >}} - -#### Installing packages - -Then, install the packages needed: - -{{< code >}} - -```bash -$ npm install --save-dev \ - webpack \ - webpack-cli \ - @types/k6 \ - babel-loader \ - @babel/core \ - @babel/preset-env \ - core-js -``` - -{{< /code >}} - -| Package | Usage | -| :---------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| [webpack](https://github.com/webpack/webpack) | The bundler part of Webpack | -| [webpack-cli](https://github.com/webpack/webpack-cli) | The CLI part of Webpack, which allows us to use it from the terminal | -| [@types/k6](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/k6) | k6 Typescript definition | -| [babel-loader](https://github.com/babel/babel-loader) | A loader used by Webpack to leverage babel functionality while bundling | -| [@babel/core](https://github.com/babel/babel/tree/master/packages/babel-core) | The core functionality of Babel | -| [@babel/preset-env](https://github.com/babel/babel/tree/master/packages/babel-preset-env) | A smart preset using [browserlist](https://github.com/browserslist/browserslist), [compat-table](https://github.com/kangax/compat-table) and [electron-to-chromium](https://github.com/Kilian/electron-to-chromium) to determine what code to transpile and polyfill. | -| [core-js](https://github.com/zloirock/core-js) | A 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: - -{{< code >}} - -```javascript -const path = require('path'); - -module.exports = { - mode: 'production', - entry: { - login: './src/login.test.js', - signup: './src/signup.test.js', +import { Kubernetes } from 'k6/x/kubernetes'; + +const podSpec = { + apiVersion: 'v1', + kind: 'Pod', + metadata: { name: 'busybox', namespace: 'testns' }, + spec: { + containers: [ + { + name: 'busybox', + image: 'busybox', + command: ['sh', '-c', 'sleep 30'], + }, + ], }, - output: { - path: path.resolve(__dirname, 'dist'), // eslint-disable-line - libraryTarget: 'commonjs', - filename: '[name].bundle.js', - }, - module: { - rules: [{ test: /\.js$/, use: 'babel-loader' }], - }, - target: 'web', - externals: /k6(\/.*)?/, }; +export default function () { + const kubernetes = new Kubernetes(); + kubernetes.create(podSpec); + const pods = kubernetes.list('Pod', 'testns'); + pods.map(function (pod) { + console.log(pod.metadata.name); + }); +} ``` -{{< /code >}} - -`Mode` - -Tells Webpack to automatically use the optimizations associated with the `mode`. -Additional details available in [the webpack docs](https://webpack.js.org/configuration/mode/). +How do k6 extensions (Go-to-JS modules) work? For enhanced performance, the k6 engine is written in Go and embeds a JavaScript VM ([goja](https://github.com/dop251/goja)) to execute JavaScript test code. That allows you to build your modules in Go code and import them as JavaScript as usual. -`Entry` +To learn more about using or creating k6 extensions, refer to the [Extension documentation](https://grafana.com/docs/k6//extensions). -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: +## Sharing JavaScript modules -{{< code >}} +As mentioned previously, users can import custom JavaScript libraries by loading either local or remote modules. Because of that, we have two options to import JavaScript modules, along with various methods to distribute them. -```javascript -// login.test.js +{{< admonition type="note" >}} -import { SomeService } from './some.service.js'; +The following options for distributing and sharing JavaScript libraries are available for both custom and other public libraries. -const svc = new SomeService(); -``` +{{< /admonition >}} -{{< /code >}} +**As remote modules** -and - -{{< code >}} +You can host your modules in a public webserver like GitHub and any CDN and be imported remotely. ```javascript -// some.service.js - -import * as lodash from 'lodash'; +// As GitHub release assets +import { + WorkloadConfig, + sayHello, +} from 'https://github.com/grafana/k6-rollup-example/releases/download/v0.0.2/index.js'; -export class SomeService { - constructor() { - this._ = lodash; - } -} +// or hosted in a CDN +import { randomIntBetween, randomItem } from 'https://jslib.k6.io/k6-utils/1.4.0/index.js'; ``` -{{< /code >}} - -would result in Webpack bundling `login.test.js`, `some.service.js` and all upstream dependencies -utilized by `lodash`. - -`Output` +When the library consists of multiple files and modules, you may want to bundle these modules to create public releases. Here are some examples for reference: -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. +- Using Webpack: [k6-jslib-utils](https://github.com/grafana/k6-jslib-utils) and [k6-jslib-aws](https://github.com/grafana/k6-jslib-aws). +- Using Rollup: [test-commons](https://github.com/grafana/k6-rollup-example/). -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](https://webpack.js.org/configuration/output/#outputlibrarytarget). +Be aware that k6 automatically executes remote modules, making it crucial to trust the source code of these remote modules. There is a **risk of altering the remote modules with certain hosting mechanisms**. To mitigate this security risk, some users prefer to download and import the modules locally to ensure full control of the source code. -The `filename` key, as the name suggests, configures the name of the finished bundles. In this -example, the [template string](https://webpack.js.org/configuration/output/#template-strings) `[name]` -is used to add a dynamic part to the output filename. +**As local modules** -#### Adding a bundle command +In this example, the previous remote modules have been downloaded to the `lib` folder of the testing project and imported as follows: -Open the `package.json` file and add a new script entry, used for running the bundling process. - -```diff -{ - "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: - -{{< code >}} - -```bash -$ npm run bundle -# ... -$ tree dist - -dist -├── login.bundle.js -└── signup.bundle.js +```javascript +import { WorkloadConfig, sayHello } from './libs/test-commons.js'; -0 directories, 2 files +import { randomIntBetween, randomItem } from './libs/k6-utils.js'; ``` -{{< /code >}} +Another option to distribute libraries is to use a package manager tool like npm, which enables version locking and the linking of local libraries. The latter can be useful during development. -### Running the tests +Although k6 does not resolve node modules, you can utilize a Bundler to load npm dependencies, as shown in the [k6-rollup-example](https://github.com/grafana/k6-rollup-example). -{{< code >}} +## Using TypeScript -```bash -$ npm run bundle -# ... -$ k6 run dist/login.bundle.js -# ... -``` +k6 does not natively support TypeScript. If you wish to write k6 tests in Typescript, you will need a bundler, as demonstrated in the previous examples: -{{< /code >}} +- Using Webpack: Refer to [k6-template-typescript](https://github.com/grafana/k6-template-typescript) and [k6-jslib-aws](https://github.com/grafana/k6-jslib-aws). +- Using Rollup: Apply the [@rollup/plugin-typescript](https://github.com/rollup/plugins/tree/master/packages/typescript) to the [k6-rollup-example](https://github.com/grafana/k6-rollup-example). -{{< code >}} +## Using modules with Docker -```bash -$ npm run bundle -# ... -$ k6 run dist/signup.bundle.js \ - --vus 10 \ - --duration 10s -# ... -``` - -{{< /code >}} +Built-in and remote modules work out of the box when running k6 in a Docker container like the [Grafana k6 Docker image](https://hub.docker.com/r/grafana/k6). -## Using local modules with Docker +### Local modules -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](https://docs.docker.com/engine/admin/volumes/volumes/), so that k6 can see all the JS modules it needs to import. +To run k6 with Docker and import a local module, you must make sure to mount the necessary folders from the host into the container, using [Docker volumes](https://docs.docker.com/engine/admin/volumes/volumes/). Thus, k6 can see all the JS modules it needs to import. For example, say you have the following structure on your host machine: @@ -380,11 +205,32 @@ $ docker run --rm -v /home/k6/example/src:/src -i grafana/k6 run /src/index.js {{< /code >}} 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: +has been marked for sharing in the Docker Desktop settings. + +### Extension modules + +The official [Grafana k6 Docker image](https://hub.docker.com/r/grafana/k6) includes the k6 release binary but lacks additional k6 extensions. Therefore, using the official Docker container to run a k6 test that requires an extension will fail. + +To run k6 with extensions in Docker, create a Docker image that includes the k6 binary with any extension you may want to use. Define a `Dockerfile` with the necessary [xk6 build](https://grafana.com/docs/k6//extensions/build-k6-binary-using-go#breaking-down-the-xk6-command) instructions as follows: + +```bash +FROM grafana/xk6:latest + +RUN GCO_ENABLED=0 xk6 build \ + --with github.com/grafana/xk6-kubernetes@latest + +ENTRYPOINT ["./k6"] +``` + +After building your custom k6 Docker image, you can [run k6 with Docker](https://grafana.com/docs/k6//get-started/running-k6/) as usual. -![Running k6 in docker on Windows](/media/docs/k6-oss/running-k6-in-docker-on-windows.png) +Alternatively, you can implement a multistage Dockerfile build such as shown on this [Dockerfile example](https://github.com/grafana/xk6-output-prometheus-remote/blob/main/Dockerfile). ## Read more -- [ES6 template](https://github.com/k6io/template-es6): a scaffolding project to use ES6 in your k6 scripts. -- [TypeScript template](https://github.com/k6io/template-typescript): a scaffolding project to use TypeScript in your k6 scripts. +- [JSLib](https://grafana.com/docs/k6//javascript-api/jslib): A collection of k6 JavaScript libraries maintained by Grafana Labs and available as remote modules. +- [Extensions directory](https://grafana.com/docs/k6//extensions/explore): A collection of k6 extensions maintained by Grafana Labs and the community. +- [k6-rollup-example](https://github.com/grafana/k6-rollup-example): Example using Rollup and Babel to bundle a common library and testing suite. +- [k6-template-es6](https://github.com/grafana/k6-template-es6): Template using Webpack and Babel to bundle k6 tests into CommonJS modules and polyfill ES+ features. +- [k6-template-typescript](https://github.com/grafana/k6-template-typescript): Template using Webpack and Babel to use TypeScript in your k6 scripts. +- [JavaScript Compatibility Mode](https://grafana.com/docs/k6//using-k6/javascript-compatibility-mode): An option to change the ECMAScript version supported by k6. diff --git a/docs/sources/v0.47.x/extensions/build-k6-binary-using-docker.md b/docs/sources/v0.47.x/extensions/build-k6-binary-using-docker.md index a6bb55ec7d..e3d6aefeac 100644 --- a/docs/sources/v0.47.x/extensions/build-k6-binary-using-docker.md +++ b/docs/sources/v0.47.x/extensions/build-k6-binary-using-docker.md @@ -8,6 +8,12 @@ weight: 03 Using the [xk6 Docker image](https://hub.docker.com/r/grafana/xk6/) can simplify the process of creating a custom k6 binary. It avoids having to setup a local Go environment, and install xk6 manually. +{{% admonition type="note" %}} + +This tutorial is about creating a custom k6 binary (using Docker). If you want to create a Docker image with a custom k6 binary, refer instead to [Using modules with Docker](https://grafana.com/docs/k6//using-k6/modules/#using-modules-with-docker). + +{{% /admonition %}} + ## Building your first extension For example, to build a custom k6 binary with the latest versions of k6 and the [`xk6-kafka`](https://github.com/mostafa/xk6-kafka) and [`xk6-output-influxdb`](https://github.com/grafana/xk6-output-influxdb) extensions, run one of the commands below, depending on your operating system: diff --git a/docs/sources/v0.47.x/using-k6/javascript-compatibility-mode.md b/docs/sources/v0.47.x/using-k6/javascript-compatibility-mode.md index 792fc58049..fa0d8ac68e 100644 --- a/docs/sources/v0.47.x/using-k6/javascript-compatibility-mode.md +++ b/docs/sources/v0.47.x/using-k6/javascript-compatibility-mode.md @@ -1,44 +1,71 @@ --- -title: JavaScript Compatibility Mode -description: 'k6 supports running test scripts with different ECMAScript compatibility modes using --compatibility-mode' +title: JavaScript compatibility mode +menuTitle: JavaScript mode +excerpt: 'k6 supports running test scripts with different ECMAScript compatibility modes using --compatibility-mode' _build: list: false weight: 19 --- -# JavaScript Compatibility Mode +# JavaScript compatibility mode -You can run test scripts with different ECMAScript compatibility modes with the -`run --compatibility-mode` CLI option or `K6_COMPATIBILITY_MODE` environment variable. +You can write k6 scripts in various ECMAScript versions: -Currently two modes are available: +- ES6+ JavaScript with ES modules (ESM). +- ES6+ JavaScript with CommonJS modules. -## Base +k6 supports both module types and most ES6+ features in all k6 execution modes: local, distributed, and cloud. + +To enable ES module support, k6 uses [Babel](https://babeljs.io/) internally to transform ESM to CommonJS. The process is as follows: + +![Babel transformation in k6](/media/docs/k6-oss/diagram-grafana-k6-babel-pipeline.png) + +Some users prefer to bundle their test code outside k6. For this reason, k6 offers two JavaScript compatibility modes: + +- [Extended mode](#extended-mode): The default option, supporting ESM and most ES6+ features. +- [Base mode](#base-mode): Limited to CommonJS, excluding the Babel step. + +When running tests, you can change the mode by using the `--compatibility-mode` option: + +| Env | CLI | Code / Config file | Default | +| ----------------------- | ---------------------- | ------------------ | ------------ | +| `K6_COMPATIBILITY_MODE` | `--compatibility-mode` | N/A | `"extended"` | + +## Extended mode + +By default, k6 uses the `--compatibility-mode=extended` mode: {{< code >}} ```bash +$ k6 run script.js +``` + +{{< /code >}} + +As illustrated in the previous diagram, if k6 detects unsupported ES+ features while parsing the test script, it then transforms the script with Babel to polyfill the unsupported features. + +Currently, the k6 Babel transformation only adds ESM support and sets `global` (node's global variable) with the value of `globalThis`. + +## Base mode + +{{< code >}} + +```cli $ k6 run --compatibility-mode=base script.js ``` -```bash +```env $ K6_COMPATIBILITY_MODE=base k6 run script.js ``` {{< /code >}} -Pure Golang JavaScript VM supporting ES5.1+. Use this mode if your scripts are already written -using only ES5.1 features, or were previously transformed by [Babel](https://babeljs.io/), -to reduce startup time, RAM usage and improve performance. See the [k6-es6 project](https://github.com/k6io/k6-es6) -for an example [Webpack](https://webpack.js.org/) setup that does this -transformation outside of k6. +The base mode omits the Babel transformation step, supporting only ES5.1+ code. You may want to enable this mode if your scripts are already written using only ES5.1 features or were previously transformed by Babel. -> ### ⚠️ Disclaimer -> -> Your mileage may vary while running `--compatibility-mode=base` and also importing external dependencies. For instance, -> `xml2js` and `cheerio` currently do not work, while `lodash` does. +Generally, this mode is not recommended as it offers minor benefits in reducing startup time. -### Basic Example +### CommonJS Example {{< code >}} @@ -69,133 +96,16 @@ module.exports.default = function () { > but it does _not_ support the > [Node.js module resolution algorithm](https://nodejs.org/api/modules.html#modules_all_together). -### Advanced Example +## Bundling with Babel outside of k6 -{{< code >}} - -```javascript -const http = require('k6/http'); -const metrics = require('k6/metrics'); -const k6 = require('k6'); +The examples below demonstrate the use of Babel with bundlers like [Webpack](https://webpack.js.org/) and [Rollup](https://rollupjs.org/): -module.exports.options = { - stages: [ - { duration: '30s', target: 20 }, - { duration: '1m30s', target: 10 }, - { duration: '20s', target: 0 }, - ], - thresholds: { - 'failed requests': ['rate<0.1'], - }, -}; +- [k6-template-es6](https://github.com/grafana/k6-template-es6): Template using Webpack and Babel to bundle k6 tests. +- [k6-rollup-example](https://github.com/grafana/k6-rollup-example): Example using Rollup and Babel to bundle a testing project. -const myFailRate = new metrics.Rate('failed requests'); +## Read more -module.exports.default = function () { - const res = http.get('https://httpbin.test.k6.io/'); - const checkRes = k6.check(res, { - 'status was 200': function (r) { - return r.status == 200; - }, - }); - if (!checkRes) { - myFailRate.add(1); - } - k6.sleep(1); -}; -``` - -{{< /code >}} - -## Extended - -{{< code >}} - -```bash -$ k6 run --compatibility-mode=extended script.js -``` - -```bash -$ K6_COMPATIBILITY_MODE=extended k6 run script.js -``` - -{{< /code >}} - -In case of syntax/parsing errors, the script will be transformed using Babel with specific plugins bringing the compatibility to ES2015(ES6)+. This means that features such as classes and arrow functions can be used. This does take some time to transpile and the produced code has slightly different line/column numbers. - -Before v0.31.0, k6 included [core.js](https://github.com/zloirock/core-js) v2 and even more Babel plugins in extended mode. -This added around 2MB extra memory usage per VU and some of the transformations (generators, async/await) of Babel were still insufficient to get k6 working with these features. - -## Performance Comparison - -There's a substantial difference in performance between both modes, as shown by -[GNU time](https://www.gnu.org/software/time/) below, especially when running tests with a large -number of VUs: - -{{< code >}} - -```bash -$ /usr/bin/time -v k6 run \ - --compatibility-mode=base \ - --vus 3500 \ - --duration=60s \ - script.js - -[...] -User time (seconds): 15.10 -System time (seconds): 10.02 -Percent of CPU this job got: 40% -Elapsed (wall clock) time (h:mm:ss or m:ss): 1:01.88 -Average shared text size (kbytes): 0 -Average unshared data size (kbytes): 0 -Average stack size (kbytes): 0 -Average total size (kbytes): 0 -Maximum resident set size (kbytes): 903612 -Average resident set size (kbytes): 0 -Major (requiring I/O) page faults: 1 -Minor (reclaiming a frame) page faults: 352090 -Voluntary context switches: 558479 -Involuntary context switches: 4689 -Swaps: 0 -File system inputs: 0 -File system outputs: 78856 -Socket messages sent: 0 -Socket messages received: 0 -Signals delivered: 0 -Page size (bytes): 4096 -Exit status: 0 -``` - -```bash -$ /usr/bin/time -v k6 run \ - --compatibility-mode=extended \ - --vus 3500 \ - --duration=60s \ - script.js - -[...] -User time (seconds): 104.44 -System time (seconds): 6.96 -Percent of CPU this job got: 101% -Elapsed (wall clock) time (h:mm:ss or m:ss): 1:49.49 -Average shared text size (kbytes): 0 -Average unshared data size (kbytes): 0 -Average stack size (kbytes): 0 -Average total size (kbytes): 0 -Maximum resident set size (kbytes): 7972316 -Average resident set size (kbytes): 0 -Major (requiring I/O) page faults: 1 -Minor (reclaiming a frame) page faults: 2595676 -Voluntary context switches: 535511 -Involuntary context switches: 9306 -Swaps: 0 -File system inputs: 0 -File system outputs: 78856 -Socket messages sent: 0 -Socket messages received: 0 -Signals delivered: 0 -Page size (bytes): 4096 -Exit status: 0 -``` - -{{< /code >}} +- [Native ESM support](https://github.com/grafana/k6/issues/3265): GitHub issue for native ESM support in k6. This feature aims to eliminate the Babel transformation step within k6. +- [Running large tests](https://grafana.com/docs/k6//testing-guides/running-large-tests): Optimize k6 for better performance. +- [k6 Modules](https://grafana.com/docs/k6//using-k6/modules): Different options to import modules in k6. +- [k6 Archive Command](https://grafana.com/docs/k6//misc/archive): The `k6 archive` command bundles all k6 test dependencies into a `tar` file, which can then be used for execution. It may also reduce the execution startup time. diff --git a/docs/sources/v0.47.x/using-k6/modules.md b/docs/sources/v0.47.x/using-k6/modules.md index 8f13aaf8fc..c7f7b6b1d1 100644 --- a/docs/sources/v0.47.x/using-k6/modules.md +++ b/docs/sources/v0.47.x/using-k6/modules.md @@ -10,11 +10,12 @@ weight: 07 ## 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: +In k6, you can import different kinds of modules: - [Built-in modules](#built-in-modules) - [Local filesystem modules](#local-filesystem-modules) - [Remote HTTP(S) modules](#remote-https-modules) +- [Extension modules](#extension-modules) ### Built-in modules @@ -27,19 +28,11 @@ For the full list of built-in modules, refer to the [API documentation](https:// import http from 'k6/http'; ``` -### Local filesystem modules +### Local 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. +These modules are stored on the local filesystem, and accessed either through relative or absolute filesystem paths. -```javascript -//helpers.js -export function someHelper() { - // ... -} -``` +k6 adopts a **browser-like module resolution** and doesn't support [Node.js module resolution](https://nodejs.org/api/modules.html#modules_all_together). File names for `imports` must be fully specified, such as `./helpers.js`. ```javascript //my-test.js @@ -50,13 +43,22 @@ export default function () { } ``` -### Remote HTTP(S) modules +```javascript +//helpers.js +export function someHelper() { + // ... +} +``` + +### Remote modules -These modules are accessed over HTTP(S), from a source like [the k6 JSLib](#the-jslib-repository) or +These modules are accessed over HTTP(S), from a public source like GitHub, any CDN, 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**. +For example, [jslib](https://grafana.com/docs/k6//javascript-api/jslib) is a set of k6 JavaScript libraries available as remote HTTPS modules. They can be downloaded and imported as local modules or directly imported as remote modules. + ```javascript import { randomItem } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; @@ -65,282 +67,105 @@ export default function () { } ``` -### The JSLib repository +You can also build your custom Javascript libraries and distribute them via a public web hosting. For reference, [k6-jslib-aws](https://github.com/grafana/k6-jslib-aws) and [k6-rollup-example](https://github.com/grafana/k6-rollup-example) host their modules as GitHub release assets. -**JSLib** is a set of libraries known to work well with k6. It is available at https://jslib.k6.io/. +### Extension modules -These libraries can either be downloaded and included with the test project or loaded directly using HTTP imports. +Like the [k6 APIs](https://grafana.com/docs/k6//javascript-api), you can build custom modules in Go code and expose them as JavaScript modules. These custom Go-to-JS modules are known as [k6 extensions](https://grafana.com/docs/k6//extensions). -## Bundling node modules - -{{% admonition type="caution" %}} - -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. - -{{% /admonition %}} - -The steps of this tutorial are as follows: - -1. [Setting up the bundler](#setting-up-the-bundler) - 1. [Installing packages](#installing-packages) - 1. [Configuring Webpack](#configuring-webpack) - 1. [Adding a bundle command](#adding-a-bundle-command) -2. [Running the bundling](#running-the-bundling) -3. [Running the tests](#running-the-tests) - -{{% admonition type="note" %}} - -A [ready-to-use Webpack and Babel starter](https://github.com/k6io/k6-es6) is avaialble in a repository. - -{{% /admonition %}} - -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: +Below is an example that imports the `k6/x/kubernetes` module from the [xk6-kubernetes](https://github.com/grafana/xk6-kubernetes) extension. ```javascript -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](https://github.com/webpack/webpack), -[parcel](https://github.com/parcel-bundler/parcel), [rollup](https://github.com/rollup/rollup) -and [browserify](https://github.com/browserify/browserify). - -Due to its flexibility, ease of use, relatively low resource consumption, and known compatibility -with k6, it is recommended to use [webpack](https://github.com/webpack/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](https://grafana.com/docs/k6//using-k6/javascript-compatibility-mode#performance-comparison). - -### 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: - -{{< code >}} - -```bash -$ mkdir ./example-project && \ - cd "$_" && \ - npm init -y -``` - -{{< /code >}} - -#### Installing packages - -Then, install the packages needed: - -{{< code >}} - -```bash -$ npm install --save-dev \ - webpack \ - webpack-cli \ - @types/k6 \ - babel-loader \ - @babel/core \ - @babel/preset-env \ - core-js -``` - -{{< /code >}} - -| Package | Usage | -| :---------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| [webpack](https://github.com/webpack/webpack) | The bundler part of Webpack | -| [webpack-cli](https://github.com/webpack/webpack-cli) | The CLI part of Webpack, which allows us to use it from the terminal | -| [@types/k6](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/k6) | k6 Typescript definition | -| [babel-loader](https://github.com/babel/babel-loader) | A loader used by Webpack to leverage babel functionality while bundling | -| [@babel/core](https://github.com/babel/babel/tree/master/packages/babel-core) | The core functionality of Babel | -| [@babel/preset-env](https://github.com/babel/babel/tree/master/packages/babel-preset-env) | A smart preset using [browserlist](https://github.com/browserslist/browserslist), [compat-table](https://github.com/kangax/compat-table) and [electron-to-chromium](https://github.com/Kilian/electron-to-chromium) to determine what code to transpile and polyfill. | -| [core-js](https://github.com/zloirock/core-js) | A 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: - -{{< code >}} - -```javascript -const path = require('path'); - -module.exports = { - mode: 'production', - entry: { - login: './src/login.test.js', - signup: './src/signup.test.js', +import { Kubernetes } from 'k6/x/kubernetes'; + +const podSpec = { + apiVersion: 'v1', + kind: 'Pod', + metadata: { name: 'busybox', namespace: 'testns' }, + spec: { + containers: [ + { + name: 'busybox', + image: 'busybox', + command: ['sh', '-c', 'sleep 30'], + }, + ], }, - output: { - path: path.resolve(__dirname, 'dist'), // eslint-disable-line - libraryTarget: 'commonjs', - filename: '[name].bundle.js', - }, - module: { - rules: [{ test: /\.js$/, use: 'babel-loader' }], - }, - target: 'web', - externals: /k6(\/.*)?/, }; +export default function () { + const kubernetes = new Kubernetes(); + kubernetes.create(podSpec); + const pods = kubernetes.list('Pod', 'testns'); + pods.map(function (pod) { + console.log(pod.metadata.name); + }); +} ``` -{{< /code >}} - -`Mode` - -Tells Webpack to automatically use the optimizations associated with the `mode`. -Additional details available in [the webpack docs](https://webpack.js.org/configuration/mode/). +How do k6 extensions (Go-to-JS modules) work? For enhanced performance, the k6 engine is written in Go and embeds a JavaScript VM ([goja](https://github.com/dop251/goja)) to execute JavaScript test code. That allows you to build your modules in Go code and import them as JavaScript as usual. -`Entry` +To learn more about using or creating k6 extensions, refer to the [Extension documentation](https://grafana.com/docs/k6//extensions). -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: +## Sharing JavaScript modules -{{< code >}} +As mentioned previously, users can import custom JavaScript libraries by loading either local or remote modules. Because of that, we have two options to import JavaScript modules, along with various methods to distribute them. -```javascript -// login.test.js +{{< admonition type="note" >}} -import { SomeService } from './some.service.js'; +The following options for distributing and sharing JavaScript libraries are available for both custom and other public libraries. -const svc = new SomeService(); -``` +{{< /admonition >}} -{{< /code >}} +**As remote modules** -and - -{{< code >}} +You can host your modules in a public webserver like GitHub and any CDN and be imported remotely. ```javascript -// some.service.js - -import * as lodash from 'lodash'; +// As GitHub release assets +import { + WorkloadConfig, + sayHello, +} from 'https://github.com/grafana/k6-rollup-example/releases/download/v0.0.2/index.js'; -export class SomeService { - constructor() { - this._ = lodash; - } -} +// or hosted in a CDN +import { randomIntBetween, randomItem } from 'https://jslib.k6.io/k6-utils/1.4.0/index.js'; ``` -{{< /code >}} - -would result in Webpack bundling `login.test.js`, `some.service.js` and all upstream dependencies -utilized by `lodash`. - -`Output` +When the library consists of multiple files and modules, you may want to bundle these modules to create public releases. Here are some examples for reference: -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. +- Using Webpack: [k6-jslib-utils](https://github.com/grafana/k6-jslib-utils) and [k6-jslib-aws](https://github.com/grafana/k6-jslib-aws). +- Using Rollup: [test-commons](https://github.com/grafana/k6-rollup-example/). -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](https://webpack.js.org/configuration/output/#outputlibrarytarget). +Be aware that k6 automatically executes remote modules, making it crucial to trust the source code of these remote modules. There is a **risk of altering the remote modules with certain hosting mechanisms**. To mitigate this security risk, some users prefer to download and import the modules locally to ensure full control of the source code. -The `filename` key, as the name suggests, configures the name of the finished bundles. In this -example, the [template string](https://webpack.js.org/configuration/output/#template-strings) `[name]` -is used to add a dynamic part to the output filename. +**As local modules** -#### Adding a bundle command +In this example, the previous remote modules have been downloaded to the `lib` folder of the testing project and imported as follows: -Open the `package.json` file and add a new script entry, used for running the bundling process. - -```diff -{ - "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: - -{{< code >}} - -```bash -$ npm run bundle -# ... -$ tree dist - -dist -├── login.bundle.js -└── signup.bundle.js +```javascript +import { WorkloadConfig, sayHello } from './libs/test-commons.js'; -0 directories, 2 files +import { randomIntBetween, randomItem } from './libs/k6-utils.js'; ``` -{{< /code >}} +Another option to distribute libraries is to use a package manager tool like npm, which enables version locking and the linking of local libraries. The latter can be useful during development. -### Running the tests +Although k6 does not resolve node modules, you can utilize a Bundler to load npm dependencies, as shown in the [k6-rollup-example](https://github.com/grafana/k6-rollup-example). -{{< code >}} +## Using TypeScript -```bash -$ npm run bundle -# ... -$ k6 run dist/login.bundle.js -# ... -``` +k6 does not natively support TypeScript. If you wish to write k6 tests in Typescript, you will need a bundler, as demonstrated in the previous examples: -{{< /code >}} +- Using Webpack: Refer to [k6-template-typescript](https://github.com/grafana/k6-template-typescript) and [k6-jslib-aws](https://github.com/grafana/k6-jslib-aws). +- Using Rollup: Apply the [@rollup/plugin-typescript](https://github.com/rollup/plugins/tree/master/packages/typescript) to the [k6-rollup-example](https://github.com/grafana/k6-rollup-example). -{{< code >}} +## Using modules with Docker -```bash -$ npm run bundle -# ... -$ k6 run dist/signup.bundle.js \ - --vus 10 \ - --duration 10s -# ... -``` - -{{< /code >}} +Built-in and remote modules work out of the box when running k6 in a Docker container like the [Grafana k6 Docker image](https://hub.docker.com/r/grafana/k6). -## Using local modules with Docker +### Local modules -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](https://docs.docker.com/engine/admin/volumes/volumes/), so that k6 can see all the JS modules it needs to import. +To run k6 with Docker and import a local module, you must make sure to mount the necessary folders from the host into the container, using [Docker volumes](https://docs.docker.com/engine/admin/volumes/volumes/). Thus, k6 can see all the JS modules it needs to import. For example, say you have the following structure on your host machine: @@ -380,11 +205,32 @@ $ docker run --rm -v /home/k6/example/src:/src -i grafana/k6 run /src/index.js {{< /code >}} 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: +has been marked for sharing in the Docker Desktop settings. + +### Extension modules + +The official [Grafana k6 Docker image](https://hub.docker.com/r/grafana/k6) includes the k6 release binary but lacks additional k6 extensions. Therefore, using the official Docker container to run a k6 test that requires an extension will fail. + +To run k6 with extensions in Docker, create a Docker image that includes the k6 binary with any extension you may want to use. Define a `Dockerfile` with the necessary [xk6 build](https://grafana.com/docs/k6//extensions/build-k6-binary-using-go#breaking-down-the-xk6-command) instructions as follows: + +```bash +FROM grafana/xk6:latest + +RUN GCO_ENABLED=0 xk6 build \ + --with github.com/grafana/xk6-kubernetes@latest + +ENTRYPOINT ["./k6"] +``` + +After building your custom k6 Docker image, you can [run k6 with Docker](https://grafana.com/docs/k6//get-started/running-k6/) as usual. -![Running k6 in docker on Windows](/media/docs/k6-oss/running-k6-in-docker-on-windows.png) +Alternatively, you can implement a multistage Dockerfile build such as shown on this [Dockerfile example](https://github.com/grafana/xk6-output-prometheus-remote/blob/main/Dockerfile). ## Read more -- [ES6 template](https://github.com/k6io/template-es6): a scaffolding project to use ES6 in your k6 scripts. -- [TypeScript template](https://github.com/k6io/template-typescript): a scaffolding project to use TypeScript in your k6 scripts. +- [JSLib](https://grafana.com/docs/k6//javascript-api/jslib): A collection of k6 JavaScript libraries maintained by Grafana Labs and available as remote modules. +- [Extensions directory](https://grafana.com/docs/k6//extensions/explore): A collection of k6 extensions maintained by Grafana Labs and the community. +- [k6-rollup-example](https://github.com/grafana/k6-rollup-example): Example using Rollup and Babel to bundle a common library and testing suite. +- [k6-template-es6](https://github.com/grafana/k6-template-es6): Template using Webpack and Babel to bundle k6 tests into CommonJS modules and polyfill ES+ features. +- [k6-template-typescript](https://github.com/grafana/k6-template-typescript): Template using Webpack and Babel to use TypeScript in your k6 scripts. +- [JavaScript Compatibility Mode](https://grafana.com/docs/k6//using-k6/javascript-compatibility-mode): An option to change the ECMAScript version supported by k6. diff --git a/docs/sources/v0.48.x/extensions/build-k6-binary-using-docker.md b/docs/sources/v0.48.x/extensions/build-k6-binary-using-docker.md index a6bb55ec7d..e3d6aefeac 100644 --- a/docs/sources/v0.48.x/extensions/build-k6-binary-using-docker.md +++ b/docs/sources/v0.48.x/extensions/build-k6-binary-using-docker.md @@ -8,6 +8,12 @@ weight: 03 Using the [xk6 Docker image](https://hub.docker.com/r/grafana/xk6/) can simplify the process of creating a custom k6 binary. It avoids having to setup a local Go environment, and install xk6 manually. +{{% admonition type="note" %}} + +This tutorial is about creating a custom k6 binary (using Docker). If you want to create a Docker image with a custom k6 binary, refer instead to [Using modules with Docker](https://grafana.com/docs/k6//using-k6/modules/#using-modules-with-docker). + +{{% /admonition %}} + ## Building your first extension For example, to build a custom k6 binary with the latest versions of k6 and the [`xk6-kafka`](https://github.com/mostafa/xk6-kafka) and [`xk6-output-influxdb`](https://github.com/grafana/xk6-output-influxdb) extensions, run one of the commands below, depending on your operating system: diff --git a/docs/sources/v0.48.x/using-k6/javascript-compatibility-mode.md b/docs/sources/v0.48.x/using-k6/javascript-compatibility-mode.md index 792fc58049..fa0d8ac68e 100644 --- a/docs/sources/v0.48.x/using-k6/javascript-compatibility-mode.md +++ b/docs/sources/v0.48.x/using-k6/javascript-compatibility-mode.md @@ -1,44 +1,71 @@ --- -title: JavaScript Compatibility Mode -description: 'k6 supports running test scripts with different ECMAScript compatibility modes using --compatibility-mode' +title: JavaScript compatibility mode +menuTitle: JavaScript mode +excerpt: 'k6 supports running test scripts with different ECMAScript compatibility modes using --compatibility-mode' _build: list: false weight: 19 --- -# JavaScript Compatibility Mode +# JavaScript compatibility mode -You can run test scripts with different ECMAScript compatibility modes with the -`run --compatibility-mode` CLI option or `K6_COMPATIBILITY_MODE` environment variable. +You can write k6 scripts in various ECMAScript versions: -Currently two modes are available: +- ES6+ JavaScript with ES modules (ESM). +- ES6+ JavaScript with CommonJS modules. -## Base +k6 supports both module types and most ES6+ features in all k6 execution modes: local, distributed, and cloud. + +To enable ES module support, k6 uses [Babel](https://babeljs.io/) internally to transform ESM to CommonJS. The process is as follows: + +![Babel transformation in k6](/media/docs/k6-oss/diagram-grafana-k6-babel-pipeline.png) + +Some users prefer to bundle their test code outside k6. For this reason, k6 offers two JavaScript compatibility modes: + +- [Extended mode](#extended-mode): The default option, supporting ESM and most ES6+ features. +- [Base mode](#base-mode): Limited to CommonJS, excluding the Babel step. + +When running tests, you can change the mode by using the `--compatibility-mode` option: + +| Env | CLI | Code / Config file | Default | +| ----------------------- | ---------------------- | ------------------ | ------------ | +| `K6_COMPATIBILITY_MODE` | `--compatibility-mode` | N/A | `"extended"` | + +## Extended mode + +By default, k6 uses the `--compatibility-mode=extended` mode: {{< code >}} ```bash +$ k6 run script.js +``` + +{{< /code >}} + +As illustrated in the previous diagram, if k6 detects unsupported ES+ features while parsing the test script, it then transforms the script with Babel to polyfill the unsupported features. + +Currently, the k6 Babel transformation only adds ESM support and sets `global` (node's global variable) with the value of `globalThis`. + +## Base mode + +{{< code >}} + +```cli $ k6 run --compatibility-mode=base script.js ``` -```bash +```env $ K6_COMPATIBILITY_MODE=base k6 run script.js ``` {{< /code >}} -Pure Golang JavaScript VM supporting ES5.1+. Use this mode if your scripts are already written -using only ES5.1 features, or were previously transformed by [Babel](https://babeljs.io/), -to reduce startup time, RAM usage and improve performance. See the [k6-es6 project](https://github.com/k6io/k6-es6) -for an example [Webpack](https://webpack.js.org/) setup that does this -transformation outside of k6. +The base mode omits the Babel transformation step, supporting only ES5.1+ code. You may want to enable this mode if your scripts are already written using only ES5.1 features or were previously transformed by Babel. -> ### ⚠️ Disclaimer -> -> Your mileage may vary while running `--compatibility-mode=base` and also importing external dependencies. For instance, -> `xml2js` and `cheerio` currently do not work, while `lodash` does. +Generally, this mode is not recommended as it offers minor benefits in reducing startup time. -### Basic Example +### CommonJS Example {{< code >}} @@ -69,133 +96,16 @@ module.exports.default = function () { > but it does _not_ support the > [Node.js module resolution algorithm](https://nodejs.org/api/modules.html#modules_all_together). -### Advanced Example +## Bundling with Babel outside of k6 -{{< code >}} - -```javascript -const http = require('k6/http'); -const metrics = require('k6/metrics'); -const k6 = require('k6'); +The examples below demonstrate the use of Babel with bundlers like [Webpack](https://webpack.js.org/) and [Rollup](https://rollupjs.org/): -module.exports.options = { - stages: [ - { duration: '30s', target: 20 }, - { duration: '1m30s', target: 10 }, - { duration: '20s', target: 0 }, - ], - thresholds: { - 'failed requests': ['rate<0.1'], - }, -}; +- [k6-template-es6](https://github.com/grafana/k6-template-es6): Template using Webpack and Babel to bundle k6 tests. +- [k6-rollup-example](https://github.com/grafana/k6-rollup-example): Example using Rollup and Babel to bundle a testing project. -const myFailRate = new metrics.Rate('failed requests'); +## Read more -module.exports.default = function () { - const res = http.get('https://httpbin.test.k6.io/'); - const checkRes = k6.check(res, { - 'status was 200': function (r) { - return r.status == 200; - }, - }); - if (!checkRes) { - myFailRate.add(1); - } - k6.sleep(1); -}; -``` - -{{< /code >}} - -## Extended - -{{< code >}} - -```bash -$ k6 run --compatibility-mode=extended script.js -``` - -```bash -$ K6_COMPATIBILITY_MODE=extended k6 run script.js -``` - -{{< /code >}} - -In case of syntax/parsing errors, the script will be transformed using Babel with specific plugins bringing the compatibility to ES2015(ES6)+. This means that features such as classes and arrow functions can be used. This does take some time to transpile and the produced code has slightly different line/column numbers. - -Before v0.31.0, k6 included [core.js](https://github.com/zloirock/core-js) v2 and even more Babel plugins in extended mode. -This added around 2MB extra memory usage per VU and some of the transformations (generators, async/await) of Babel were still insufficient to get k6 working with these features. - -## Performance Comparison - -There's a substantial difference in performance between both modes, as shown by -[GNU time](https://www.gnu.org/software/time/) below, especially when running tests with a large -number of VUs: - -{{< code >}} - -```bash -$ /usr/bin/time -v k6 run \ - --compatibility-mode=base \ - --vus 3500 \ - --duration=60s \ - script.js - -[...] -User time (seconds): 15.10 -System time (seconds): 10.02 -Percent of CPU this job got: 40% -Elapsed (wall clock) time (h:mm:ss or m:ss): 1:01.88 -Average shared text size (kbytes): 0 -Average unshared data size (kbytes): 0 -Average stack size (kbytes): 0 -Average total size (kbytes): 0 -Maximum resident set size (kbytes): 903612 -Average resident set size (kbytes): 0 -Major (requiring I/O) page faults: 1 -Minor (reclaiming a frame) page faults: 352090 -Voluntary context switches: 558479 -Involuntary context switches: 4689 -Swaps: 0 -File system inputs: 0 -File system outputs: 78856 -Socket messages sent: 0 -Socket messages received: 0 -Signals delivered: 0 -Page size (bytes): 4096 -Exit status: 0 -``` - -```bash -$ /usr/bin/time -v k6 run \ - --compatibility-mode=extended \ - --vus 3500 \ - --duration=60s \ - script.js - -[...] -User time (seconds): 104.44 -System time (seconds): 6.96 -Percent of CPU this job got: 101% -Elapsed (wall clock) time (h:mm:ss or m:ss): 1:49.49 -Average shared text size (kbytes): 0 -Average unshared data size (kbytes): 0 -Average stack size (kbytes): 0 -Average total size (kbytes): 0 -Maximum resident set size (kbytes): 7972316 -Average resident set size (kbytes): 0 -Major (requiring I/O) page faults: 1 -Minor (reclaiming a frame) page faults: 2595676 -Voluntary context switches: 535511 -Involuntary context switches: 9306 -Swaps: 0 -File system inputs: 0 -File system outputs: 78856 -Socket messages sent: 0 -Socket messages received: 0 -Signals delivered: 0 -Page size (bytes): 4096 -Exit status: 0 -``` - -{{< /code >}} +- [Native ESM support](https://github.com/grafana/k6/issues/3265): GitHub issue for native ESM support in k6. This feature aims to eliminate the Babel transformation step within k6. +- [Running large tests](https://grafana.com/docs/k6//testing-guides/running-large-tests): Optimize k6 for better performance. +- [k6 Modules](https://grafana.com/docs/k6//using-k6/modules): Different options to import modules in k6. +- [k6 Archive Command](https://grafana.com/docs/k6//misc/archive): The `k6 archive` command bundles all k6 test dependencies into a `tar` file, which can then be used for execution. It may also reduce the execution startup time. diff --git a/docs/sources/v0.48.x/using-k6/modules.md b/docs/sources/v0.48.x/using-k6/modules.md index 8f13aaf8fc..c7f7b6b1d1 100644 --- a/docs/sources/v0.48.x/using-k6/modules.md +++ b/docs/sources/v0.48.x/using-k6/modules.md @@ -10,11 +10,12 @@ weight: 07 ## 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: +In k6, you can import different kinds of modules: - [Built-in modules](#built-in-modules) - [Local filesystem modules](#local-filesystem-modules) - [Remote HTTP(S) modules](#remote-https-modules) +- [Extension modules](#extension-modules) ### Built-in modules @@ -27,19 +28,11 @@ For the full list of built-in modules, refer to the [API documentation](https:// import http from 'k6/http'; ``` -### Local filesystem modules +### Local 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. +These modules are stored on the local filesystem, and accessed either through relative or absolute filesystem paths. -```javascript -//helpers.js -export function someHelper() { - // ... -} -``` +k6 adopts a **browser-like module resolution** and doesn't support [Node.js module resolution](https://nodejs.org/api/modules.html#modules_all_together). File names for `imports` must be fully specified, such as `./helpers.js`. ```javascript //my-test.js @@ -50,13 +43,22 @@ export default function () { } ``` -### Remote HTTP(S) modules +```javascript +//helpers.js +export function someHelper() { + // ... +} +``` + +### Remote modules -These modules are accessed over HTTP(S), from a source like [the k6 JSLib](#the-jslib-repository) or +These modules are accessed over HTTP(S), from a public source like GitHub, any CDN, 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**. +For example, [jslib](https://grafana.com/docs/k6//javascript-api/jslib) is a set of k6 JavaScript libraries available as remote HTTPS modules. They can be downloaded and imported as local modules or directly imported as remote modules. + ```javascript import { randomItem } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; @@ -65,282 +67,105 @@ export default function () { } ``` -### The JSLib repository +You can also build your custom Javascript libraries and distribute them via a public web hosting. For reference, [k6-jslib-aws](https://github.com/grafana/k6-jslib-aws) and [k6-rollup-example](https://github.com/grafana/k6-rollup-example) host their modules as GitHub release assets. -**JSLib** is a set of libraries known to work well with k6. It is available at https://jslib.k6.io/. +### Extension modules -These libraries can either be downloaded and included with the test project or loaded directly using HTTP imports. +Like the [k6 APIs](https://grafana.com/docs/k6//javascript-api), you can build custom modules in Go code and expose them as JavaScript modules. These custom Go-to-JS modules are known as [k6 extensions](https://grafana.com/docs/k6//extensions). -## Bundling node modules - -{{% admonition type="caution" %}} - -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. - -{{% /admonition %}} - -The steps of this tutorial are as follows: - -1. [Setting up the bundler](#setting-up-the-bundler) - 1. [Installing packages](#installing-packages) - 1. [Configuring Webpack](#configuring-webpack) - 1. [Adding a bundle command](#adding-a-bundle-command) -2. [Running the bundling](#running-the-bundling) -3. [Running the tests](#running-the-tests) - -{{% admonition type="note" %}} - -A [ready-to-use Webpack and Babel starter](https://github.com/k6io/k6-es6) is avaialble in a repository. - -{{% /admonition %}} - -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: +Below is an example that imports the `k6/x/kubernetes` module from the [xk6-kubernetes](https://github.com/grafana/xk6-kubernetes) extension. ```javascript -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](https://github.com/webpack/webpack), -[parcel](https://github.com/parcel-bundler/parcel), [rollup](https://github.com/rollup/rollup) -and [browserify](https://github.com/browserify/browserify). - -Due to its flexibility, ease of use, relatively low resource consumption, and known compatibility -with k6, it is recommended to use [webpack](https://github.com/webpack/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](https://grafana.com/docs/k6//using-k6/javascript-compatibility-mode#performance-comparison). - -### 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: - -{{< code >}} - -```bash -$ mkdir ./example-project && \ - cd "$_" && \ - npm init -y -``` - -{{< /code >}} - -#### Installing packages - -Then, install the packages needed: - -{{< code >}} - -```bash -$ npm install --save-dev \ - webpack \ - webpack-cli \ - @types/k6 \ - babel-loader \ - @babel/core \ - @babel/preset-env \ - core-js -``` - -{{< /code >}} - -| Package | Usage | -| :---------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| [webpack](https://github.com/webpack/webpack) | The bundler part of Webpack | -| [webpack-cli](https://github.com/webpack/webpack-cli) | The CLI part of Webpack, which allows us to use it from the terminal | -| [@types/k6](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/k6) | k6 Typescript definition | -| [babel-loader](https://github.com/babel/babel-loader) | A loader used by Webpack to leverage babel functionality while bundling | -| [@babel/core](https://github.com/babel/babel/tree/master/packages/babel-core) | The core functionality of Babel | -| [@babel/preset-env](https://github.com/babel/babel/tree/master/packages/babel-preset-env) | A smart preset using [browserlist](https://github.com/browserslist/browserslist), [compat-table](https://github.com/kangax/compat-table) and [electron-to-chromium](https://github.com/Kilian/electron-to-chromium) to determine what code to transpile and polyfill. | -| [core-js](https://github.com/zloirock/core-js) | A 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: - -{{< code >}} - -```javascript -const path = require('path'); - -module.exports = { - mode: 'production', - entry: { - login: './src/login.test.js', - signup: './src/signup.test.js', +import { Kubernetes } from 'k6/x/kubernetes'; + +const podSpec = { + apiVersion: 'v1', + kind: 'Pod', + metadata: { name: 'busybox', namespace: 'testns' }, + spec: { + containers: [ + { + name: 'busybox', + image: 'busybox', + command: ['sh', '-c', 'sleep 30'], + }, + ], }, - output: { - path: path.resolve(__dirname, 'dist'), // eslint-disable-line - libraryTarget: 'commonjs', - filename: '[name].bundle.js', - }, - module: { - rules: [{ test: /\.js$/, use: 'babel-loader' }], - }, - target: 'web', - externals: /k6(\/.*)?/, }; +export default function () { + const kubernetes = new Kubernetes(); + kubernetes.create(podSpec); + const pods = kubernetes.list('Pod', 'testns'); + pods.map(function (pod) { + console.log(pod.metadata.name); + }); +} ``` -{{< /code >}} - -`Mode` - -Tells Webpack to automatically use the optimizations associated with the `mode`. -Additional details available in [the webpack docs](https://webpack.js.org/configuration/mode/). +How do k6 extensions (Go-to-JS modules) work? For enhanced performance, the k6 engine is written in Go and embeds a JavaScript VM ([goja](https://github.com/dop251/goja)) to execute JavaScript test code. That allows you to build your modules in Go code and import them as JavaScript as usual. -`Entry` +To learn more about using or creating k6 extensions, refer to the [Extension documentation](https://grafana.com/docs/k6//extensions). -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: +## Sharing JavaScript modules -{{< code >}} +As mentioned previously, users can import custom JavaScript libraries by loading either local or remote modules. Because of that, we have two options to import JavaScript modules, along with various methods to distribute them. -```javascript -// login.test.js +{{< admonition type="note" >}} -import { SomeService } from './some.service.js'; +The following options for distributing and sharing JavaScript libraries are available for both custom and other public libraries. -const svc = new SomeService(); -``` +{{< /admonition >}} -{{< /code >}} +**As remote modules** -and - -{{< code >}} +You can host your modules in a public webserver like GitHub and any CDN and be imported remotely. ```javascript -// some.service.js - -import * as lodash from 'lodash'; +// As GitHub release assets +import { + WorkloadConfig, + sayHello, +} from 'https://github.com/grafana/k6-rollup-example/releases/download/v0.0.2/index.js'; -export class SomeService { - constructor() { - this._ = lodash; - } -} +// or hosted in a CDN +import { randomIntBetween, randomItem } from 'https://jslib.k6.io/k6-utils/1.4.0/index.js'; ``` -{{< /code >}} - -would result in Webpack bundling `login.test.js`, `some.service.js` and all upstream dependencies -utilized by `lodash`. - -`Output` +When the library consists of multiple files and modules, you may want to bundle these modules to create public releases. Here are some examples for reference: -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. +- Using Webpack: [k6-jslib-utils](https://github.com/grafana/k6-jslib-utils) and [k6-jslib-aws](https://github.com/grafana/k6-jslib-aws). +- Using Rollup: [test-commons](https://github.com/grafana/k6-rollup-example/). -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](https://webpack.js.org/configuration/output/#outputlibrarytarget). +Be aware that k6 automatically executes remote modules, making it crucial to trust the source code of these remote modules. There is a **risk of altering the remote modules with certain hosting mechanisms**. To mitigate this security risk, some users prefer to download and import the modules locally to ensure full control of the source code. -The `filename` key, as the name suggests, configures the name of the finished bundles. In this -example, the [template string](https://webpack.js.org/configuration/output/#template-strings) `[name]` -is used to add a dynamic part to the output filename. +**As local modules** -#### Adding a bundle command +In this example, the previous remote modules have been downloaded to the `lib` folder of the testing project and imported as follows: -Open the `package.json` file and add a new script entry, used for running the bundling process. - -```diff -{ - "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: - -{{< code >}} - -```bash -$ npm run bundle -# ... -$ tree dist - -dist -├── login.bundle.js -└── signup.bundle.js +```javascript +import { WorkloadConfig, sayHello } from './libs/test-commons.js'; -0 directories, 2 files +import { randomIntBetween, randomItem } from './libs/k6-utils.js'; ``` -{{< /code >}} +Another option to distribute libraries is to use a package manager tool like npm, which enables version locking and the linking of local libraries. The latter can be useful during development. -### Running the tests +Although k6 does not resolve node modules, you can utilize a Bundler to load npm dependencies, as shown in the [k6-rollup-example](https://github.com/grafana/k6-rollup-example). -{{< code >}} +## Using TypeScript -```bash -$ npm run bundle -# ... -$ k6 run dist/login.bundle.js -# ... -``` +k6 does not natively support TypeScript. If you wish to write k6 tests in Typescript, you will need a bundler, as demonstrated in the previous examples: -{{< /code >}} +- Using Webpack: Refer to [k6-template-typescript](https://github.com/grafana/k6-template-typescript) and [k6-jslib-aws](https://github.com/grafana/k6-jslib-aws). +- Using Rollup: Apply the [@rollup/plugin-typescript](https://github.com/rollup/plugins/tree/master/packages/typescript) to the [k6-rollup-example](https://github.com/grafana/k6-rollup-example). -{{< code >}} +## Using modules with Docker -```bash -$ npm run bundle -# ... -$ k6 run dist/signup.bundle.js \ - --vus 10 \ - --duration 10s -# ... -``` - -{{< /code >}} +Built-in and remote modules work out of the box when running k6 in a Docker container like the [Grafana k6 Docker image](https://hub.docker.com/r/grafana/k6). -## Using local modules with Docker +### Local modules -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](https://docs.docker.com/engine/admin/volumes/volumes/), so that k6 can see all the JS modules it needs to import. +To run k6 with Docker and import a local module, you must make sure to mount the necessary folders from the host into the container, using [Docker volumes](https://docs.docker.com/engine/admin/volumes/volumes/). Thus, k6 can see all the JS modules it needs to import. For example, say you have the following structure on your host machine: @@ -380,11 +205,32 @@ $ docker run --rm -v /home/k6/example/src:/src -i grafana/k6 run /src/index.js {{< /code >}} 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: +has been marked for sharing in the Docker Desktop settings. + +### Extension modules + +The official [Grafana k6 Docker image](https://hub.docker.com/r/grafana/k6) includes the k6 release binary but lacks additional k6 extensions. Therefore, using the official Docker container to run a k6 test that requires an extension will fail. + +To run k6 with extensions in Docker, create a Docker image that includes the k6 binary with any extension you may want to use. Define a `Dockerfile` with the necessary [xk6 build](https://grafana.com/docs/k6//extensions/build-k6-binary-using-go#breaking-down-the-xk6-command) instructions as follows: + +```bash +FROM grafana/xk6:latest + +RUN GCO_ENABLED=0 xk6 build \ + --with github.com/grafana/xk6-kubernetes@latest + +ENTRYPOINT ["./k6"] +``` + +After building your custom k6 Docker image, you can [run k6 with Docker](https://grafana.com/docs/k6//get-started/running-k6/) as usual. -![Running k6 in docker on Windows](/media/docs/k6-oss/running-k6-in-docker-on-windows.png) +Alternatively, you can implement a multistage Dockerfile build such as shown on this [Dockerfile example](https://github.com/grafana/xk6-output-prometheus-remote/blob/main/Dockerfile). ## Read more -- [ES6 template](https://github.com/k6io/template-es6): a scaffolding project to use ES6 in your k6 scripts. -- [TypeScript template](https://github.com/k6io/template-typescript): a scaffolding project to use TypeScript in your k6 scripts. +- [JSLib](https://grafana.com/docs/k6//javascript-api/jslib): A collection of k6 JavaScript libraries maintained by Grafana Labs and available as remote modules. +- [Extensions directory](https://grafana.com/docs/k6//extensions/explore): A collection of k6 extensions maintained by Grafana Labs and the community. +- [k6-rollup-example](https://github.com/grafana/k6-rollup-example): Example using Rollup and Babel to bundle a common library and testing suite. +- [k6-template-es6](https://github.com/grafana/k6-template-es6): Template using Webpack and Babel to bundle k6 tests into CommonJS modules and polyfill ES+ features. +- [k6-template-typescript](https://github.com/grafana/k6-template-typescript): Template using Webpack and Babel to use TypeScript in your k6 scripts. +- [JavaScript Compatibility Mode](https://grafana.com/docs/k6//using-k6/javascript-compatibility-mode): An option to change the ECMAScript version supported by k6. diff --git a/docs/sources/v0.49.x/extensions/build-k6-binary-using-docker.md b/docs/sources/v0.49.x/extensions/build-k6-binary-using-docker.md index a6bb55ec7d..e3d6aefeac 100644 --- a/docs/sources/v0.49.x/extensions/build-k6-binary-using-docker.md +++ b/docs/sources/v0.49.x/extensions/build-k6-binary-using-docker.md @@ -8,6 +8,12 @@ weight: 03 Using the [xk6 Docker image](https://hub.docker.com/r/grafana/xk6/) can simplify the process of creating a custom k6 binary. It avoids having to setup a local Go environment, and install xk6 manually. +{{% admonition type="note" %}} + +This tutorial is about creating a custom k6 binary (using Docker). If you want to create a Docker image with a custom k6 binary, refer instead to [Using modules with Docker](https://grafana.com/docs/k6//using-k6/modules/#using-modules-with-docker). + +{{% /admonition %}} + ## Building your first extension For example, to build a custom k6 binary with the latest versions of k6 and the [`xk6-kafka`](https://github.com/mostafa/xk6-kafka) and [`xk6-output-influxdb`](https://github.com/grafana/xk6-output-influxdb) extensions, run one of the commands below, depending on your operating system: diff --git a/docs/sources/v0.49.x/using-k6/javascript-compatibility-mode.md b/docs/sources/v0.49.x/using-k6/javascript-compatibility-mode.md index 792fc58049..fa0d8ac68e 100644 --- a/docs/sources/v0.49.x/using-k6/javascript-compatibility-mode.md +++ b/docs/sources/v0.49.x/using-k6/javascript-compatibility-mode.md @@ -1,44 +1,71 @@ --- -title: JavaScript Compatibility Mode -description: 'k6 supports running test scripts with different ECMAScript compatibility modes using --compatibility-mode' +title: JavaScript compatibility mode +menuTitle: JavaScript mode +excerpt: 'k6 supports running test scripts with different ECMAScript compatibility modes using --compatibility-mode' _build: list: false weight: 19 --- -# JavaScript Compatibility Mode +# JavaScript compatibility mode -You can run test scripts with different ECMAScript compatibility modes with the -`run --compatibility-mode` CLI option or `K6_COMPATIBILITY_MODE` environment variable. +You can write k6 scripts in various ECMAScript versions: -Currently two modes are available: +- ES6+ JavaScript with ES modules (ESM). +- ES6+ JavaScript with CommonJS modules. -## Base +k6 supports both module types and most ES6+ features in all k6 execution modes: local, distributed, and cloud. + +To enable ES module support, k6 uses [Babel](https://babeljs.io/) internally to transform ESM to CommonJS. The process is as follows: + +![Babel transformation in k6](/media/docs/k6-oss/diagram-grafana-k6-babel-pipeline.png) + +Some users prefer to bundle their test code outside k6. For this reason, k6 offers two JavaScript compatibility modes: + +- [Extended mode](#extended-mode): The default option, supporting ESM and most ES6+ features. +- [Base mode](#base-mode): Limited to CommonJS, excluding the Babel step. + +When running tests, you can change the mode by using the `--compatibility-mode` option: + +| Env | CLI | Code / Config file | Default | +| ----------------------- | ---------------------- | ------------------ | ------------ | +| `K6_COMPATIBILITY_MODE` | `--compatibility-mode` | N/A | `"extended"` | + +## Extended mode + +By default, k6 uses the `--compatibility-mode=extended` mode: {{< code >}} ```bash +$ k6 run script.js +``` + +{{< /code >}} + +As illustrated in the previous diagram, if k6 detects unsupported ES+ features while parsing the test script, it then transforms the script with Babel to polyfill the unsupported features. + +Currently, the k6 Babel transformation only adds ESM support and sets `global` (node's global variable) with the value of `globalThis`. + +## Base mode + +{{< code >}} + +```cli $ k6 run --compatibility-mode=base script.js ``` -```bash +```env $ K6_COMPATIBILITY_MODE=base k6 run script.js ``` {{< /code >}} -Pure Golang JavaScript VM supporting ES5.1+. Use this mode if your scripts are already written -using only ES5.1 features, or were previously transformed by [Babel](https://babeljs.io/), -to reduce startup time, RAM usage and improve performance. See the [k6-es6 project](https://github.com/k6io/k6-es6) -for an example [Webpack](https://webpack.js.org/) setup that does this -transformation outside of k6. +The base mode omits the Babel transformation step, supporting only ES5.1+ code. You may want to enable this mode if your scripts are already written using only ES5.1 features or were previously transformed by Babel. -> ### ⚠️ Disclaimer -> -> Your mileage may vary while running `--compatibility-mode=base` and also importing external dependencies. For instance, -> `xml2js` and `cheerio` currently do not work, while `lodash` does. +Generally, this mode is not recommended as it offers minor benefits in reducing startup time. -### Basic Example +### CommonJS Example {{< code >}} @@ -69,133 +96,16 @@ module.exports.default = function () { > but it does _not_ support the > [Node.js module resolution algorithm](https://nodejs.org/api/modules.html#modules_all_together). -### Advanced Example +## Bundling with Babel outside of k6 -{{< code >}} - -```javascript -const http = require('k6/http'); -const metrics = require('k6/metrics'); -const k6 = require('k6'); +The examples below demonstrate the use of Babel with bundlers like [Webpack](https://webpack.js.org/) and [Rollup](https://rollupjs.org/): -module.exports.options = { - stages: [ - { duration: '30s', target: 20 }, - { duration: '1m30s', target: 10 }, - { duration: '20s', target: 0 }, - ], - thresholds: { - 'failed requests': ['rate<0.1'], - }, -}; +- [k6-template-es6](https://github.com/grafana/k6-template-es6): Template using Webpack and Babel to bundle k6 tests. +- [k6-rollup-example](https://github.com/grafana/k6-rollup-example): Example using Rollup and Babel to bundle a testing project. -const myFailRate = new metrics.Rate('failed requests'); +## Read more -module.exports.default = function () { - const res = http.get('https://httpbin.test.k6.io/'); - const checkRes = k6.check(res, { - 'status was 200': function (r) { - return r.status == 200; - }, - }); - if (!checkRes) { - myFailRate.add(1); - } - k6.sleep(1); -}; -``` - -{{< /code >}} - -## Extended - -{{< code >}} - -```bash -$ k6 run --compatibility-mode=extended script.js -``` - -```bash -$ K6_COMPATIBILITY_MODE=extended k6 run script.js -``` - -{{< /code >}} - -In case of syntax/parsing errors, the script will be transformed using Babel with specific plugins bringing the compatibility to ES2015(ES6)+. This means that features such as classes and arrow functions can be used. This does take some time to transpile and the produced code has slightly different line/column numbers. - -Before v0.31.0, k6 included [core.js](https://github.com/zloirock/core-js) v2 and even more Babel plugins in extended mode. -This added around 2MB extra memory usage per VU and some of the transformations (generators, async/await) of Babel were still insufficient to get k6 working with these features. - -## Performance Comparison - -There's a substantial difference in performance between both modes, as shown by -[GNU time](https://www.gnu.org/software/time/) below, especially when running tests with a large -number of VUs: - -{{< code >}} - -```bash -$ /usr/bin/time -v k6 run \ - --compatibility-mode=base \ - --vus 3500 \ - --duration=60s \ - script.js - -[...] -User time (seconds): 15.10 -System time (seconds): 10.02 -Percent of CPU this job got: 40% -Elapsed (wall clock) time (h:mm:ss or m:ss): 1:01.88 -Average shared text size (kbytes): 0 -Average unshared data size (kbytes): 0 -Average stack size (kbytes): 0 -Average total size (kbytes): 0 -Maximum resident set size (kbytes): 903612 -Average resident set size (kbytes): 0 -Major (requiring I/O) page faults: 1 -Minor (reclaiming a frame) page faults: 352090 -Voluntary context switches: 558479 -Involuntary context switches: 4689 -Swaps: 0 -File system inputs: 0 -File system outputs: 78856 -Socket messages sent: 0 -Socket messages received: 0 -Signals delivered: 0 -Page size (bytes): 4096 -Exit status: 0 -``` - -```bash -$ /usr/bin/time -v k6 run \ - --compatibility-mode=extended \ - --vus 3500 \ - --duration=60s \ - script.js - -[...] -User time (seconds): 104.44 -System time (seconds): 6.96 -Percent of CPU this job got: 101% -Elapsed (wall clock) time (h:mm:ss or m:ss): 1:49.49 -Average shared text size (kbytes): 0 -Average unshared data size (kbytes): 0 -Average stack size (kbytes): 0 -Average total size (kbytes): 0 -Maximum resident set size (kbytes): 7972316 -Average resident set size (kbytes): 0 -Major (requiring I/O) page faults: 1 -Minor (reclaiming a frame) page faults: 2595676 -Voluntary context switches: 535511 -Involuntary context switches: 9306 -Swaps: 0 -File system inputs: 0 -File system outputs: 78856 -Socket messages sent: 0 -Socket messages received: 0 -Signals delivered: 0 -Page size (bytes): 4096 -Exit status: 0 -``` - -{{< /code >}} +- [Native ESM support](https://github.com/grafana/k6/issues/3265): GitHub issue for native ESM support in k6. This feature aims to eliminate the Babel transformation step within k6. +- [Running large tests](https://grafana.com/docs/k6//testing-guides/running-large-tests): Optimize k6 for better performance. +- [k6 Modules](https://grafana.com/docs/k6//using-k6/modules): Different options to import modules in k6. +- [k6 Archive Command](https://grafana.com/docs/k6//misc/archive): The `k6 archive` command bundles all k6 test dependencies into a `tar` file, which can then be used for execution. It may also reduce the execution startup time. diff --git a/docs/sources/v0.49.x/using-k6/modules.md b/docs/sources/v0.49.x/using-k6/modules.md index 8f13aaf8fc..c7f7b6b1d1 100644 --- a/docs/sources/v0.49.x/using-k6/modules.md +++ b/docs/sources/v0.49.x/using-k6/modules.md @@ -10,11 +10,12 @@ weight: 07 ## 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: +In k6, you can import different kinds of modules: - [Built-in modules](#built-in-modules) - [Local filesystem modules](#local-filesystem-modules) - [Remote HTTP(S) modules](#remote-https-modules) +- [Extension modules](#extension-modules) ### Built-in modules @@ -27,19 +28,11 @@ For the full list of built-in modules, refer to the [API documentation](https:// import http from 'k6/http'; ``` -### Local filesystem modules +### Local 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. +These modules are stored on the local filesystem, and accessed either through relative or absolute filesystem paths. -```javascript -//helpers.js -export function someHelper() { - // ... -} -``` +k6 adopts a **browser-like module resolution** and doesn't support [Node.js module resolution](https://nodejs.org/api/modules.html#modules_all_together). File names for `imports` must be fully specified, such as `./helpers.js`. ```javascript //my-test.js @@ -50,13 +43,22 @@ export default function () { } ``` -### Remote HTTP(S) modules +```javascript +//helpers.js +export function someHelper() { + // ... +} +``` + +### Remote modules -These modules are accessed over HTTP(S), from a source like [the k6 JSLib](#the-jslib-repository) or +These modules are accessed over HTTP(S), from a public source like GitHub, any CDN, 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**. +For example, [jslib](https://grafana.com/docs/k6//javascript-api/jslib) is a set of k6 JavaScript libraries available as remote HTTPS modules. They can be downloaded and imported as local modules or directly imported as remote modules. + ```javascript import { randomItem } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; @@ -65,282 +67,105 @@ export default function () { } ``` -### The JSLib repository +You can also build your custom Javascript libraries and distribute them via a public web hosting. For reference, [k6-jslib-aws](https://github.com/grafana/k6-jslib-aws) and [k6-rollup-example](https://github.com/grafana/k6-rollup-example) host their modules as GitHub release assets. -**JSLib** is a set of libraries known to work well with k6. It is available at https://jslib.k6.io/. +### Extension modules -These libraries can either be downloaded and included with the test project or loaded directly using HTTP imports. +Like the [k6 APIs](https://grafana.com/docs/k6//javascript-api), you can build custom modules in Go code and expose them as JavaScript modules. These custom Go-to-JS modules are known as [k6 extensions](https://grafana.com/docs/k6//extensions). -## Bundling node modules - -{{% admonition type="caution" %}} - -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. - -{{% /admonition %}} - -The steps of this tutorial are as follows: - -1. [Setting up the bundler](#setting-up-the-bundler) - 1. [Installing packages](#installing-packages) - 1. [Configuring Webpack](#configuring-webpack) - 1. [Adding a bundle command](#adding-a-bundle-command) -2. [Running the bundling](#running-the-bundling) -3. [Running the tests](#running-the-tests) - -{{% admonition type="note" %}} - -A [ready-to-use Webpack and Babel starter](https://github.com/k6io/k6-es6) is avaialble in a repository. - -{{% /admonition %}} - -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: +Below is an example that imports the `k6/x/kubernetes` module from the [xk6-kubernetes](https://github.com/grafana/xk6-kubernetes) extension. ```javascript -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](https://github.com/webpack/webpack), -[parcel](https://github.com/parcel-bundler/parcel), [rollup](https://github.com/rollup/rollup) -and [browserify](https://github.com/browserify/browserify). - -Due to its flexibility, ease of use, relatively low resource consumption, and known compatibility -with k6, it is recommended to use [webpack](https://github.com/webpack/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](https://grafana.com/docs/k6//using-k6/javascript-compatibility-mode#performance-comparison). - -### 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: - -{{< code >}} - -```bash -$ mkdir ./example-project && \ - cd "$_" && \ - npm init -y -``` - -{{< /code >}} - -#### Installing packages - -Then, install the packages needed: - -{{< code >}} - -```bash -$ npm install --save-dev \ - webpack \ - webpack-cli \ - @types/k6 \ - babel-loader \ - @babel/core \ - @babel/preset-env \ - core-js -``` - -{{< /code >}} - -| Package | Usage | -| :---------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| [webpack](https://github.com/webpack/webpack) | The bundler part of Webpack | -| [webpack-cli](https://github.com/webpack/webpack-cli) | The CLI part of Webpack, which allows us to use it from the terminal | -| [@types/k6](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/k6) | k6 Typescript definition | -| [babel-loader](https://github.com/babel/babel-loader) | A loader used by Webpack to leverage babel functionality while bundling | -| [@babel/core](https://github.com/babel/babel/tree/master/packages/babel-core) | The core functionality of Babel | -| [@babel/preset-env](https://github.com/babel/babel/tree/master/packages/babel-preset-env) | A smart preset using [browserlist](https://github.com/browserslist/browserslist), [compat-table](https://github.com/kangax/compat-table) and [electron-to-chromium](https://github.com/Kilian/electron-to-chromium) to determine what code to transpile and polyfill. | -| [core-js](https://github.com/zloirock/core-js) | A 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: - -{{< code >}} - -```javascript -const path = require('path'); - -module.exports = { - mode: 'production', - entry: { - login: './src/login.test.js', - signup: './src/signup.test.js', +import { Kubernetes } from 'k6/x/kubernetes'; + +const podSpec = { + apiVersion: 'v1', + kind: 'Pod', + metadata: { name: 'busybox', namespace: 'testns' }, + spec: { + containers: [ + { + name: 'busybox', + image: 'busybox', + command: ['sh', '-c', 'sleep 30'], + }, + ], }, - output: { - path: path.resolve(__dirname, 'dist'), // eslint-disable-line - libraryTarget: 'commonjs', - filename: '[name].bundle.js', - }, - module: { - rules: [{ test: /\.js$/, use: 'babel-loader' }], - }, - target: 'web', - externals: /k6(\/.*)?/, }; +export default function () { + const kubernetes = new Kubernetes(); + kubernetes.create(podSpec); + const pods = kubernetes.list('Pod', 'testns'); + pods.map(function (pod) { + console.log(pod.metadata.name); + }); +} ``` -{{< /code >}} - -`Mode` - -Tells Webpack to automatically use the optimizations associated with the `mode`. -Additional details available in [the webpack docs](https://webpack.js.org/configuration/mode/). +How do k6 extensions (Go-to-JS modules) work? For enhanced performance, the k6 engine is written in Go and embeds a JavaScript VM ([goja](https://github.com/dop251/goja)) to execute JavaScript test code. That allows you to build your modules in Go code and import them as JavaScript as usual. -`Entry` +To learn more about using or creating k6 extensions, refer to the [Extension documentation](https://grafana.com/docs/k6//extensions). -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: +## Sharing JavaScript modules -{{< code >}} +As mentioned previously, users can import custom JavaScript libraries by loading either local or remote modules. Because of that, we have two options to import JavaScript modules, along with various methods to distribute them. -```javascript -// login.test.js +{{< admonition type="note" >}} -import { SomeService } from './some.service.js'; +The following options for distributing and sharing JavaScript libraries are available for both custom and other public libraries. -const svc = new SomeService(); -``` +{{< /admonition >}} -{{< /code >}} +**As remote modules** -and - -{{< code >}} +You can host your modules in a public webserver like GitHub and any CDN and be imported remotely. ```javascript -// some.service.js - -import * as lodash from 'lodash'; +// As GitHub release assets +import { + WorkloadConfig, + sayHello, +} from 'https://github.com/grafana/k6-rollup-example/releases/download/v0.0.2/index.js'; -export class SomeService { - constructor() { - this._ = lodash; - } -} +// or hosted in a CDN +import { randomIntBetween, randomItem } from 'https://jslib.k6.io/k6-utils/1.4.0/index.js'; ``` -{{< /code >}} - -would result in Webpack bundling `login.test.js`, `some.service.js` and all upstream dependencies -utilized by `lodash`. - -`Output` +When the library consists of multiple files and modules, you may want to bundle these modules to create public releases. Here are some examples for reference: -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. +- Using Webpack: [k6-jslib-utils](https://github.com/grafana/k6-jslib-utils) and [k6-jslib-aws](https://github.com/grafana/k6-jslib-aws). +- Using Rollup: [test-commons](https://github.com/grafana/k6-rollup-example/). -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](https://webpack.js.org/configuration/output/#outputlibrarytarget). +Be aware that k6 automatically executes remote modules, making it crucial to trust the source code of these remote modules. There is a **risk of altering the remote modules with certain hosting mechanisms**. To mitigate this security risk, some users prefer to download and import the modules locally to ensure full control of the source code. -The `filename` key, as the name suggests, configures the name of the finished bundles. In this -example, the [template string](https://webpack.js.org/configuration/output/#template-strings) `[name]` -is used to add a dynamic part to the output filename. +**As local modules** -#### Adding a bundle command +In this example, the previous remote modules have been downloaded to the `lib` folder of the testing project and imported as follows: -Open the `package.json` file and add a new script entry, used for running the bundling process. - -```diff -{ - "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: - -{{< code >}} - -```bash -$ npm run bundle -# ... -$ tree dist - -dist -├── login.bundle.js -└── signup.bundle.js +```javascript +import { WorkloadConfig, sayHello } from './libs/test-commons.js'; -0 directories, 2 files +import { randomIntBetween, randomItem } from './libs/k6-utils.js'; ``` -{{< /code >}} +Another option to distribute libraries is to use a package manager tool like npm, which enables version locking and the linking of local libraries. The latter can be useful during development. -### Running the tests +Although k6 does not resolve node modules, you can utilize a Bundler to load npm dependencies, as shown in the [k6-rollup-example](https://github.com/grafana/k6-rollup-example). -{{< code >}} +## Using TypeScript -```bash -$ npm run bundle -# ... -$ k6 run dist/login.bundle.js -# ... -``` +k6 does not natively support TypeScript. If you wish to write k6 tests in Typescript, you will need a bundler, as demonstrated in the previous examples: -{{< /code >}} +- Using Webpack: Refer to [k6-template-typescript](https://github.com/grafana/k6-template-typescript) and [k6-jslib-aws](https://github.com/grafana/k6-jslib-aws). +- Using Rollup: Apply the [@rollup/plugin-typescript](https://github.com/rollup/plugins/tree/master/packages/typescript) to the [k6-rollup-example](https://github.com/grafana/k6-rollup-example). -{{< code >}} +## Using modules with Docker -```bash -$ npm run bundle -# ... -$ k6 run dist/signup.bundle.js \ - --vus 10 \ - --duration 10s -# ... -``` - -{{< /code >}} +Built-in and remote modules work out of the box when running k6 in a Docker container like the [Grafana k6 Docker image](https://hub.docker.com/r/grafana/k6). -## Using local modules with Docker +### Local modules -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](https://docs.docker.com/engine/admin/volumes/volumes/), so that k6 can see all the JS modules it needs to import. +To run k6 with Docker and import a local module, you must make sure to mount the necessary folders from the host into the container, using [Docker volumes](https://docs.docker.com/engine/admin/volumes/volumes/). Thus, k6 can see all the JS modules it needs to import. For example, say you have the following structure on your host machine: @@ -380,11 +205,32 @@ $ docker run --rm -v /home/k6/example/src:/src -i grafana/k6 run /src/index.js {{< /code >}} 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: +has been marked for sharing in the Docker Desktop settings. + +### Extension modules + +The official [Grafana k6 Docker image](https://hub.docker.com/r/grafana/k6) includes the k6 release binary but lacks additional k6 extensions. Therefore, using the official Docker container to run a k6 test that requires an extension will fail. + +To run k6 with extensions in Docker, create a Docker image that includes the k6 binary with any extension you may want to use. Define a `Dockerfile` with the necessary [xk6 build](https://grafana.com/docs/k6//extensions/build-k6-binary-using-go#breaking-down-the-xk6-command) instructions as follows: + +```bash +FROM grafana/xk6:latest + +RUN GCO_ENABLED=0 xk6 build \ + --with github.com/grafana/xk6-kubernetes@latest + +ENTRYPOINT ["./k6"] +``` + +After building your custom k6 Docker image, you can [run k6 with Docker](https://grafana.com/docs/k6//get-started/running-k6/) as usual. -![Running k6 in docker on Windows](/media/docs/k6-oss/running-k6-in-docker-on-windows.png) +Alternatively, you can implement a multistage Dockerfile build such as shown on this [Dockerfile example](https://github.com/grafana/xk6-output-prometheus-remote/blob/main/Dockerfile). ## Read more -- [ES6 template](https://github.com/k6io/template-es6): a scaffolding project to use ES6 in your k6 scripts. -- [TypeScript template](https://github.com/k6io/template-typescript): a scaffolding project to use TypeScript in your k6 scripts. +- [JSLib](https://grafana.com/docs/k6//javascript-api/jslib): A collection of k6 JavaScript libraries maintained by Grafana Labs and available as remote modules. +- [Extensions directory](https://grafana.com/docs/k6//extensions/explore): A collection of k6 extensions maintained by Grafana Labs and the community. +- [k6-rollup-example](https://github.com/grafana/k6-rollup-example): Example using Rollup and Babel to bundle a common library and testing suite. +- [k6-template-es6](https://github.com/grafana/k6-template-es6): Template using Webpack and Babel to bundle k6 tests into CommonJS modules and polyfill ES+ features. +- [k6-template-typescript](https://github.com/grafana/k6-template-typescript): Template using Webpack and Babel to use TypeScript in your k6 scripts. +- [JavaScript Compatibility Mode](https://grafana.com/docs/k6//using-k6/javascript-compatibility-mode): An option to change the ECMAScript version supported by k6.