k6 Load Testing #7: Test Data with __ENV, -e, SharedArray, JSON and CSV Files (Real Terminal)
Code & Test Automation
0:00 / 0:00
k6 Load Testing #7: Test Data with __ENV, -e, SharedArray, JSON and CSV Files (Real Terminal)
4 просмотра · 1 дн. назад
Code & Test Automation
21 подписчик
4 просмотра · 1 дн. назад
Code & Test Automation | / @codetestautomation
Feed Grafana k6 tests with configuration and data: __ENV with a safe local default, -e versus shell environment variables, SharedArray with open() and JSON.parse, a hand-split CSV, __VU and __ITER, and exec.scenario.iterationInTest for unique data per iteration. Plus the real error when open() is used outside the init stage. Every run is real k6 output against a small local practice server.
In this video:
__ENV.BASE_URL with a default
k6 run -e and shell variables
--include-system-env-vars=false
users.json and SharedArray
__VU and __ITER
products.csv without a remote library
exec.scenario.iterationInTest
open() only in the init stage
Video 7 of 10 in the k6 Load Testing series with Grafana k6, for freshers and experienced test engineers on Windows, macOS and Linux: install, scripts, checks, thresholds, stages, scenarios and executors, test data, custom metrics, setup and teardown, and CI workflows. Almost every minute is a real terminal session with real k6 output, run only against a local practice server.
CHAPTERS
0:00 Introduction
0:51 Start the practice server
1:06 Configuration out of the script
1:52 __ENV with a default
2:49 Run with the default
3:13 Passing -e
4:02 System environment variables
4:45 Never secrets in scripts
5:15 A JSON data file
5:44 SharedArray
6:09 Where test data comes from
7:06 Picking a user per VU
7:35 Logging in
7:56 Run it
8:06 __VU and __ITER
8:39 A CSV data file
9:33 iterationInTest
10:27 Unique data per iteration
10:59 Bad data shows up in checks
12:25 open() only in init
13:16 Stop the server
13:26 Putting it together
13:43 Recap
14:19 Next episode
HOW THE DEMOS WERE MADE
Everything between the title card and the recap is a live screen recording of a real terminal: bash 5.2 in xfce4-terminal on Debian Linux, with the official k6 v2.3.0 release binary from GitHub (checksum verified) and the GNU nano 8.4 editor. Every script was typed at a human pace and every line of k6 output is real; nothing was edited in afterwards. SAFETY: every test in this video sends load ONLY to a small practice server (ShopKart, a Python standard-library app in the project zip) running on localhost on the same machine. Where the course notes use the public demo site test.k6.io, the video uses the local server instead and shows the original line as TYPED ONLY. Load is kept small (a few virtual users for seconds). No account was used: no sign-in to Grafana Cloud, no k6 cloud command, and anonymous usage reporting is switched off with K6_NO_USAGE_REPORT=true. Commands that were not run here (package managers that need sudo or admin rights, Docker, cloud commands, public URLs) are shown as TYPED ONLY: typed on screen, never run, and cancelled with Ctrl+C, so no output is ever invented.
CORRECTIONS TO THE COURSE NOTES
The notes' default BASE_URL 'https://test.k6.io' and '-e BASE_URL=https://example.com' are external sites; the video uses 'http://localhost:3333' and shows those lines as TYPED ONLY. Never load-test a site you do not own.
With k6 run, shell environment variables also appear in __ENV (k6 run --include-system-env-vars=false turns that off); a -e value wins over a shell variable of the same name.
__VU starts at 1, so index with (__VU - 1) % users.length. __ITER counts per VU, so __VU/__ITER combinations are easy to get wrong; for unique data per iteration use exec.scenario.iterationInTest from 'k6/execution'.
'Avoid loading large files inside the default function' is stronger in practice: open() inside the default function fails with 'the "open" function is only available in the init stage'.
k6 has no built-in CSV parser: the common papaparse import from jslib.k6.io is a remote URL (TYPED ONLY here); a small simple CSV can be split by hand inside the SharedArray function. SharedArray data is read-only ('SharedArray is immutable').
console.log output is hidden by k6 run -q (--quiet); run without -q (logs go to stderr, so use 2›&1 before a pipe).
PRACTICE
1. Start the practice server: python3 server/shop_server.py &
2. k6 run -e BASE_URL=http://127.0.0.1:3333 env-test.js 2›&1 | grep answered
3. Add a fifth row to products.csv and rerun data-test.js.
4. Change the options to 3 VUs and 12 iterations and check that every iteration number appears once.
5. Move open() into the default function and read the error.
Practice tip: install k6, unzip the project, start the local ShopKart practice server, and type every script right after you see it. Only ever point load tests at a server you own or have written permission to test; this series uses a local server on localhost for every run.
Next video: #08 Custom Metrics and Tags.
#k6 #LoadTesting #PerformanceTesting #GrafanaK6 #CodeAndTestAutomation
Subscribe to Code & Test Automation for more k6, performance testing, Java, coding and test automation tutorials: / @codetestautomation