Log/Compliance/

Load testing an AI backend with k6: three bugs that mattered

Snap confinement, a phantom quiz slug and locked lessons each broke the k6 run in a different way. Here is what each one taught me.

I was working on the backend for a financial-education AI assistant. The compliance layer was mostly built: a prompt-injection guard, an output scanner, an audit ledger with HMAC chaining, role-based admin, and a lesson and quiz progression system wrapped around an LLM-powered chat. One piece of the test gate was still red on this machine: the k6 load phase. I spent a day getting it green.

Three bugs. All k6-related, none exotic, each one worth remembering.

Snap confinement broke script loading

k6 is installed as a snap on this Ubuntu machine. Snap confinement sandboxes the process so it only sees its own data directory and the user’s home. The repo lives under /media, which k6 cannot read under confinement, so passing the script path from there failed outright.

The fix was to stage the script into a cache folder under $HOME before each run and point k6 at the staged copy. The lesson: when a tool runs under snap or Flatpak, treat the sandbox like a Docker container with a surprising mount set, and stage inputs explicitly.

A phantom quiz slug

The load phase had a step called doQuiz that was meant to simulate a user working through a lesson’s end-of-section quiz. The slug in the script was money-management-quiz. That slug no longer existed in the curriculum, so the run came back as a wall of 404s. k6’s default output buries individual request errors under the aggregate, so the first signs of trouble looked nothing like a stale slug.

The fix was small: swap the dead slug for a current one. The quizzes already have unit and integration tests covering their logic, so the load test only needs a stable slug to pressure, not a tour of the whole quiz surface. The drift between the content folder and the test script was always going to break again. Hardcoded slugs rot faster than you expect.

Locked lessons returned 403

The script was calling doLesson with lesson slugs that were not guaranteed to be unlocked. Most lessons are locked until a user has completed earlier ones, so a cold test user hits a 403 on anything but the first lesson. The doLesson requests were all failing, but the aggregate hid it until I pulled per-request errors apart.

The fix was to hardcode doLesson to building-a-budget, the first lesson in Level 1 and always unlocked. Idempotent load generation needs a stable, always-reachable target. Progression logic belongs in behavioural tests, not throughput tests. Mixing the two is where I lost the day.

Result

The load phase passed, 28 of 28 scenarios green. The k6 harness itself was broken in three independent ways, and once each was fixed the backend held up fine under the planned load.

What I would do differently: write the load test against the running staging environment first, not the local snap-k6, and have the content pipeline publish a test-fixtures file that lists always-unlocked slugs. Then the load script reads that file instead of hardcoding a slug that will quietly rot the next time the curriculum changes. A person still has to look at the failing output and ask whether the bug is in the system or in the test. k6 will not tell you.

All entries · Book a call · Discuss on X