|
| 1 | +--- |
| 2 | +description: >- |
| 3 | + Fedify can expose cooperative benchmark endpoints for measuring federation |
| 4 | + workloads without requiring an external metrics backend. |
| 5 | +--- |
| 6 | + |
| 7 | +Benchmarking |
| 8 | +============ |
| 9 | + |
| 10 | +*This API is available since Fedify 2.3.0.* |
| 11 | + |
| 12 | +Fedify can run as a cooperative benchmark target by enabling |
| 13 | +`~FederationOptions.benchmarkMode`. This mode exposes local benchmark |
| 14 | +endpoints under `/.well-known/fedify/bench/` and configures an in-process |
| 15 | +OpenTelemetry metrics reader so benchmark clients can collect server-side |
| 16 | +measurements without a separate metrics backend. |
| 17 | + |
| 18 | +> [!WARNING] |
| 19 | +> Do not enable `benchmarkMode` in production. It is intended for benchmark |
| 20 | +> targets that you control. |
| 21 | +
|
| 22 | + |
| 23 | +Enabling benchmark mode |
| 24 | +----------------------- |
| 25 | + |
| 26 | +Enable `benchmarkMode` when creating the `Federation` object. If you use the |
| 27 | +benchmark trigger endpoint, configure the sink inboxes on the server: |
| 28 | + |
| 29 | +~~~~ typescript twoslash |
| 30 | +import type { KvStore } from "@fedify/fedify"; |
| 31 | +// ---cut-before--- |
| 32 | +import { createFederation } from "@fedify/fedify"; |
| 33 | + |
| 34 | +const federation = createFederation<void>({ |
| 35 | +// ---cut-start--- |
| 36 | + kv: null as unknown as KvStore, |
| 37 | +// ---cut-end--- |
| 38 | + benchmarkMode: { |
| 39 | + triggerSinks: ["https://sink.example/inbox"], |
| 40 | + }, |
| 41 | +}); |
| 42 | +~~~~ |
| 43 | + |
| 44 | +When enabled, Fedify changes only benchmark-target defaults: |
| 45 | + |
| 46 | + - `~FederationOptions.allowPrivateAddress` defaults to `true`, unless a |
| 47 | + custom document loader factory is configured. |
| 48 | + - `~FederationOptions.signatureTimeWindow` defaults to `false`. |
| 49 | + - Explicit `allowPrivateAddress` and `signatureTimeWindow` values still win. |
| 50 | + - Inbox idempotency is unchanged. Benchmark clients that need repeated |
| 51 | + deliveries should mint unique activity IDs. |
| 52 | + |
| 53 | +If you provide `meterProvider` together with `benchmarkMode`, Fedify throws a |
| 54 | +`TypeError`. OpenTelemetry metric readers have to be attached when a |
| 55 | +`MeterProvider` is constructed, so benchmark mode owns its in-process provider. |
| 56 | + |
| 57 | +If the same application code sometimes runs with benchmark mode and sometimes |
| 58 | +runs with your normal OpenTelemetry pipeline, pass your application |
| 59 | +`meterProvider` only when benchmark mode is off: |
| 60 | + |
| 61 | +~~~~ typescript twoslash |
| 62 | +import type { KvStore } from "@fedify/fedify"; |
| 63 | +import type { MeterProvider } from "@opentelemetry/api"; |
| 64 | +// ---cut-start--- |
| 65 | +declare const process: { env: Record<string, string | undefined> }; |
| 66 | +const kv = null as unknown as KvStore; |
| 67 | +const meterProvider = null as unknown as MeterProvider; |
| 68 | +// ---cut-end--- |
| 69 | +import { createFederation } from "@fedify/fedify"; |
| 70 | + |
| 71 | +const benchmarkEnabled = process.env.FEDIFY_BENCHMARK === "1"; |
| 72 | + |
| 73 | +const federation = createFederation<void>({ |
| 74 | + kv, |
| 75 | + benchmarkMode: benchmarkEnabled |
| 76 | + ? { triggerSinks: ["https://sink.example/inbox"] } |
| 77 | + : false, |
| 78 | + meterProvider: benchmarkEnabled ? undefined : meterProvider, |
| 79 | +}); |
| 80 | +~~~~ |
| 81 | + |
| 82 | + |
| 83 | +Benchmark stats endpoint |
| 84 | +------------------------ |
| 85 | + |
| 86 | +`GET /.well-known/fedify/bench/stats` returns a versioned JSON snapshot of the |
| 87 | +server-side metrics collected by the benchmark mode reader: |
| 88 | + |
| 89 | +~~~~ json |
| 90 | +{ |
| 91 | + "version": 1, |
| 92 | + "source": "server", |
| 93 | + "generatedAt": "2026-06-02T00:00:00.000Z", |
| 94 | + "scopeMetrics": [], |
| 95 | + "errors": [] |
| 96 | +} |
| 97 | +~~~~ |
| 98 | + |
| 99 | +The `scopeMetrics` field contains serialized OpenTelemetry scope metrics. |
| 100 | +Observable queue depth is included when configured queues implement |
| 101 | +`MessageQueue.getDepth()`. |
| 102 | + |
| 103 | + |
| 104 | +Benchmark trigger endpoint |
| 105 | +-------------------------- |
| 106 | + |
| 107 | +`POST /.well-known/fedify/bench/trigger` asks the target application to call |
| 108 | +`Context.sendActivity()` with an explicit sender, recipients, and activity. |
| 109 | +This exercises the target's normal outbox and queue path. |
| 110 | + |
| 111 | +The request body has this shape: |
| 112 | + |
| 113 | +~~~~ json |
| 114 | +{ |
| 115 | + "sender": { "identifier": "alice" }, |
| 116 | + "recipients": [ |
| 117 | + { |
| 118 | + "@context": "https://www.w3.org/ns/activitystreams", |
| 119 | + "type": "Service", |
| 120 | + "id": "https://sink.example/actors/bob", |
| 121 | + "inbox": "https://sink.example/inbox" |
| 122 | + } |
| 123 | + ], |
| 124 | + "activity": { |
| 125 | + "@context": "https://www.w3.org/ns/activitystreams", |
| 126 | + "type": "Create", |
| 127 | + "id": "https://example.com/activities/bench-1", |
| 128 | + "actor": "https://example.com/users/alice", |
| 129 | + "object": { |
| 130 | + "type": "Note", |
| 131 | + "id": "https://example.com/notes/bench-1", |
| 132 | + "content": "benchmark" |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | +~~~~ |
| 137 | + |
| 138 | +The `sender` must be either `{ "identifier": string }` or |
| 139 | +`{ "username": string }`. Recipients are parsed as ActivityPub actors and must |
| 140 | +have `id` and `inbox` properties. The activity is parsed as an ActivityPub |
| 141 | +`Activity`. |
| 142 | + |
| 143 | +By default, every recipient inbox must appear in the server-configured |
| 144 | +`~FederationBenchmarkOptions.triggerSinks` list. This keeps benchmark traffic |
| 145 | +pointed at benchmark sink inboxes and prevents callers from choosing their own |
| 146 | +allowlist. To bypass this guard for a controlled run, set |
| 147 | +`~FederationBenchmarkOptions.allowUnsafeTriggerRecipients` to `true` in the |
| 148 | +application configuration. |
| 149 | + |
| 150 | +A successful trigger returns `202 Accepted`: |
| 151 | + |
| 152 | +~~~~ json |
| 153 | +{ |
| 154 | + "version": 1, |
| 155 | + "activityId": "https://example.com/activities/bench-1", |
| 156 | + "queueCorrelationId": "https://example.com/activities/bench-1", |
| 157 | + "recipientCount": 1, |
| 158 | + "inboxCount": 1 |
| 159 | +} |
| 160 | +~~~~ |
| 161 | + |
| 162 | +The `queueCorrelationId` is the activity ID preserved on the queued fanout or |
| 163 | +outbox work. |
| 164 | + |
| 165 | + |
| 166 | +Metrics |
| 167 | +------- |
| 168 | + |
| 169 | +Benchmark mode uses the same Fedify metrics documented in |
| 170 | +[*OpenTelemetry*](./opentelemetry.md), including queue task metrics, queue |
| 171 | +depth, HTTP server metrics, and signature verification histograms. The |
| 172 | +benchmark endpoints themselves are classified as `fedify.endpoint=benchmark` |
| 173 | +in `fedify.http.server.request.*` metrics. |
0 commit comments