Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

11.7 KB
Raw Blame
import Fastify from 'fastify'
import { afterEach, expect, it } from 'vitest'
import { readFileSync } from 'node:fs'
import { BUS_CHANNEL_KINDS, busKind, frameKind, markCpuKind, selfProfileFromEnv, startSelfProfile, topCalls, topSelfTime } from './selfProfile.js'
import { enterWork, workKind } from './queryStats.js'

let stop = () => {}
afterEach(() => stop())

it('ranks files and functions by self time, with dependencies by package and no absolute paths', () => {
  const at = (id: number, functionName: string, url: string, lineNumber = 0) => ({ id, callFrame: { functionName, url, lineNumber } })
  const top = topSelfTime({
    nodes: [at(1, '(root)', ''), at(2, 'readChanges', `file://${process.cwd()}/dist/read/changes.js`, 9), at(3, 'deserialize', 'file:///app/node_modules/bson/lib/bson.cjs', 41)],
    samples: [2, 3, 3, 3, 2], timeDeltas: [0, 1000, 2000, 3000, 4000],
  })
  expect(top).toEqual({ sampledMs: 10, files: { 'node_modules/bson/lib/bson.cjs': 9, 'dist/read/changes.js': 1 },
    functions: { 'deserialize node_modules/bson/lib/bson.cjs:42': 9, 'readChanges dist/read/changes.js:10': 1 },
    callers: {}, inclusive: { 'readChanges dist/read/changes.js:10': 1 } })
})

it('names the code of ours that a dependency or built-in spends its time for, and the time under each function of ours', () => {
  const app = (path: string) => `file://${process.cwd()}/${path}`
  const at = (id: number, functionName: string, url: string, children: number[] = [], lineNumber = 0) => ({ id, callFrame: { functionName, url, lineNumber }, children })
  const top = topSelfTime({
    nodes: [
      at(1, '(root)', '', [2, 6]),
      at(2, 'changes', app('dist/read/service.js'), [3, 4], 113),
      at(3, 'structuredClone', 'node:internal/worker/js_transferable', [], 111),
      at(4, 'visibleState', app('dist/superTabs/model.js'), [5, 7], 637),
      at(5, 'structuredClone', 'node:internal/worker/js_transferable', [], 111),
      at(6, '(garbage collector)', ''),
      // Recursion counts once per sample under a function of ours.
      at(7, 'visibleState', app('dist/superTabs/model.js'), [8], 637),
      at(8, 'parseEngineResponse', 'file:///app/node_modules/@prisma/client/runtime/library.js', [], 111),
    ],
    samples: [3, 5, 5, 4, 6, 8], timeDeltas: [0, 1000, 2000, 2000, 1000, 1000, 3000],
  })
  expect(top.callers).toEqual({
    'structuredClone node:internal/worker/js_transferable:112 ← visibleState dist/superTabs/model.js:638': 4,
    'parseEngineResponse node_modules/@prisma/client/runtime/library.js:112 ← visibleState dist/superTabs/model.js:638': 3,
    'structuredClone node:internal/worker/js_transferable:112 ← changes dist/read/service.js:114': 1,
  })
  expect(top.inclusive).toEqual({ 'changes dist/read/service.js:114': 9, 'visibleState dist/superTabs/model.js:638': 8 })
})

it('counts the busy time of a named piece of work, and of what it awaits, under its name', async () => {
  const logs: Record<string, unknown>[] = []
  stop = startSelfProfile({ everyMs: 3_600_000, reportMs: 200, log: (_m, c) => logs.push(c) })
  await new Promise<void>(resolve => setImmediate(() => {
    markCpuKind('http GET /api/x')
    void Promise.resolve().then(() => { const end = performance.now() + 40; while (performance.now() < end) { /* spin */ } }).then(() => resolve())
  }))
  await new Promise(resolve => setTimeout(resolve, 250))
  const busy = logs[0]!.busyMs as Record<string, number>
  expect(busy['http GET /api/x']).toBeGreaterThanOrEqual(35)
})

const spin = (ms: number) => { const end = performance.now() + ms; while (performance.now() < end) { /* spin */ } }
const reported = async (logs: unknown[]) => { while (!logs.length) await new Promise(resolve => setTimeout(resolve, 20)) }

it('logs a WebSocket frame by its type only when the type is known, anything a client invents as other', () => {
  const known = new Set(['terminal_input'])
  expect(frameKind('web-ws', 'terminal_input', known)).toBe('web-ws terminal_input')
  expect(frameKind('web-ws', 'hello from [email protected]', known)).toBe('web-ws other')
  expect(frameKind('manager-ws', { t: 'up' }, known)).toBe('manager-ws other')
  expect(frameKind('manager-ws', undefined, known)).toBe('manager-ws other')
})

it('keeps at most 50 names in a window; work under any further name counts as other', async () => {
  const logs: Record<string, number>[] = []
  // One window long enough for all 120 names, so the cap is the only limit.
  stop = startSelfProfile({ everyMs: 3_600_000, reportMs: 2_000, log: (_m, c) => logs.push(c.busyMs as Record<string, number>) })
  for (let i = 0; i < 120; i++) {
    await new Promise<void>(resolve => setImmediate(() => { markCpuKind(`job name${i}`); spin(1); resolve() }))
  }
  await reported(logs)
  const busy = logs[0]!
  expect(busy['job name0']).toBeGreaterThan(0)
  expect(busy.other).toBeGreaterThanOrEqual(60)
  expect(Object.keys(busy).filter(k => k.startsWith('job name')).length).toBeLessThanOrEqual(50)
})

it('names HTTP requests when CPU_SELF_PROFILE=1, and adds no request hook when it is off', async () => {
  // Each server has its own route: work the first one started can still run in the second one's window.
  const served = async (env: NodeJS.ProcessEnv, route: string) => {
    const app = Fastify()
    app.get(route, async () => { spin(5); return {} })
    const logs: Record<string, number>[] = []
    const options = { everyMs: 3_600_000, reportMs: 200, log: (_m: string, c: Record<string, unknown>) => logs.push(c.busyMs as Record<string, number>) }
    const fromEnv = selfProfileFromEnv(app, env, options)
    // Off, the profile is started by hand to see whether the request would still be named.
    stop = fromEnv ?? startSelfProfile(options)
    await app.inject({ method: 'GET', url: route })
    await new Promise(resolve => setTimeout(resolve, 250))
    stop(); stop = () => {}
    await app.close()
    return { started: fromEnv !== undefined, kinds: Object.keys(Object.assign({}, ...logs)) }
  }
  const on = await served({ CPU_SELF_PROFILE: '1' }, '/api/on')
  expect(on.started).toBe(true)
  expect(on.kinds).toContain('http GET /api/on')
  const off = await served({}, '/api/off')
  expect(off.started).toBe(false)
  expect(off.kinds).not.toContain('http GET /api/off')
})

it('names the work for the query stats too, with the profile off', async () => {
  const seen = await new Promise<string>(resolve => setImmediate(() => {
    markCpuKind('web-ws swarm_changes')
    void Promise.resolve().then(() => resolve(workKind()))
  }))
  expect(seen).toBe('web-ws swarm_changes')
  enterWork(undefined)
})

it('splits the profile by the route that ran each sample, from the work kinds marked while it ran', () => {
  const app = (path: string) => `file://${process.cwd()}/${path}`
  const at = (id: number, functionName: string, url: string, children: number[] = [], lineNumber = 0) => ({ id, callFrame: { functionName, url, lineNumber }, children })
  const top = topSelfTime({
    startTime: 0,
    nodes: [at(1, '(root)', '', [2, 4]), at(2, 'changes', app('dist/read/service.js'), [3], 9), at(3, 'structuredClone', 'node:internal/worker/js_transferable', [], 111),
      at(4, 'observeMachine', app('dist/superTabs/store.js'), [5], 99), at(5, 'structuredClone', 'node:internal/worker/js_transferable', [], 111)],
    // Samples at 1, 2, 3, 4 and 5 ms, each 1 ms long.
    samples: [3, 3, 5, 5, 5], timeDeltas: [1000, 1000, 1000, 1000, 1000, 1000],
  }, { start: 0, marks: [[0, 'http GET /changes'], [2.5, 'http POST /machines']] })
  expect(top.byRoute).toEqual({
    'http POST /machines': { sampledMs: 3, inclusive: { 'observeMachine dist/superTabs/store.js:100': 3 },
      callers: { 'structuredClone node:internal/worker/js_transferable:112 ← observeMachine dist/superTabs/store.js:100': 3 } },
    'http GET /changes': { sampledMs: 2, inclusive: { 'changes dist/read/service.js:10': 2 },
      callers: { 'structuredClone node:internal/worker/js_transferable:112 ← changes dist/read/service.js:10': 2 } },
  })
})

it('counts calls of our functions from precise coverage, an anonymous one by its place, and nothing outside our code', () => {
  const app = (path: string) => `file://${process.cwd()}/${path}`
  const calls = topCalls([
    { url: app('dist/superTabs/model.js'), functions: [{ functionName: 'stableId', ranges: [{ startOffset: 10, count: 900 }] },
      { functionName: '', ranges: [{ startOffset: 4021, count: 50 }] }, { functionName: 'idle', ranges: [{ startOffset: 90, count: 0 }] }] },
    { url: 'file:///app/node_modules/@prisma/client/runtime/library.js', functions: [{ functionName: 'parse', ranges: [{ startOffset: 1, count: 99999 }] }] },
  ])
  expect(calls).toEqual({ 'stableId dist/superTabs/model.js': 900, '(anonymous) dist/superTabs/model.js@4021': 50 })
})

it('names a Redis message by its subscriber and channel kind, never the id in the channel', () => {
  expect(busKind('terminal', 'termup:6a8fe4fcf1a6b5972eed0103')).toBe('bus terminal termup')
  expect(busKind('main', 'up:machine-123')).toBe('bus main up')
  expect(busKind('app', 'appup:stream_9')).toBe('bus app appup')
  expect(busKind('main', 'swarm-stamp')).toBe('bus main swarm-stamp')
  // Anything that does not look like one of the bus's own names counts as other.
  expect(busKind('main', 'Weird Channel:1')).toBe('bus main other')
  expect(busKind('main', `${'x'.repeat(40)}:1`)).toBe('bus main other')
})

it('names only the bus\'s own channel kinds; an id sent as a whole channel name, or an unknown kind, counts as other (review of #1880)', () => {
  // These names are Prometheus labels too (work_busy_seconds_total, db_queries_total), so a bare id must never pass.
  expect(busKind('main', '6a730145e13f6277cdf98c1a')).toBe('bus main other')
  expect(busKind('main', 'person-name')).toBe('bus main other')
  expect(busKind('main', 'newkind:6a730145e13f6277cdf98c1a')).toBe('bus main other')
  expect(busKind('main', 'swarm-deliveries:m1')).toBe('bus main swarm-deliveries')
})

it('knows every channel kind the bus subscribes to, so none counts as other by mistake', () => {
  const source = readFileSync(new URL('./bus.ts', import.meta.url), 'utf8')
  const prefixes = [...source.matchAll(/`([a-z0-9-]+):\$\{/g)].map(m => m[1]!)
  // Calls only: `addSub(X, …)`, `addBinarySub(X, …)`, `addSubOn(someSubscriber, X, …)`; a definition's typed parameters (`channel: string`) are not calls.
  const subscribed = [...source.matchAll(/\b(?:addSub|addBinarySub)\(\s*([^,]+),|\baddSubOn\(\s*\w+Subscriber,\s*([^,]+),/g)]
    .map(m => (m[1] ?? m[2])!.trim()).filter(channel => !/:\s/.test(channel))
  // Each subscribed channel is a `kind:${id}` literal, a builder made from one, or SWARM_STAMP.
  expect(subscribed.length).toBeGreaterThan(20)
  const builders = new Map([...source.matchAll(/const (\w+) = \([^)]*\): string => `([a-z0-9-]+):/g)].map(m => [m[1]!, m[2]!]))
  const kinds = new Set<string>()
  for (const channel of subscribed) {
    if (channel === 'channel') continue // addSub's own body passes its parameter on
    if (channel === 'SWARM_STAMP') { kinds.add('swarm-stamp'); continue }
    const literal = channel.match(/^`([a-z0-9-]+):/)
    const builder = builders.get(channel.replace(/\(.*$/, ''))
    const kind = literal?.[1] ?? builder
    expect(kind, channel).toBeDefined()
    kinds.add(kind!)
  }
  for (const kind of kinds) expect(BUS_CHANNEL_KINDS.has(kind), kind).toBe(true)
  expect(prefixes.length).toBeGreaterThan(0)
})

it('hands every kind\'s busy ms of the minute to onWindow, for the counters and the last hour (#2957)', async () => {
  const windows: Array<Map<string, number>> = []
  stop = startSelfProfile({ everyMs: 3_600_000, reportMs: 200, log: () => {}, onWindow: busy => { windows.push(new Map(busy)) } })
  await new Promise<void>(resolve => setImmediate(() => { markCpuKind('http GET /api/y'); spin(30); resolve() }))
  await reported(windows)
  expect(windows[0]!.get('http GET /api/y')).toBeGreaterThanOrEqual(25)
})