---
title: "Analyze and improve an existing k6 script | Grafana Labs"
description: "Use Script Authoring mode to find weak spots in an existing script, apply concrete fixes, and validate in the Cloud script editor first when possible."
---

> For a curated documentation index, see [llms.txt](/llms.txt). For the complete documentation index, see [llms-full.txt](/llms-full.txt).

# 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.

1. 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](/docs/grafana-cloud/testing/k6/author-run/k6-script-authoring-mode/#write-a-new-test) and [Enrich with an OpenAPI spec](/docs/grafana-cloud/testing/k6/author-run/k6-script-authoring-mode/#enrich-with-an-openapi-spec).
2. 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()`.
3. 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.thresholds` if 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.
4. Enter a prompt to apply the fixes, for example:
   
   > Apply those fixes and explain each change.
5. 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](/docs/grafana-cloud/testing/k6/author-run/k6-script-authoring-mode/#open-the-k6-script-editor) and [Use the script editor](/docs/grafana-cloud/testing/k6/author-run/script-editor/).
6. If you prefer a local check, copy the updated script into a file such as `your-script.js`, then run this command.
   
   Bash ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy
   
   ```bash
   k6 run --iterations 1 your-script.js
   ```
7. If 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.
