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)<300andabortOnFail. - 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)insummaryTrendStats. - With the API running, a browse run using
VUS=5andDURATION=6ssends at least 10 HTTP requests and records zero failed checks. - With the API running, an order run using
PEAK=5,RAMP=2sandHOLD=4ssends 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_durationvalues formed,p(95)andp(99), and include thehttp_req_failedmetric.
Constraints
- Keep the scripts at
backend/loadtest/browse.jsandbackend/loadtest/order.js. - The browse script reads
BASE_URL,VUSandDURATIONfrom the environment. DefaultBASE_URLtohttp://localhost:8080,VUSto300andDURATIONto3m. - Use top-level
vusanddurationoptions 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 mentionhttp_req_failed,http_req_duration,p(99)<300andabortOnFail. - The order script reads
BASE_URL,PEAK,RAMPandHOLDfrom the environment. The default peak is 100 orders per second, with the stages lasting two minutes in total. - Use one
ramping-arrival-ratescenario for the order workload. - Each order iteration sends its own
X-User-IDand 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.jsonand the order summary asbackend/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:
brew install k6
cd backend
make up
make migrate
go run ./cmd/seedIn separate terminals, start the API and worker:
cd backend
make runcd backend
make workerRun both default workloads and save their summaries:
cd backend
k6 run --summary-export=loadtest/before-browse.json loadtest/browse.js
k6 run --summary-export=loadtest/before-order.json loadtest/order.jsRun the exercise tests against the running API:
cd backend
TEST_API_URL=http://localhost:8080 make test W=6B4Inspect 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.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Add
browse.jswith 300 VUs for three minutes by default, listing restaurants and fetching a menu. - Add
order.jswith an arrival-rate ramp to 100 orders per second over two minutes by default. - Use distinct VU identities and unique idempotency keys for order iterations.
- Save both before summaries with median, p95, p99 and error-rate measurements.
- Record the hardware configuration used for both workloads.
Commands
cd backend && k6 run --summary-export=loadtest/before-browse.json loadtest/browse.jscd backend && k6 run --summary-export=loadtest/before-order.json loadtest/order.jscd backend && TEST_API_URL=http://localhost:8080 make test W=6B4Tests
TestW6B4_BrowseDefaultsToThreeHundredVUsForThreeMinutesinbackend/internal/loadtest/loadtest_test.goTestW6B4_BrowseAbortsOnErrorsAndAssertsP99inbackend/internal/loadtest/loadtest_test.goTestW6B4_OrderRampsToOneHundredPerSecondOverTwoMinutesinbackend/internal/loadtest/loadtest_test.goTestW6B4_ScriptsReportP99inbackend/internal/loadtest/loadtest_test.goTestW6B4_BrowseRunsAgainstTheAPIWithoutFailedChecksinbackend/internal/loadtest/loadtest_test.goTestW6B4_OrderRunCreatesADistinctOrderPerIterationinbackend/internal/loadtest/loadtest_test.goTestW6B4_BeforeSummariesRecordPercentilesAndErrorRateinbackend/internal/loadtest/loadtest_test.go