|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import type { AssistantMessage, Part } from "@opencode-ai/sdk/v2" |
| 3 | +import { finalTokensPerSecond, liveTokensPerSecond, tokensPerSecond } from "../../src/util/throughput" |
| 4 | + |
| 5 | +function message(input: { created: number; completed?: number; output?: number; reasoning?: number }) { |
| 6 | + return { |
| 7 | + id: "msg_1", |
| 8 | + sessionID: "ses_1", |
| 9 | + role: "assistant", |
| 10 | + parentID: "msg_0", |
| 11 | + modelID: "model", |
| 12 | + providerID: "provider", |
| 13 | + mode: "build", |
| 14 | + agent: "build", |
| 15 | + path: { cwd: "/", root: "/" }, |
| 16 | + cost: 0, |
| 17 | + time: { created: input.created, completed: input.completed }, |
| 18 | + tokens: { input: 500, output: input.output ?? 0, reasoning: input.reasoning ?? 0, cache: { read: 900, write: 0 } }, |
| 19 | + } satisfies AssistantMessage |
| 20 | +} |
| 21 | + |
| 22 | +function text(id: string, chars: number, time?: { start: number; end?: number }): Part { |
| 23 | + return { id, sessionID: "ses_1", messageID: "msg_1", type: "text", text: "x".repeat(chars), time } |
| 24 | +} |
| 25 | + |
| 26 | +function reasoning(id: string, chars: number, time: { start: number; end?: number }): Part { |
| 27 | + return { id, sessionID: "ses_1", messageID: "msg_1", type: "reasoning", text: "x".repeat(chars), time } |
| 28 | +} |
| 29 | + |
| 30 | +describe("tokensPerSecond", () => { |
| 31 | + test("divides tokens by elapsed seconds", () => { |
| 32 | + expect(tokensPerSecond(200, 1_000, 3_000)).toBe(100) |
| 33 | + expect(tokensPerSecond(125, 0, 1_500)).toBe(83) |
| 34 | + }) |
| 35 | + |
| 36 | + test("returns undefined for windows under a second or no tokens", () => { |
| 37 | + expect(tokensPerSecond(200, 1_000, 1_999)).toBeUndefined() |
| 38 | + expect(tokensPerSecond(200, 1_000, 1_000)).toBeUndefined() |
| 39 | + expect(tokensPerSecond(200, 2_000, 1_000)).toBeUndefined() |
| 40 | + expect(tokensPerSecond(0, 0, 5_000)).toBeUndefined() |
| 41 | + }) |
| 42 | +}) |
| 43 | + |
| 44 | +describe("liveTokensPerSecond", () => { |
| 45 | + test("rate tracks a growing stream of timestamped deltas", () => { |
| 46 | + const msg = message({ created: 0 }) |
| 47 | + // First token arrives at 2s (time-to-first-token), then 50 tok/s (200 chars/s) of output. |
| 48 | + const samples = [ |
| 49 | + { now: 2_500, chars: 100 }, |
| 50 | + { now: 3_000, chars: 200 }, |
| 51 | + { now: 4_000, chars: 400 }, |
| 52 | + { now: 6_000, chars: 800 }, |
| 53 | + ] |
| 54 | + const rates = samples.map((sample) => |
| 55 | + liveTokensPerSecond(msg, [text("a", sample.chars, { start: 2_000 })], sample.now), |
| 56 | + ) |
| 57 | + expect(rates).toEqual([undefined, 50, 50, 50]) |
| 58 | + }) |
| 59 | + |
| 60 | + test("sums text and reasoning parts and measures from the earliest part start", () => { |
| 61 | + const parts = [reasoning("r", 400, { start: 1_000, end: 2_000 }), text("t", 400, { start: 2_000 })] |
| 62 | + // 800 chars = 200 tokens over 2s. |
| 63 | + expect(liveTokensPerSecond(message({ created: 0 }), parts, 3_000)).toBe(100) |
| 64 | + }) |
| 65 | + |
| 66 | + test("excludes tool execution after the last generated part finished", () => { |
| 67 | + const parts = [ |
| 68 | + text("t", 800, { start: 1_000, end: 3_000 }), |
| 69 | + { |
| 70 | + id: "tool", |
| 71 | + sessionID: "ses_1", |
| 72 | + messageID: "msg_1", |
| 73 | + type: "tool", |
| 74 | + callID: "c", |
| 75 | + tool: "bash", |
| 76 | + state: { status: "pending", input: {}, raw: "" }, |
| 77 | + } satisfies Part, |
| 78 | + ] |
| 79 | + expect(liveTokensPerSecond(message({ created: 0 }), parts, 30_000)).toBe(100) |
| 80 | + }) |
| 81 | + |
| 82 | + test("falls back to message creation when parts have no timestamps", () => { |
| 83 | + expect(liveTokensPerSecond(message({ created: 1_000 }), [text("t", 400)], 3_000)).toBe(50) |
| 84 | + }) |
| 85 | + |
| 86 | + test("returns undefined before any output", () => { |
| 87 | + expect(liveTokensPerSecond(message({ created: 0 }), [], 5_000)).toBeUndefined() |
| 88 | + }) |
| 89 | +}) |
| 90 | + |
| 91 | +describe("finalTokensPerSecond", () => { |
| 92 | + test("uses reported output and reasoning tokens, ignoring input and cache", () => { |
| 93 | + const msg = message({ created: 0, completed: 5_000, output: 300, reasoning: 100 }) |
| 94 | + expect(finalTokensPerSecond(msg, [text("t", 10, { start: 1_000, end: 5_000 })])).toBe(100) |
| 95 | + }) |
| 96 | + |
| 97 | + test("returns undefined until the message completes", () => { |
| 98 | + expect(finalTokensPerSecond(message({ created: 0, output: 300 }), [])).toBeUndefined() |
| 99 | + }) |
| 100 | +}) |
0 commit comments