# Load-testing license verification with k6


You load-test the paths you already respect as load-bearing: the checkout, the login, the search box. Then there is the one call that decides whether the software is allowed to run at all — the license verification — and it almost never sees a load test. It works fine in development, where exactly one instance activates exactly once. The first time it meets real concurrency is in production, at the worst possible moment: a deploy or a restart brings the whole fleet back at once, every instance with a cold cache, all of them reaching for a license in the same two seconds.

A license check is a dependency call like any other, and the activation path has a capacity. This post puts it under load with [k6](https://k6.io), and the first job is to be precise about *which* part you are actually testing.

## What is local, and what is on the wire

The common mistake is to picture every license check as a round-trip to a server. With [Keyright](https://delta1labs.com/products/keyright) it is not. The steady-state verification is a local operation: the SDK holds a signed license, and `Validate()` checks its signature against the embedded public key and reads its claims. No socket, no DNS, microseconds.

```csharp
// Steady state: no network at all. Validate() checks the signature on the
// cached, signed license with the embedded public key, then reads claims.
LicenseInfo info = _client.Validate(licenseKey);
if (info.Status != LicenseStatus.Valid)
    throw new LicenseException(info.Status);   // fail closed
```

Load-testing *that* call is pointless — it is CPU-bound signature math with no shared resource to contend over. What touches the network, and therefore has a capacity you can exhaust, is two things:

- **Activation.** The first time an instance runs, or the first time after its cache is gone, it calls the issuing service to obtain a signed license for its machine identity. This is the expensive call, and it is the one that happens in a thundering herd after a deploy.
- **Refresh.** On an interval the SDK revalidates, so a revocation or an entitlement change propagates. This is a *conditional* request — it sends the current version tag and usually gets back a cheap `304 Not Modified`.

So the real load test is not "verify a license a million times." It is "what happens to the activation endpoint when 300 instances come back from a deploy at the same instant, and then settle into a steady hum of conditional refreshes."

<figure class="my-8"><svg viewBox="0 0 720 300" role="img" aria-label="Diagram: on cold start a client calls POST /v1/activate over the network to the issuing service and receives a signed license. Steady-state Validate runs locally with no network. A periodic refresh sends an ETag and usually gets 304 Not Modified. k6 applies concentrated load to the activation endpoint and a steadier load to refresh." style="width:100%;height:auto"><defs><marker id="ar" markerWidth="9" markerHeight="9" refX="7" refY="4" orient="auto"><path d="M0,0 L8,4 L0,8 z" fill="#64748b"/></marker></defs><rect x="0" y="0" width="720" height="300" fill="none"/><text x="14" y="22" font-family="system-ui" font-weight="600" font-size="13" fill="#6366f1">Where the network load actually is</text><rect x="14" y="44" width="150" height="104" rx="6" fill="#e0e7ff" stroke="#6366f1"/><text x="26" y="64" font-family="system-ui" font-weight="600" font-size="12" fill="#0f172a">Client instance</text><text x="26" y="84" font-family="system-ui" font-size="10" fill="#334155">cold start / after deploy</text><text x="26" y="108" font-family="ui-monospace,monospace" font-size="10" fill="#065f46">Validate() → local</text><text x="26" y="124" font-family="system-ui" font-size="9" fill="#475569">signature check, no network</text><text x="26" y="140" font-family="system-ui" font-size="9" fill="#475569">microseconds</text><rect x="300" y="36" width="190" height="56" rx="6" fill="#fee2e2" stroke="#ef4444"/><text x="312" y="56" font-family="ui-monospace,monospace" font-size="11" fill="#991b1b">POST /v1/activate</text><text x="312" y="74" font-family="system-ui" font-size="9" fill="#991b1b">network · expensive · thundering herd</text><rect x="300" y="110" width="190" height="52" rx="6" fill="#dcfce7" stroke="#10b981"/><text x="312" y="130" font-family="ui-monospace,monospace" font-size="11" fill="#065f46">GET refresh + ETag</text><text x="312" y="148" font-family="system-ui" font-size="9" fill="#475569">usually 304 · cheap · periodic</text><rect x="560" y="60" width="146" height="92" rx="6" fill="#cffafe" stroke="#0891b2"/><text x="572" y="82" font-family="system-ui" font-weight="600" font-size="12" fill="#0e7490">Issuing service</text><text x="572" y="104" font-family="system-ui" font-size="10" fill="#334155">signs a license</text><text x="572" y="122" font-family="system-ui" font-size="10" fill="#334155">your deployment</text><text x="572" y="140" font-family="system-ui" font-size="9" fill="#475569">has a capacity</text><path d="M164,78 L300,64" stroke="#ef4444" stroke-width="1.6" fill="none" marker-end="url(#ar)"/><path d="M164,112 L300,134" stroke="#10b981" stroke-width="1.6" fill="none" marker-end="url(#ar)"/><path d="M490,64 L560,96" stroke="#64748b" stroke-width="1.4" fill="none" marker-end="url(#ar)"/><path d="M490,136 L560,120" stroke="#64748b" stroke-width="1.4" fill="none" marker-end="url(#ar)"/><rect x="300" y="196" width="190" height="60" rx="6" fill="#ede9fe" stroke="#6366f1"/><text x="312" y="216" font-family="system-ui" font-weight="600" font-size="12" fill="#4338ca">k6</text><text x="312" y="234" font-family="system-ui" font-size="9" fill="#475569">ramping-VUs → activation storm</text><text x="312" y="248" font-family="system-ui" font-size="9" fill="#475569">+ steady refresh scenario</text><path d="M395,196 L395,96" stroke="#6366f1" stroke-width="1.4" stroke-dasharray="5 3" fill="none" marker-end="url(#ar)"/><text x="14" y="284" font-family="system-ui" font-size="10" fill="#64748b">Only activation and refresh cross the network; steady-state Validate is local, so there is nothing to load-test there.</text></svg></figure>

## The HTTP calls under test

Activation is a plain POST. The client sends the product it is activating, the license key, and a stable machine identity; it gets back a signed license document.

```http
POST /v1/activate HTTP/1.1
Host: license.example.com
Content-Type: application/json

{ "productKey": "prod_designer", "licenseKey": "KR-7Q2F-...", "machineId": "a1b2c3d4" }
```

A refresh is a conditional GET. The client sends the version tag it already has; when nothing has changed it gets an empty-bodied `304`, which is the whole point — a healthy fleet generates mostly 304s, not full license documents.

```http
GET /v1/license/lic_8f3a HTTP/1.1
Host: license.example.com
If-None-Match: "v7-2026-11-01"

→ HTTP/1.1 304 Not Modified
```

Those are the two requests k6 will generate.

## Modelling the activation storm

The default k6 executor holds a fixed number of virtual users, but that is not the shape of the risk here. The risk is a *ramp*: zero to a few hundred instances in seconds, as a rollout restarts the fleet. Use `ramping-vus` and make each virtual user a distinct machine coming online.

```javascript
import http from 'k6/http';
import { check, sleep } from 'k6';
import { Trend, Rate } from 'k6/metrics';

const activateLatency = new Trend('activate_latency', true);
const activateFailed = new Rate('activate_failed');

export const options = {
  scenarios: {
    // A deploy brings the fleet back with a cold cache: everyone activates at once.
    activation_storm: {
      executor: 'ramping-vus',
      startVUs: 0,
      stages: [
        { duration: '30s', target: 300 }, // ramp to 300 concurrent clients
        { duration: '1m',  target: 300 }, // hold the herd
        { duration: '30s', target: 0 },   // drain
      ],
    },
  },
  thresholds: {
    // Activation is on the critical path of a cold start. If p95 crosses
    // 800ms, a fleet restart visibly stalls; fail the run if it does.
    activate_latency: ['p(95)<800'],
    activate_failed:  ['rate<0.01'],  // under 1% may fail under the storm
    checks:           ['rate>0.99'],
  },
};

const BASE = __ENV.KEYRIGHT_URL;    // e.g. https://license.staging.example.com

export default function () {
  // Each VU/iteration is a distinct machine identity.
  const machineId = `load-${__VU}-${__ITER}`;
  const res = http.post(`${BASE}/v1/activate`, JSON.stringify({
    productKey: __ENV.PRODUCT_KEY,
    licenseKey: __ENV.LICENSE_KEY,
    machineId,
  }), { headers: { 'Content-Type': 'application/json' } });

  activateLatency.add(res.timings.duration);
  activateFailed.add(res.status !== 200);

  check(res, {
    'status is 200': (r) => r.status === 200,
    'body is a signed license': (r) => {
      try { return (JSON.parse(r.body).signature || '').length > 0; }
      catch { return false; }
    },
  });
  sleep(1);
}
```

Two details carry the weight. The **`check` on the body** matters because an activation endpoint under stress can return `200` with a truncated or error payload; counting only status codes would call a broken response a success. And the **thresholds are the test**: a k6 run with no thresholds just prints numbers, but thresholds make it pass or fail, which is what lets you wire it into CI and catch a regression before it ships.

Run it with the target and credentials in the environment, never baked into the script:

```bash
k6 run \
  -e KEYRIGHT_URL=https://license.staging.example.com \
  -e PRODUCT_KEY=prod_designer \
  -e LICENSE_KEY=KR-7Q2F-TEST \
  activation-storm.js
```

## Exercising the cheap path too

A second scenario proves that steady-state refresh stays cheap. Here the interesting assertion is that most responses are `304` — if your fleet is paying for a full license document on every refresh, something upstream is not honouring the version tag, and that quietly multiplies your steady-state cost.

```javascript
export function refresh() {
  const res = http.get(`${BASE}/v1/license/${__ENV.LICENSE_ID}`, {
    headers: { 'If-None-Match': __ENV.ETAG },
  });
  check(res, {
    'not-modified fast path': (r) => r.status === 304,
  });
}
```

Add it alongside the storm as a second scenario with a `constant-arrival-rate` executor, so it models a fixed request rate independent of how slow the responses get — which is exactly how a fleet of timers behaves.

## Reading the result, and what it does not tell you

A green run means your deployment absorbs the herd within your latency budget and overwhelmingly serves refreshes from the 304 fast path. That is the number you want before the next big rollout.

Be honest about the limits.

- **You are testing your deployment, not the verification math.** The signature check is local and constant-time-ish; nothing you do in k6 stresses it. The capacity you are measuring belongs to the issuing service you stand up.
- **Abuse protection will fight your load generator, correctly.** An activation endpoint worth deploying has rate-limiting and anti-abuse rules. A load test can trip them, and a `429` is not a capacity failure — it is the service doing its job. Test against a staging deployment and a product configured with limits that let the run through, or you are measuring the rate-limiter, not the service.
- **Re-activation should be idempotent, so model fresh machines.** Activating the same `machineId` twice should be cheap and must not consume a second seat. That is why the script derives a distinct identity per iteration — a load test that re-activates one machine measures the idempotent fast path, not the storm.
- **Never aim it at a shared or live service.** Point it only at a deployment you own and can reset, with a dedicated test license. Keyright self-hosted Community Edition is enough to stand up a realistic target for this.

## Takeaway

The license check is a load-bearing dependency, and the half of it that touches the network has a capacity like any other endpoint. k6 lets you find that capacity on your terms — modelling the synchronized cold-start burst that a deploy really causes, asserting on p95 and failure rate rather than eyeballing a graph, and confirming the refresh path stays on its cheap 304 lane. Do it once in staging and the next fleet-wide restart stops being the moment you discover the limit.

