Benchmark report package

You build benchreport, a Go package that parses a k6 JSON summary export into a Markdown performance table with hardware recorded on every row.

Ticket

Build backend/internal/benchreport, a Go package that reads a k6 --summary-export JSON document through an io.Reader and formats typed run results as a GitHub-flavoured Markdown table. The report captures request counts, requests per second, average and p95 latency, and the error rate.

The project needs these results to make local performance measurements useful beyond a single run. Each row must identify the hardware that produced it, so the README can use the numbers when discussing Capacity estimation. The next exercise uses this evidence in the project documentation.

The tests call this exported API:

go
type Summary struct {
    Requests  int
    RPS       float64
    AvgMs     float64
    P95Ms     float64
    ErrorRate float64
}

type Run struct {
    Scenario string
    Hardware string
    Summary  Summary
}

var ErrMissingMetric error
var ErrNoHardware error

func ParseK6(r io.Reader) (Summary, error)
func Table(runs []Run) (string, error)

Acceptance criteria

  • ParseK6 reads the supplied k6 summary through an io.Reader and extracts the required values from the top-level metrics object. For the provided sample, it returns Requests: 18450, RPS: 1230.07, AvgMs: 84.25, P95Ms: 212.44 and ErrorRate: 0.00298.

  • Malformed JSON causes ParseK6 to return an error instead of a zero-valued Summary.

  • If a required metric is absent, ParseK6 returns an error satisfying errors.Is(err, ErrMissingMetric). The error message identifies the missing metric by its exact name: http_reqs, http_req_duration or http_req_failed.

  • Table produces the exact Markdown header and separator, preserves run order, and formats the supplied values as follows: requests per second with zero decimal places, p95 with one decimal place and ms, and error rate as a percentage with two decimal places. For the test runs, the output is:

    | Scenario | Hardware | Requests/s | p95 | Error rate |
    | --- | --- | --- | --- | --- |
    | browse local | M2 MacBook Air, 8 GB | 1230 | 212.4 ms | 0.30% |
    | browse server | 2 vCPU, 4 GB VM | 450 | 380.0 ms | 0.00% |

    Every generated line, including the final data row, ends with a newline.

  • If a run has a hardware description containing only whitespace, Table returns an error satisfying errors.Is(err, ErrNoHardware) and identifies the affected scenario. For the test case, that scenario is bad.

  • An empty run list, including nil, causes Table to return an error rather than an empty Markdown table.

Constraints

  • Read the k6 JSON from the supplied io.Reader. Do not require a filesystem path or a particular filename.
  • Read http_reqs.count and http_reqs.rate, http_req_duration.avg and http_req_duration.p(95), and http_req_failed.value.
  • Return errors for malformed JSON and missing required metrics. Missing-metric errors must satisfy errors.Is(err, ErrMissingMetric) and name the missing metric.
  • Return an error for an empty run list.
  • Check hardware descriptions after trimming whitespace. Missing-hardware errors must satisfy errors.Is(err, ErrNoHardware) and name the affected scenario.
  • Use the exact table header and separator shown in the acceptance criteria. Preserve input order and end every line with a newline.
  • Do not modify the provided tests.

Design direction

Keep parsing and presentation as separate responsibilities. ParseK6 should turn the relevant parts of the JSON export into a typed Summary. Table should work only with Run values and produce the Markdown output.

Consider these trade-offs:

  • k6 exports contain more metrics than this report needs. Decode only the required metrics so unrelated metrics do not become dependencies.
  • Keep latency values in milliseconds and the error rate as a fraction in Summary. Convert the fraction to a percentage when formatting the table.
  • Use the sentinel errors to distinguish missing metrics from missing hardware. Wrap them with context so callers can use errors.Is and still identify the affected metric or scenario.

Any package layout that meets the contract passes.

How to check your work

Start the local Docker services and make sure the API is running at http://localhost:8080 before running the k6 scenario. Then run the commands from the backend directory:

bash
cd backend
make test W=8B4
go test -race ./...
golangci-lint run ./...
govulncheck ./...
k6 run --summary-export=loadtest/final-browse.json loadtest/browse.js

The first command runs the exercise's Go testing checks; the later commands run the full race-enabled suite, the configured Go linter, the vulnerability scan and the local browse scenario. Read the first failing assertion or diagnostic first: it identifies the contract to investigate before you work through subsequent failures.

Use the exported k6 summary to create a report from actual run values. Record the CPU, RAM and whether the benchmark ran in Docker or on bare metal alongside every result. Use all three measurements, including Requests per second (RPS) and p95 latency, when interpreting the benchmark.

Finished Exercise 8.7?

App integration and capacity

3 items

  • 8.5Expo stretch integrationnot completed
  • 8.6Tenfold capacity estimationnot completed
  • 8.7Benchmark report packagenot completed

Project documentation

Up next · Module 4 · 5 items · about 4 h 60 min

Start module 4

Implementation

Run locally

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

  1. make test W=8B4 passes the benchmark report tests.
  2. ParseK6 extracts the required values and rejects invalid JSON or missing metrics.
  3. Table formats ordered rows and rejects missing hardware or an empty run list.
  4. Race-enabled backend tests, linting and govulncheck complete successfully.
  5. The local k6 browse run produces an export that can be processed by benchreport.

Commands

terminal
cd backend && make test W=8B4
terminal
cd backend && go test -race ./... && golangci-lint run ./... && govulncheck ./...
terminal
cd backend && k6 run --summary-export=loadtest/final-browse.json loadtest/browse.js

Tests

  • TestW8B4_ParseK6ReadsRequestsRateLatencyAndErrors in backend/internal/benchreport/benchreport_test.go
  • TestW8B4_ParseK6RefusesInvalidJSON in backend/internal/benchreport/benchreport_test.go
  • TestW8B4_ParseK6NamesTheMissingMetric in backend/internal/benchreport/benchreport_test.go
  • TestW8B4_TablePutsTheHardwareOnEveryRow in backend/internal/benchreport/benchreport_test.go
  • TestW8B4_TableRefusesARowWithoutHardware in backend/internal/benchreport/benchreport_test.go
  • TestW8B4_TableRefusesAnEmptyRunList in backend/internal/benchreport/benchreport_test.go