What is the mechanism for validating data integrity after a load test run in k6
0 reputation · 25 Jan 2022, 15:31 UTC
0 reputation · 25 Jan 2022, 15:31 UTC
The primary goal is to confirm that assertions within a k6 script correctly validate application behavior during a load test.
k6 does not manage external data persistence or backup operations.
35275 reputation · 26 Jan 2022, 02:52 UTC
k6 does not validate data integrity against an external data source after a load test run completes. Its assertion mechanism operates during execution: check() evaluates response properties and records pass/fail metrics, but it does not query databases or external services once iterations finish. For post-test integrity analysis, the standard mechanism is the built-in JSON output captured with --out json=results.json, paired with separate external tooling to query the target data store.
Cross-request state validation. Within a single virtual user (VU), you can validate state across requests by storing response values in local variables and passing them into subsequent check() calls. Cross-VU state is different: VUs are isolated by default, so shared state requires a SharedArray or an external module. Neither approach lets assertions query an external data source directly after the run.
Output format. JSON is the standard k6 output for post-test integrity checks because it preserves per-check metrics, request timings, and tags in a structured file that external scripts can parse. Other outputs exist, but JSON is the most straightforward for offline integrity comparison.
check() during the run to validate response payloads and capture identifiers (IDs, tokens, timestamps) needed for later comparison.k6 run --out json=results.json script.js.Run a minimal script with a check() and the JSON output flag, then inspect the file for check metrics:
k6 run --out json=results.json script.js
Confirm the JSON contains entries for each check. For external validation, use a database client to query the target store and compare counts or records against the identifiers captured during the run.
Thresholds can fail the overall test when check failure rates exceed a limit, but they only evaluate metrics collected during the run; they do not validate persisted data.
This assumes a standard k6 distribution where JSON output is built in. If your requirement is strictly post-run validation against an external database, k6 alone is insufficient—plan for an external verification script. If you only need to confirm that responses were correct during the load test, check() with JSON output is the correct mechanism.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 26 Jan 2022, 00:05 UTC
It is important to clarify that k6's check() operates strictly during the test execution phase, validating response properties against the immediate request context.
For validating data persistence or integrity against an external store after the run concludes, the --out json output is necessary, but this requires subsequent external scripting (e.g., Python/Node) to query the actual database using IDs captured during the test.