Test the response time of an API

Add a time budget to a step of a zriz pipeline. The step fails when the API needs more milliseconds than your limit.

Lesson 11 of 12 · zz 0.9.0 or later · Updated · Plain text for agents: /learn/test-api-response-time.md

The problem

An API can return the right data and still be too slow. A test of the status code does not see this. Users see it first.

The test

The example is a shop API. The health call must answer 200 in under 800 ms.

Start from a project that zz init made: the quick start gives the commands. The resource target already has the action check.

.zriz/resources/target.json:

{
  "type": "http",
  "description": "Your app under test",
  "base-url": "${env.TARGET_URL}",
  "headers": { "Content-Type": "application/json" },
  "actions": {
    "check": { "method": "GET", "path": "/api/health" }
  }
}

.zriz/pipelines/health-is-fast.json:

{
  "description": "The health call answers in under 800 ms",
  "steps": [
    {
      "call": "target/check",
      "expect": [
        ["status", "==", 200],
        ["timing.target-ms", "<", 800]
      ]
    }
  ]
}

Run it

zz run health-is-fast
"status":"pass"

What it proves

Note: One call is one sample. Set the limit to three times the usual time, or more.

Next