Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

remotePerformance.bench.ts

163 lines4.9 KB

1import {
2 EnvironmentId,
3 EventId,
4 MessageId,
5 ProjectId,
6 ProviderInstanceId,
7 ThreadId,
8 type OrchestrationEvent,
9 type OrchestrationThread,
10} from "@t3tools/contracts";
11import * as Effect from "effect/Effect";
12import * as Option from "effect/Option";
13import { HttpClient, HttpClientResponse } from "effect/unstable/http";
14import { describe, test } from "vite-plus/test";
15​
16import { issueRemoteWebSocketTicket } from "./authorization/remote.ts";
17import { PrimaryConnectionTarget } from "./connection/model.ts";
18import { fetchRemoteEnvironmentDescriptor } from "./environment/descriptor.ts";
19import type { RemoteEnvironmentRequestError } from "./rpc/http.ts";
20import { fetchEnvironmentThreadSnapshot } from "./state/threadSnapshotHttp.ts";
21import { applyThreadDetailEvent } from "./state/threadReducer.ts";
22​
23const timestamp = "2026-09-01T00:00:00.000Z";
24const runOptions = { warmupTime: 1_000, time: 1_500 };
25const thread: OrchestrationThread = {
26 id: ThreadId.make("thread-1"),
27 projectId: ProjectId.make("project-1"),
28 title: "Remote thread",
29 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
30 runtimeMode: "full-access",
31 interactionMode: "default",
32 branch: null,
33 worktreePath: null,
34 latestTurn: null,
35 createdAt: timestamp,
36 updatedAt: timestamp,
37 archivedAt: null,
38 settledOverride: null,
39 settledAt: null,
40 deletedAt: null,
41 pullRequests: [],
42 messages: Array.from({ length: 100 }, (_, index) => ({
43 id: MessageId.make(`message-${index}`),
44 role: "assistant",
45 text: "Message text. ".repeat(40),
46 turnId: null,
47 streaming: false,
48 createdAt: timestamp,
49 updatedAt: timestamp,
50 })),
51 proposedPlans: [],
52 activities: [],
53 checkpoints: [],
54 session: null,
55};
56const target = new PrimaryConnectionTarget({
57 environmentId: EnvironmentId.make("remote-1"),
58 label: "Remote",
59 httpBaseUrl: "https://remote.example.test",
60 wsBaseUrl: "wss://remote.example.test/ws",
61});
62const responses = {
63 "/.well-known/t3/environment": {
64 environmentId: target.environmentId,
65 label: target.label,
66 platform: { os: "linux", arch: "x64" },
67 serverVersion: "0.0.0-test",
68 capabilities: { repositoryIdentity: true },
69 },
70 "/api/auth/websocket-ticket": { ticket: "test-ticket", expiresAt: timestamp },
71 "/api/orchestration/threads/thread-1": { snapshotSequence: 1, thread },
72};
73const httpClient = HttpClient.make((request) =>
74 Effect.sync(() => {
75 const path = new URL(request.url).pathname as keyof typeof responses;
76 return HttpClientResponse.fromWeb(request, Response.json(responses[path]));
77 }),
78);
79const requests: Record<
80 string,
81 Effect.Effect<unknown, RemoteEnvironmentRequestError, HttpClient.HttpClient>
82> = {
83 "read remote connection descriptor": fetchRemoteEnvironmentDescriptor({
84 httpBaseUrl: target.httpBaseUrl,
85 }),
86 "issue remote WebSocket ticket": issueRemoteWebSocketTicket({
87 httpBaseUrl: target.httpBaseUrl,
88 bearerToken: "test-token",
89 }),
90 "load remote snapshot with 100 messages": fetchEnvironmentThreadSnapshot({
91 prepared: {
92 environmentId: target.environmentId,
93 label: target.label,
94 httpBaseUrl: target.httpBaseUrl,
95 socketUrl: target.wsBaseUrl,
96 httpAuthorization: null,
97 target,
98 },
99 threadId: thread.id,
100 signer: Option.none(),
101 }),
102};
103​
104describe("remote HTTP processing with an in-memory transport", () => {
105 for (const [name, request] of Object.entries(requests)) {
106 test(name, async ({ bench }) => {
107 await bench(name, async () => {
108 await Effect.runPromise(
109 request.pipe(Effect.provideService(HttpClient.HttpClient, httpClient)),
110 );
111 }).run(runOptions);
112 });
113 }
114});
115​
116const delta: OrchestrationEvent = {
117 eventId: EventId.make("delta"),
118 sequence: 2,
119 aggregateKind: "thread",
120 aggregateId: thread.id,
121 occurredAt: timestamp,
122 commandId: null,
123 causationEventId: null,
124 correlationId: null,
125 metadata: {},
126 type: "thread.message-sent",
127 payload: {
128 threadId: thread.id,
129 messageId: MessageId.make("message-99"),
130 role: "assistant",
131 text: " next",
132 turnId: null,
133 streaming: true,
134 createdAt: timestamp,
135 updatedAt: timestamp,
136 },
137};
138​
139describe("remote message replay", () => {
140 for (const count of [100, 1_000]) {
141 const loaded = {
142 ...thread,
143 messages: Array.from({ length: count }, (_, index) => ({
144 ...thread.messages[0]!,
145 id: MessageId.make(`message-${index}`),
146 })),
147 };
148 const event = {
149 ...delta,
150 payload: { ...delta.payload, messageId: loaded.messages.at(-1)!.id },
151 };
152 const name = `apply 200 text deltas to ${count} loaded messages`;
153 test(name, async ({ bench }) => {
154 await bench(name, () => {
155 let current: OrchestrationThread = loaded;
156 for (let index = 0; index < 200; index += 1) {
157 const result = applyThreadDetailEvent(current, event);
158 if (result.kind === "updated") current = result.thread;
159 }
160 }).run(runOptions);
161 });
162 }
163});