Per-VU auth tokens + refresh

Auth, sessions & uploads

Each VU mints a token the first time it needs one, attaches it to every request, and transparently re-mints it just before expiry — with a custom token_refreshes counter tracking how often. Swap the synthetic mint for a call to your auth server.

Browse all 52 demos 12 categories
Run it
$ loadr run examples/36-auth-tokens.yaml
examples/36-auth-tokens.yaml
# Per-VU auth tokens with automatic refresh — Artillery's `generating-vu-tokens`
# + `refresh-auth-token` in one place.
#
# Each VU mints its own token the first time it needs one (scripts/auth.js,
# beforeRequest hook), attaches it to every request, and silently re-mints it
# just before it expires. A custom `token_refreshes` counter tracks how often
# that happens. Swap the synthetic mint() for a real call to your auth server.
#
#   loadr run examples/36-auth-tokens.yaml
name: auth-tokens
description: Per-VU token minting + transparent refresh before expiry

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

js:
  file: scripts/auth.js
  timeout: 5s

metrics:
  token_refreshes: { kind: counter }

scenarios:
  authed_traffic:
    executor: constant-vus
    vus: 10
    duration: 1m
    flow:
      - request:
          name: profile
          url: /me
          checks: [ { type: status, equals: 200 } ]
      - think_time: { type: uniform, min: 200ms, max: 800ms }
      - request:
          name: orders
          url: /orders
          checks: [ { type: status, equals: 200 } ]

thresholds:
  checks: [ "rate>0.99" ]

View raw: examples/36-auth-tokens.yaml

Related files: scripts/auth.js

What it shows

  • beforeRequest JS hook mints/refreshes
  • Per-VU token state
  • Custom counter metric for refreshes

Key metrics: token_refreshes.