|
1 | 1 | ---
|
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' |
4 | 5 | _build:
|
5 | 6 | list: false
|
6 | 7 | weight: 19
|
7 | 8 | ---
|
8 | 9 |
|
9 |
| -# JavaScript Compatibility Mode |
| 10 | +# JavaScript compatibility mode |
10 | 11 |
|
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: |
13 | 13 |
|
14 |
| -Currently two modes are available: |
| 14 | +- ES6+ JavaScript with ES modules (ESM). |
| 15 | +- ES6+ JavaScript with CommonJS modules. |
15 | 16 |
|
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 | + |
| 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: |
17 | 37 |
|
18 | 38 | {{< code >}}
|
19 | 39 |
|
20 | 40 | ```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 |
21 | 55 | $ k6 run --compatibility-mode=base script.js
|
22 | 56 | ```
|
23 | 57 |
|
24 |
| -```bash |
| 58 | +```env |
25 | 59 | $ K6_COMPATIBILITY_MODE=base k6 run script.js
|
26 | 60 | ```
|
27 | 61 |
|
28 | 62 | {{< /code >}}
|
29 | 63 |
|
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. |
35 | 65 |
|
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. |
40 | 67 |
|
41 |
| -### Basic Example |
| 68 | +### CommonJS Example |
42 | 69 |
|
43 | 70 | {{< code >}}
|
44 | 71 |
|
@@ -69,133 +96,16 @@ module.exports.default = function () {
|
69 | 96 | > but it does _not_ support the
|
70 | 97 | > [Node.js module resolution algorithm](https://nodejs.org/api/modules.html#modules_all_together).
|
71 | 98 |
|
72 |
| -### Advanced Example |
| 99 | +## Bundling with Babel outside of k6 |
73 | 100 |
|
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/): |
80 | 102 |
|
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. |
91 | 105 |
|
92 |
| -const myFailRate = new metrics.Rate('failed requests'); |
| 106 | +## Read more |
93 | 107 |
|
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