Analyze and improve an existing k6 script
Use the script from an earlier milestone, from the Grafana Cloud k6 script editor, or paste any k6 script you want reviewed.
To improve an existing script, complete these steps.
Paste your current script into k6 Script Authoring mode. If you have an OpenAPI or Swagger spec for the service under test, attach or paste it in the same thread so the output can be compared to the contract. Refer to Use k6 Script Authoring mode and Enrich with an OpenAPI spec.
Ask for a focused analysis using a prompt like this one.
Analyze this script and identify missing checks, weak or missing thresholds, cardinality risks, and pacing problems, for example, missing
sleep().Review the findings and map each one to code changes. Consider the following items.
- Add
check()assertions for critical responses. For QuickPizza, require at minimum HTTP 200 on the main GET. - Add threshold criteria in
options.thresholdsif you want CI-style pass/fail. - Replace dynamic URL labels with URL grouping patterns when paths include IDs.
- Add pacing (
sleep) or adjust scenarios when iterations would hammer the host.
- Add
Enter a prompt to apply the fixes, for example:
Apply those fixes and explain each change.
To open the k6 script editor, enter a prompt such as “Open the k6 script editor.” Select the k6 project that should hold the test. Run a short smoke check from the editor with Create and Run. Refer to Open the k6 script editor and Use the script editor.
If you prefer a local check, copy the updated script into a file such as
your-script.js, then run this command.k6 run --iterations 1 your-script.jsIf you keep a local copy, diff in your editor against the previous version and keep only changes that match your test intent.
You now know how to use Script Authoring mode to review scripts in Cloud or locally. In the next milestone, you iterate quickly using follow-up prompts.
At this point in your path, you can explore the following topics: