diff --git a/packages/typescript/src/api/syncChannel.ts b/packages/typescript/src/api/syncChannel.ts index 9195d9615c452..92c43c8f5c278 100644 --- a/packages/typescript/src/api/syncChannel.ts +++ b/packages/typescript/src/api/syncChannel.ts @@ -327,6 +327,10 @@ export class SyncRpcChannel { ); } if (this.collectTiming) { + // A callback may have issued a nested request while waiting for this response. + this.lastBytesSent = typeof payload === "string" + ? Buffer.byteLength(payload, "utf-8") + : payload.length; this.lastBytesReceived = this._msgPayload.length; } return this._msgPayload; diff --git a/packages/typescript/test/sync/timing.test.ts b/packages/typescript/test/sync/timing.test.ts new file mode 100644 index 0000000000000..7a6d0b93b59fa --- /dev/null +++ b/packages/typescript/test/sync/timing.test.ts @@ -0,0 +1,40 @@ +import { API } from "@typescript/typescript/sync"; +import assert from "node:assert"; +import { + describe, + test, +} from "node:test"; +import { fileURLToPath } from "node:url"; +import { createVirtualFileSystem } from "../testUtils.ts"; + +describe("sync IPC timing", () => { + test("records the outer request size after a nested request", () => { + const fs = createVirtualFileSystem({ "/tsconfig.json": "{}", "/a.ts": "" }); + const nestedArgument = "x".repeat(10_000); + let nested = false; + using api = new API({ + cwd: fileURLToPath(new URL("../../../../", import.meta.url).toString()), + collectTiming: true, + fs: { + ...fs, + readFile: path => { + if (!nested) { + nested = true; + api.parseCommandLine([nestedArgument]); + } + return fs.readFile!(path); + }, + }, + }); + api.createSnapshot({ openProjects: ["/tsconfig.json"] }); + + assert.ok(nested); + const { recentRequests } = api.getTimingInfo(); + const nestedIndex = recentRequests.findIndex(request => request.method === "parseCommandLine"); + assert.ok(nestedIndex >= 0); + assert.ok(recentRequests[nestedIndex].bytesSent > nestedArgument.length); + const outer = recentRequests[nestedIndex + 1]; + assert.equal(outer.method, "createSnapshot"); + assert.ok(outer.bytesSent < nestedArgument.length, `${outer.method} recorded ${outer.bytesSent} bytes sent`); + }); +});