cluster

Infrastructure files for Nordgedanken and Midnightthoughts.
git clone git://archive.git.mtrnord.blog/MTRNord/cluster.git
Log | Files | Refs | README

federation.js (2323B)


      1 /**
      2  * Federation endpoint load test.
      3  *
      4  * Tests both /api/federation/report (full check) and /api/federation/federation-ok
      5  * (lightweight status) under concurrent load. Uses a randomised pool of server names
      6  * to avoid hitting the connection cache for a single target on every request.
      7  *
      8  * Run:
      9  *   k6 run k6/federation.js
     10  *   BASE_URL=http://prod:8080 SERVER_NAMES=mtrnord.blog k6 run k6/federation.js
     11  *
     12  * Tune VU counts and duration via env vars:
     13  *   REPORT_VUS=3 OK_VUS=10 DURATION=120s k6 run k6/federation.js
     14  */
     15 
     16 import http from "k6/http";
     17 import { check, sleep } from "k6";
     18 import {
     19   BASE_URL,
     20   SERVER_NAMES,
     21   DEFAULT_THRESHOLDS,
     22   randomItem,
     23 } from "./config.js";
     24 
     25 const REPORT_VUS = parseInt(__ENV.REPORT_VUS || "3", 10);
     26 const OK_VUS = parseInt(__ENV.OK_VUS || "5", 10);
     27 const DURATION = __ENV.DURATION || "60s";
     28 
     29 export const options = {
     30   scenarios: {
     31     // Full federation report — expensive (does DNS + TLS + HTTP to external servers)
     32     federation_report: {
     33       executor: "constant-vus",
     34       vus: REPORT_VUS,
     35       duration: DURATION,
     36       exec: "fullReport",
     37     },
     38     // Lightweight status check — much cheaper
     39     federation_ok: {
     40       executor: "constant-vus",
     41       vus: OK_VUS,
     42       duration: DURATION,
     43       exec: "federationOk",
     44     },
     45   },
     46   thresholds: DEFAULT_THRESHOLDS,
     47 };
     48 
     49 export function fullReport() {
     50   const server = randomItem(SERVER_NAMES);
     51   const res = http.get(
     52     `${BASE_URL}/api/federation/report?server_name=${encodeURIComponent(server)}&stats_opt_in=false`,
     53     { timeout: "30s" },
     54   );
     55   check(res, {
     56     "report: status 200": (r) => r.status === 200,
     57     "report: has FederationOK": (r) => {
     58       try {
     59         return typeof JSON.parse(r.body).FederationOK === "boolean";
     60       } catch {
     61         return false;
     62       }
     63     },
     64   });
     65   // Small pause — federation checks are slow enough without hammering continuously
     66   sleep(1);
     67 }
     68 
     69 export function federationOk() {
     70   const server = randomItem(SERVER_NAMES);
     71   const res = http.get(
     72     `${BASE_URL}/api/federation/federation-ok?server_name=${encodeURIComponent(server)}`,
     73     { timeout: "15s" },
     74   );
     75   check(res, {
     76     "federation-ok: status 200": (r) => r.status === 200,
     77     "federation-ok: GOOD or BAD": (r) =>
     78       r.body.trim() === "GOOD" || r.body.trim() === "BAD",
     79   });
     80   sleep(0.5);
     81 }