API testing
Write async HTTP workloads, checks and lifecycle hooks.
On this page
Choose the right working directory. Examples using
npm runshortcuts,node bin/loadstride.js, or top-levelstarter-kit/paths run from the LoadStride source checkout. Those shortcuts do not exist in your application’s package.json. Installed-package users runnpx --no-install loadstridefrom their application directory. See Getting started for both options.
Write a test
export const options = {
vus: 10,
duration: '30s',
timeout: '5s',
gracefulStop: '5s',
thresholds: {
http_req_duration: ['p(95)<200', 'p(99)<500'],
http_req_failed: ['rate<0.01'],
checks: ['rate==1'],
iteration_errors: ['count==0'],
},
};
export async function setup({ http, signal }) {
// Obtain shared test data before the measured load phase.
return { baseURL: process.env.TARGET_URL ?? 'http://127.0.0.1:3000' };
}
export default async function ({ http, check, sleep, vu, iteration, data, signal }) {
const response = await http.get(data.baseURL);
check(response, {
'status is 200': r => r.status === 200 && !r.error,
'response is valid': r => r.json().ok === true,
});
await sleep(0.1); // Seconds, like k6.
}
export async function teardown(data, { http, signal }) {
// Optional cleanup after the load phase.
}
Await every request and sleep. Check predicates are synchronous. vu is a
one-based worker ID; iteration is a zero-based, globally unique iteration index.
In arrival mode it is the scheduled slot index, so dropped slots leave gaps.
Setup returns data shared by all workers. Setup and teardown requests are excluded
from the report. Hooks receive http and signal, run once, and hook failures
surface as runtime errors. After cancellation their signal is already aborted.
Virtual users share the Node.js process and module state. They are concurrent
async workers, not isolated JavaScript runtimes. Use the vu ID to manage separate
user data. CPU-intensive or blocking scripts reduce everyone’s scheduling capacity.
HTTP API
await http.get(url, options)await http.post(url, body, options)await http.put(url, body, options)await http.patch(url, body, options)await http.del(url, options)await http.request(method, url, options)
Options include name (endpoint grouping label), headers, body, timeout (milliseconds), signal,
redirect (native fetch policy), and maxBodyBytes (default 1 MiB).
Serialize JSON explicitly and supply its content type:
const response = await http.post(url, JSON.stringify({ name: 'loadstride' }), {
headers: { 'content-type': 'application/json' },
timeout: 2000,
});
Responses expose status, headers, body, bytes, duration (milliseconds),
bodyTruncated, error, and json(). Network failures and timeouts return a
response with an error rather than throwing. A partial response can have a
nonzero status and an error; include !r.error when checking success.
All response bytes are consumed and timed, but only the configured body limit
is retained. json() rejects truncated bodies. maxBodyBytes: 0 disables body
capture. Bytes represent decoded response payload, not network wire bytes.
Native fetch handles redirects and connection pooling; intermediate redirect
responses are not separate request samples. Automatic cookie jars and per-user
connection pools are not yet provided.
Based on the released v0.2.0 guide. View source on GitHub.