Skip to content

Commit c6a77ca

Browse files
ppcanoheitortsergentyorugac
authored
Update Modules and JavaScript Compatibility Mode docs (#1489)
* Update Modules and JavaScript Mode docs * Update Javascript Compatibility Mode * Port changes to v0.49.x * Update docs/sources/next/using-k6/javascript-compatibility-mode.md Co-authored-by: Heitor Tashiro Sergent <[email protected]> * Update docs/sources/next/using-k6/javascript-compatibility-mode.md Co-authored-by: Heitor Tashiro Sergent <[email protected]> * Update docs/sources/next/using-k6/javascript-compatibility-mode.md Co-authored-by: Heitor Tashiro Sergent <[email protected]> * Update docs/sources/next/using-k6/javascript-compatibility-mode.md Co-authored-by: Heitor Tashiro Sergent <[email protected]> * Update docs/sources/next/using-k6/modules.md Co-authored-by: Heitor Tashiro Sergent <[email protected]> * Update docs/sources/next/using-k6/javascript-compatibility-mode.md Co-authored-by: Heitor Tashiro Sergent <[email protected]> * Update docs/sources/next/using-k6/javascript-compatibility-mode.md Co-authored-by: Heitor Tashiro Sergent <[email protected]> * Update docs/sources/next/using-k6/javascript-compatibility-mode.md Co-authored-by: Heitor Tashiro Sergent <[email protected]> * Update docs/sources/next/using-k6/modules.md Co-authored-by: Heitor Tashiro Sergent <[email protected]> * Update docs/sources/next/using-k6/modules.md Co-authored-by: Heitor Tashiro Sergent <[email protected]> * Update docs/sources/next/using-k6/modules.md Co-authored-by: Heitor Tashiro Sergent <[email protected]> * Update docs/sources/next/using-k6/modules.md Co-authored-by: Heitor Tashiro Sergent <[email protected]> * Port Heitor changes to previous versions * Remove mention about improving startup time * Correct the Babel polyfill features * Update `Extension modules` instructions * Update docs/sources/next/using-k6/modules.md Co-authored-by: Olha Yevtushenko <[email protected]> * Fix brower typo --------- Co-authored-by: Heitor Tashiro Sergent <[email protected]> Co-authored-by: Olha Yevtushenko <[email protected]>
1 parent 6ac7e7b commit c6a77ca

File tree

12 files changed

+648
-1600
lines changed

12 files changed

+648
-1600
lines changed

docs/sources/next/extensions/build-k6-binary-using-docker.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ weight: 03
88

99
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.
1010

11+
{{% admonition type="note" %}}
12+
13+
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/<K6_VERSION>/using-k6/modules/#using-modules-with-docker).
14+
15+
{{% /admonition %}}
16+
1117
## Building your first extension
1218

1319
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:
Lines changed: 54 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,71 @@
11
---
2-
title: JavaScript Compatibility Mode
3-
description: 'k6 supports running test scripts with different ECMAScript compatibility modes using --compatibility-mode'
2+
title: JavaScript compatibility mode
3+
menuTitle: JavaScript mode
4+
excerpt: 'k6 supports running test scripts with different ECMAScript compatibility modes using --compatibility-mode'
45
_build:
56
list: false
67
weight: 19
78
---
89

9-
# JavaScript Compatibility Mode
10+
# JavaScript compatibility mode
1011

11-
You can run test scripts with different ECMAScript compatibility modes with the
12-
`run --compatibility-mode` CLI option or `K6_COMPATIBILITY_MODE` environment variable.
12+
You can write k6 scripts in various ECMAScript versions:
1313

14-
Currently two modes are available:
14+
- ES6+ JavaScript with ES modules (ESM).
15+
- ES6+ JavaScript with CommonJS modules.
1516

16-
## Base
17+
k6 supports both module types and most ES6+ features in all k6 execution modes: local, distributed, and cloud.
18+
19+
To enable ES module support, k6 uses [Babel](https://babeljs.io/) internally to transform ESM to CommonJS. The process is as follows:
20+
21+
![Babel transformation in k6](/media/docs/k6-oss/diagram-grafana-k6-babel-pipeline.png)
22+
23+
Some users prefer to bundle their test code outside k6. For this reason, k6 offers two JavaScript compatibility modes:
24+
25+
- [Extended mode](#extended-mode): The default option, supporting ESM and most ES6+ features.
26+
- [Base mode](#base-mode): Limited to CommonJS, excluding the Babel step.
27+
28+
When running tests, you can change the mode by using the `--compatibility-mode` option:
29+
30+
| Env | CLI | Code / Config file | Default |
31+
| ----------------------- | ---------------------- | ------------------ | ------------ |
32+
| `K6_COMPATIBILITY_MODE` | `--compatibility-mode` | N/A | `"extended"` |
33+
34+
## Extended mode
35+
36+
By default, k6 uses the `--compatibility-mode=extended` mode:
1737

1838
{{< code >}}
1939

2040
```bash
41+
$ k6 run script.js
42+
```
43+
44+
{{< /code >}}
45+
46+
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.
47+
48+
Currently, the k6 Babel transformation only adds ESM support and sets `global` (node's global variable) with the value of `globalThis`.
49+
50+
## Base mode
51+
52+
{{< code >}}
53+
54+
```cli
2155
$ k6 run --compatibility-mode=base script.js
2256
```
2357

24-
```bash
58+
```env
2559
$ K6_COMPATIBILITY_MODE=base k6 run script.js
2660
```
2761

2862
{{< /code >}}
2963

30-
Pure Golang JavaScript VM supporting ES5.1+. Use this mode if your scripts are already written
31-
using only ES5.1 features, or were previously transformed by [Babel](https://babeljs.io/),
32-
to reduce startup time, RAM usage and improve performance. See the [k6-es6 project](https://github.com/k6io/k6-es6)
33-
for an example [Webpack](https://webpack.js.org/) setup that does this
34-
transformation outside of k6.
64+
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.
3565

36-
> ### ⚠️ Disclaimer
37-
>
38-
> Your mileage may vary while running `--compatibility-mode=base` and also importing external dependencies. For instance,
39-
> `xml2js` and `cheerio` currently do not work, while `lodash` does.
66+
Generally, this mode is not recommended as it offers minor benefits in reducing startup time.
4067

41-
### Basic Example
68+
### CommonJS Example
4269

4370
{{< code >}}
4471

@@ -69,133 +96,16 @@ module.exports.default = function () {
6996
> but it does _not_ support the
7097
> [Node.js module resolution algorithm](https://nodejs.org/api/modules.html#modules_all_together).
7198
72-
### Advanced Example
99+
## Bundling with Babel outside of k6
73100

74-
{{< code >}}
75-
76-
```javascript
77-
const http = require('k6/http');
78-
const metrics = require('k6/metrics');
79-
const k6 = require('k6');
101+
The examples below demonstrate the use of Babel with bundlers like [Webpack](https://webpack.js.org/) and [Rollup](https://rollupjs.org/):
80102

81-
module.exports.options = {
82-
stages: [
83-
{ duration: '30s', target: 20 },
84-
{ duration: '1m30s', target: 10 },
85-
{ duration: '20s', target: 0 },
86-
],
87-
thresholds: {
88-
'failed requests': ['rate<0.1'],
89-
},
90-
};
103+
- [k6-template-es6](https://github.com/grafana/k6-template-es6): Template using Webpack and Babel to bundle k6 tests.
104+
- [k6-rollup-example](https://github.com/grafana/k6-rollup-example): Example using Rollup and Babel to bundle a testing project.
91105

92-
const myFailRate = new metrics.Rate('failed requests');
106+
## Read more
93107

94-
module.exports.default = function () {
95-
const res = http.get('https://httpbin.test.k6.io/');
96-
const checkRes = k6.check(res, {
97-
'status was 200': function (r) {
98-
return r.status == 200;
99-
},
100-
});
101-
if (!checkRes) {
102-
myFailRate.add(1);
103-
}
104-
k6.sleep(1);
105-
};
106-
```
107-
108-
{{< /code >}}
109-
110-
## Extended
111-
112-
{{< code >}}
113-
114-
```bash
115-
$ k6 run --compatibility-mode=extended script.js
116-
```
117-
118-
```bash
119-
$ K6_COMPATIBILITY_MODE=extended k6 run script.js
120-
```
121-
122-
{{< /code >}}
123-
124-
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.
125-
126-
Before v0.31.0, k6 included [core.js](https://github.com/zloirock/core-js) v2 and even more Babel plugins in extended mode.
127-
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.
128-
129-
## Performance Comparison
130-
131-
There's a substantial difference in performance between both modes, as shown by
132-
[GNU time](https://www.gnu.org/software/time/) below, especially when running tests with a large
133-
number of VUs:
134-
135-
{{< code >}}
136-
137-
```bash
138-
$ /usr/bin/time -v k6 run \
139-
--compatibility-mode=base \
140-
--vus 3500 \
141-
--duration=60s \
142-
script.js
143-
144-
[...]
145-
User time (seconds): 15.10
146-
System time (seconds): 10.02
147-
Percent of CPU this job got: 40%
148-
Elapsed (wall clock) time (h:mm:ss or m:ss): 1:01.88
149-
Average shared text size (kbytes): 0
150-
Average unshared data size (kbytes): 0
151-
Average stack size (kbytes): 0
152-
Average total size (kbytes): 0
153-
Maximum resident set size (kbytes): 903612
154-
Average resident set size (kbytes): 0
155-
Major (requiring I/O) page faults: 1
156-
Minor (reclaiming a frame) page faults: 352090
157-
Voluntary context switches: 558479
158-
Involuntary context switches: 4689
159-
Swaps: 0
160-
File system inputs: 0
161-
File system outputs: 78856
162-
Socket messages sent: 0
163-
Socket messages received: 0
164-
Signals delivered: 0
165-
Page size (bytes): 4096
166-
Exit status: 0
167-
```
168-
169-
```bash
170-
$ /usr/bin/time -v k6 run \
171-
--compatibility-mode=extended \
172-
--vus 3500 \
173-
--duration=60s \
174-
script.js
175-
176-
[...]
177-
User time (seconds): 104.44
178-
System time (seconds): 6.96
179-
Percent of CPU this job got: 101%
180-
Elapsed (wall clock) time (h:mm:ss or m:ss): 1:49.49
181-
Average shared text size (kbytes): 0
182-
Average unshared data size (kbytes): 0
183-
Average stack size (kbytes): 0
184-
Average total size (kbytes): 0
185-
Maximum resident set size (kbytes): 7972316
186-
Average resident set size (kbytes): 0
187-
Major (requiring I/O) page faults: 1
188-
Minor (reclaiming a frame) page faults: 2595676
189-
Voluntary context switches: 535511
190-
Involuntary context switches: 9306
191-
Swaps: 0
192-
File system inputs: 0
193-
File system outputs: 78856
194-
Socket messages sent: 0
195-
Socket messages received: 0
196-
Signals delivered: 0
197-
Page size (bytes): 4096
198-
Exit status: 0
199-
```
200-
201-
{{< /code >}}
108+
- [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.
109+
- [Running large tests](https://grafana.com/docs/k6/<K6_VERSION>/testing-guides/running-large-tests): Optimize k6 for better performance.
110+
- [k6 Modules](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/modules): Different options to import modules in k6.
111+
- [k6 Archive Command](https://grafana.com/docs/k6/<K6_VERSION>/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.

0 commit comments

Comments
 (0)