Lifecycle hooks

Scripting & metrics

Run code once before the load (seed data, fetch a shared token), once after (clean up, assert totals), and per-VU on init — the test lifecycle, scripted.

Browse all 52 demos 12 categories
Run it
$ loadr run examples/22-lifecycle.yaml
examples/22-lifecycle.yaml
# Scenario lifecycle hooks + a custom summary report.
#
# Each VU logs in once via the scenario `on_start` hook, runs its `exec`
# function every iteration, then logs out once via `on_stop` when it retires.
# After the run, the exported handleSummary(data) function prints a custom
# end-of-run report instead of loadr's default console summary.
name: lifecycle-hooks
description: per-VU on_start/on_stop and a custom handleSummary report

defaults:
  http:
    base_url: https://api.example.com

js:
  file: scripts/lifecycle.js

scenarios:
  users:
    executor: constant-vus
    vus: 5
    duration: 30s
    on_start: login     # once per VU, before its first iteration
    exec: browse        # once per iteration
    on_stop: logout     # once per VU, when it retires

thresholds:
  http_req_duration: [ "p(95)<500" ]

View raw: examples/22-lifecycle.yaml

Related files: scripts/lifecycle.js

What it shows

  • setup / teardown once per run
  • Per-VU init
  • Share state into the flow