~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
127 lines · 5.4 KB Raw History
// The benchmark's client (bench.mjs): requests sent from worker threads, so
// that what a server answers is not held to what one Node thread can send
// and read. Each thread has its own connections, kept alive.
//
//   const pool = await openPool(4)
//   const { times, statuses, bodies, total, firstError } = await pool.run(requests, at, { keep })
//   await pool.close()
//
// A request is { method, url, headers, body } (body a string or bytes).
// `at` requests are in flight at once, spread over the threads; a thread
// sends its next as an answer comes, as many people each waiting on their
// own would. Each time is from sending a request to having its answer whole,
// in ms; a request that could not be sent has status 0. With `keep`, each
// answer's body comes back as text; without, only those of answers that are
// no 2xx.
//
// A server that closes each connection after its answer has the client open
// a connection a request, and each one closed holds its local port for a
// while (TIME_WAIT): at a few hundred a second the client's ports run out.
// Then the request waits for one and is sent again, timed from when it is
// sent; each such wait is counted (`portWaits`), and the table marks it.

import { Worker, isMainThread, parentPort } from 'node:worker_threads'

const ok2xx = (s) => s >= 200 && s < 300
// A request not answered in this long is given up, as status 0: a server
// that stalls stops the run with that, rather than hanging it.
export const TIMEOUT_MS = 120000
const PORT_WAIT_MS = 50
const PORT_WAITS_MAX = 1200

/// One request: its time, status and body, and how many times it waited
/// for a local port. Anything else that keeps it from being sent throws.
export async function timedFetch({ method, url, headers, body }) {
  for (let waits = 0; ; waits++) {
    const t = performance.now()
    try {
      const r = await fetch(url, { method, headers, body, signal: AbortSignal.timeout(TIMEOUT_MS) })
      const buf = Buffer.from(await r.arrayBuffer())
      return { ms: performance.now() - t, status: r.status, buf, waits }
    } catch (e) {
      if (!/EADDRNOTAVAIL/.test(String(e?.cause?.code ?? e?.cause?.message ?? e)) || waits === PORT_WAITS_MAX) throw e
      await new Promise((ok) => setTimeout(ok, PORT_WAIT_MS))
    }
  }
}

if (!isMainThread && parentPort) {
  parentPort.on('message', async ({ id, requests, at, keep }) => {
    const times = new Float64Array(requests.length)
    const statuses = new Uint16Array(requests.length)
    const bodies = new Array(requests.length)
    let portWaits = 0
    let next = 0
    await Promise.all(Array.from({ length: at }, async () => {
      while (next < requests.length) {
        const i = next++
        const t = performance.now()
        try {
          const r = await timedFetch(requests[i])
          times[i] = r.ms
          statuses[i] = r.status
          portWaits += r.waits
          if (keep || !ok2xx(r.status)) bodies[i] = r.buf.toString('utf8')
        } catch (e) {
          times[i] = performance.now() - t
          bodies[i] = String(e?.cause?.message ?? e?.message ?? e)
        }
      }
    }))
    parentPort.postMessage({ id, times, statuses, bodies, portWaits })
  })
}

/// A pool of `threads` worker threads to send requests from.
export async function openPool(threads) {
  const workers = await Promise.all(Array.from({ length: threads }, () => new Promise((ok, fail) => {
    const w = new Worker(new URL(import.meta.url))
    w.once('online', () => ok(w))
    w.once('error', fail)
  })))
  let ids = 0
  return {
    threads,
    /// `requests`, `at` at once: each one's time, status and body (above),
    /// in the order given; how long they took together; how many times a
    /// request waited for a local port; and the first answer that is no
    /// 2xx, if any, as `firstError` ({ i, status, body }).
    async run(requests, at, { keep = false } = {}) {
      const active = Math.max(1, Math.min(threads, at, requests.length))
      // Request i goes to thread i % active, and thread k sends its share
      // of `at` at once.
      const shares = Array.from({ length: active }, (_, k) => ({ k, idx: [], at: Math.floor(at / active) + (k < at % active ? 1 : 0) }))
      requests.forEach((_, i) => shares[i % active].idx.push(i))
      const times = new Float64Array(requests.length)
      const statuses = new Uint16Array(requests.length)
      const bodies = new Array(requests.length)
      let portWaits = 0
      const t = performance.now()
      await Promise.all(shares.map(({ k, idx, at: a }) => new Promise((ok, fail) => {
        const id = ++ids
        const w = workers[k]
        const on = (m) => {
          if (m.id !== id) return
          w.off('message', on)
          w.off('error', fail)
          idx.forEach((i, j) => {
            times[i] = m.times[j]
            statuses[i] = m.statuses[j]
            if (m.bodies[j] !== undefined) bodies[i] = m.bodies[j]
          })
          portWaits += m.portWaits
          ok()
        }
        w.on('message', on)
        w.once('error', fail)
        w.postMessage({ id, requests: idx.map((i) => requests[i]), at: a, keep })
      })))
      const total = performance.now() - t
      const i = statuses.findIndex((s) => !ok2xx(s))
      return { times, statuses, bodies, total, portWaits, firstError: i === -1 ? undefined : { i, status: statuses[i], body: (bodies[i] ?? '').slice(0, 300) } }
    },
    async close() {
      await Promise.all(workers.map((w) => w.terminate()))
    },
  }
}