No results for

Powered byAlgolia

Archive Command

What is an archive?

When the complexity of a k6 test goes beyond a single JS file it quickly becomes cumbersome to find and bundle up all the dependencies (JS, open()'ed data files, TLS client certs, etc.). k6 archives are a native way to bundle and distribute, or share, a test.

A k6 archive is simply a tar file with all files needed to execute a k6 test.

How to create and run an archive

Let's say that you normally execute a test using:

1$ k6 run script.js

Now if you replace run with archive k6 will run the init stage of the code to determine which JS files are being imported and what data files are being open()'ed and bundles all of the files up into a tar file:

1$ k6 archive script.js

This would produce a tar file on disk called archive.tar (you can change that by setting -O filename.tar). It's also easy to run an archive, as k6 run is compatible with archive files you can execute:

⚠️ Overriding options

As always you can override options using CLI flags or environment variables when running an archive.

1$ k6 run archive.tar

Use cases

Archive files have a variety of use cases, but they all share the common need to bundle of a test's files into a single file for easy distribution.

Sharing a test

By bundling up a test into an archive it's easy to share the test with your teammates by simply storing or sending a single tar file. As we saw in the previous section, your teammates can execute the archive by running k6 run archive.tar.

Preparing tests for CI

If you have a complex CI pipeline and your load tests are separated from your application code, you could store k6 archives as build artifacts whenever the load test source code is changed, and then pull in those k6 archives from the artifacts storage for test execution as needed.

k6 Cloud Execution

k6 offers a commercial service for running large scale and geographically distributed load tests on managed cloud infrastructure. Cloud executed tests are triggered from the k6 command-line via the k6 cloud script.js command (similar to k6 run) which will trigger an implicit creation of a k6 archive that is uploaded and distributed to k6 cloud load generators for execution.

Distributed Execution

k6-operator can distribute a k6 test across a Kubernetes cluster.

When a k6 test has multiple files, you can use the archive functionality to bundle the k6 test in a single archived file and pass this file to run the test.

Contents of an archive file

An archive contains the original source of the JS code, any open()'ed data files, SSL/TLS client certificates as well as a metadata.json with all the options (a cascading of the options set on the CLI, via Environment variables and in-script options (export let options = {...})).

Let's create an archive from the following sample test. Here is the layout in the filesystem of the files:

Sample test structure
1/home/johndoe/tests/api-test $ tree
3├── utils
4| └-- common.js
5├── endpoints
6| ├── login.js
7| └-- search.js
8├── node_modules
9| └-- somelib
10| └-- lib.js
11├── data
12| └-- users.json
13└-- script.js

Now, if the current working directory is /home/johndoe/tests/api-test/ and we run k6 archive script.js we'd get a tar file called archive.tar (you can change the name of the file using -O filename.tar). The contents of the archive file would look like something like this:

Structure of archive.tar
1├-- data
2├-- files
3| └-- home
4| └-- nobody <-- the username has been anonymized (see section further down)
5| └-- tests
6| └-- api-test
7| └-- data
8| └-- users.json
9├-- metadata.json
10└-- scripts
11 └-- home
12 └-- nobody <-- the username has been anonymized (see section further down)
13 └-- tests
14 └-- api-test
15 ├-- script.js
16 ├-- utils
17 | └-- common.js
18 ├-- endpoints
19 | ├-- login.js
20 | └-- search.js
21 └-- node_modules
22 └-- somelib
23 └-- lib.js

Breaking down the file structure we get:

data contains the source code of the main JS file (script.js in this example).

files contains the full original directory tree of all open()'ed data files.

metadata.json The resolved "default" options for this test based on CLI flags, Environment variables and in-script options.

scripts contains the full original directory tree of all import'ed JS dependencies.

2 "type": "js",
3 "options": {
4 "paused": null,
5 "vus": null,
6 "duration": null,
7 "iterations": null,
8 "stages": null,
9 "setupTimeout": null,
10 "teardownTimeout": null,
11 "rps": null,
12 "maxRedirects": null,
13 "userAgent": null,
14 "batch": null,
15 "batchPerHost": null,
16 "httpDebug": null,
17 "insecureSkipTLSVerify": null,
18 "tlsCipherSuites": null,
19 "tlsVersion": {
20 "min": "",
21 "max": ""
22 },
23 "tlsAuth": null,
24 "throw": null,
25 "thresholds": null,
26 "blacklistIPs": null,
27 "hosts": null,
28 "noConnectionReuse": null,
29 "ext": null,
30 "summaryTrendStats": null,
31 "systemTags": [
32 "url",
33 "name",
34 "check",
35 "error",
36 "tls_version",
37 "method",
38 "subproto",
39 "status",
40 "group",
41 "proto"
42 ],
43 "tags": null
44 },
45 "filename": "/home/johndoe/tests/api-test/script.js",
46 "pwd": "/home/johndoe/tests/api-test/",
47 "env": {}

What an archive file does not contain

We try to be cautious with what we include in an archive file. Some things we do to that end:

  • We anonymize the username found in any path to JS and data file dependencies
  • By default, k6 omits environment variables from the system in the archive. However, if you use the flag --include-system-env-vars, k6 includes the environment variables in the archive (along with any variables passed with --env). To avoid including system environment variables in the archive, you can use the k6 archive flag --exclude-env-vars.