Browse and order k6 scripts

You build browse and order k6 scripts that exercise the restaurant and order APIs and save reproducible before-number summaries.

Ticket

Build two k6 scripts in backend/loadtest/. The browse script drives restaurant-list and menu reads with virtual users. The order script sends new orders at a configured arrival rate.

These scripts establish the API's before numbers. The next exercise, "Find and fix the first bottleneck", compares the same workloads after the first fix. Keep the scripts and their inputs reproducible so the measurements remain comparable.

Both scripts must export const options, and the tests read it with k6 inspect, so options must be plain values computed at load time from __ENV.

Acceptance criteria

  • The browse script defaults to 300 virtual users and a duration of 3m.
  • The browse script's thresholds mention http_req_failed, http_req_duration, p(99)<300 and abortOnFail.
  • The order script defines exactly one scenario using ramping-arrival-rate. Its stages reach a peak target of 100 orders per second over a total duration of two minutes by default.
  • Both scripts include p(99) in summaryTrendStats.
  • With the API running, a browse run using VUS=5 and DURATION=6s sends at least 10 HTTP requests and records zero failed checks.
  • With the API running, an order run using PEAK=5, RAMP=2s and HOLD=4s sends at least 8 HTTP requests and records zero failed checks. Each iteration must create a new order with HTTP 201 rather than reuse an idempotency key and receive HTTP 200.
  • Both saved summaries contain positive http_req_duration values for med, p(95) and p(99), and include the http_req_failed metric.

Constraints

  • Keep the scripts at backend/loadtest/browse.js and backend/loadtest/order.js.
  • The browse script reads BASE_URL, VUS and DURATION from the environment. Default BASE_URL to http://localhost:8080, VUS to 300 and DURATION to 3m.
  • Use top-level vus and duration options for browsing, not scenarios. The default workload runs 300 VUs for three minutes.
  • Each browse iteration fetches the restaurant list, selects one returned restaurant and fetches its menu. Both reads must check for HTTP 200.
  • Give each virtual user its own X-User-ID. The API's rate limiter keys on that header and falls back to the client address when it is absent.
  • Include p(99) in the browse summary statistics. Its thresholds must mention http_req_failed, http_req_duration, p(99)<300 and abortOnFail.
  • The order script reads BASE_URL, PEAK, RAMP and HOLD from the environment. The default peak is 100 orders per second, with the stages lasting two minutes in total.
  • Use one ramping-arrival-rate scenario for the order workload.
  • Each order iteration sends its own X-User-ID and a fresh idempotency key of at most 36 characters. Check that a newly created order returns HTTP 201.
  • Save the browse summary as backend/loadtest/before-browse.json and the order summary as backend/loadtest/before-order.json.
  • Do not modify the provided tests.

Design direction

Keep browse.js and order.js separate. They represent different traffic models, so avoid hiding their configuration behind a shared abstraction.

For browsing, use a fixed number of virtual users (VUs) over a duration. For orders, use an open arrival-rate workload. The load test should continue targeting the configured arrival rate even when the API slows down.

Keep customer signup and menu discovery outside the measured order iterations. Find a restaurant with a usable menu item and construct a valid order line before the workload begins. This keeps setup traffic separate from the incoming order traffic.

Give every VU its own identity. A shared identity can make the rate limiter constrain the workload for the wrong reason. During each order iteration, generate a fresh idempotency key so the API creates a new order instead of returning a deduplicated response.

Include the required summary statistics in both scripts. A 1 percent error-rate threshold is a useful guard for the browse workload, and the next exercise checks whether the after error rate stays at or below 1 percent. Any layout that meets the contract passes.

How to check your work

Install k6 and start the infrastructure and seed data:

bash
brew install k6
cd backend
make up
make migrate
go run ./cmd/seed

In separate terminals, start the API and worker:

bash
cd backend
make run
bash
cd backend
make worker

Run both default workloads and save their summaries:

bash
cd backend
k6 run --summary-export=loadtest/before-browse.json loadtest/browse.js
k6 run --summary-export=loadtest/before-order.json loadtest/order.js

Run the exercise tests against the running API:

bash
cd backend
TEST_API_URL=http://localhost:8080 make test W=6B4

Inspect both JSON summaries for http_req_duration values under med, p(95) and p(99), and the http_req_failed metric. Record those measurements before changing the implementation. When a test fails, read the first failing assertion and identify the specific script, request, status code or metric it reports before investigating later failures.

Finished Exercise 6.11?

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. Add browse.js with 300 VUs for three minutes by default, listing restaurants and fetching a menu.
  2. Add order.js with an arrival-rate ramp to 100 orders per second over two minutes by default.
  3. Use distinct VU identities and unique idempotency keys for order iterations.
  4. Save both before summaries with median, p95, p99 and error-rate measurements.
  5. Record the hardware configuration used for both workloads.

Commands

terminal
cd backend && k6 run --summary-export=loadtest/before-browse.json loadtest/browse.js
terminal
cd backend && k6 run --summary-export=loadtest/before-order.json loadtest/order.js
terminal
cd backend && TEST_API_URL=http://localhost:8080 make test W=6B4

Tests

  • TestW6B4_BrowseDefaultsToThreeHundredVUsForThreeMinutes in backend/internal/loadtest/loadtest_test.go
  • TestW6B4_BrowseAbortsOnErrorsAndAssertsP99 in backend/internal/loadtest/loadtest_test.go
  • TestW6B4_OrderRampsToOneHundredPerSecondOverTwoMinutes in backend/internal/loadtest/loadtest_test.go
  • TestW6B4_ScriptsReportP99 in backend/internal/loadtest/loadtest_test.go
  • TestW6B4_BrowseRunsAgainstTheAPIWithoutFailedChecks in backend/internal/loadtest/loadtest_test.go
  • TestW6B4_OrderRunCreatesADistinctOrderPerIteration in backend/internal/loadtest/loadtest_test.go
  • TestW6B4_BeforeSummariesRecordPercentilesAndErrorRate in backend/internal/loadtest/loadtest_test.go