Open History to see the last commit
1// Run with: node apps/server/scripts/measure-pr-preview.ts owner/repo 123 456
2// Numbers form a session with shared repository-permission caches. Browser and
3// service caches are excluded. Uses real GitHub reads, without a server or database.
4// CLI-generated GraphQL
5// queries are replayed with rateLimit.cost, outside the timed section, to measure
6// their cost without confusing other applications' traffic with this process's.
7import * as NodeServices from "@effect/platform-node/NodeServices";
8import * as Effect from "effect/Effect";
9import * as Console from "effect/Console";
10import * as Layer from "effect/Layer";
11import * as Schema from "effect/Schema";
12
13import * as GitHubPullRequestCli from "../src/pullRequest/GitHubPullRequestCli.ts";
14import * as GitHubPullRequestProvider from "../src/pullRequest/GitHubPullRequestProvider.ts";
15import * as GitHubCli from "../src/sourceControl/GitHubCli.ts";
16import * as VcsProcess from "../src/vcs/VcsProcess.ts";
17
18const [repository, ...numbers] = process.argv.slice(2);
19if (!repository || numbers.length === 0 || numbers.some((number) => !/^\d+$/.test(number))) {
20 throw new Error("Usage: node apps/server/scripts/measure-pr-preview.ts owner/repo number...");
21}
22
23type Read = { graphqlRequests: number; restRequests: number; cost: number; debug: string };
24const reads: Read[] = [];
25const measuredProcess = Layer.effect(
26 VcsProcess.VcsProcess,
27 Effect.gen(function* () {
28 const vcs = yield* VcsProcess.VcsProcess;
29 return VcsProcess.VcsProcess.of({
30 run: (input) =>
31 vcs.run({ ...input, env: { ...input.env, GH_DEBUG: input.env?.GH_DEBUG ?? "api" } }).pipe(
32 Effect.tap((output) =>
33 Effect.sync(() => {
34 const graphqlRequests = [...output.stderr.matchAll(/^> POST \/graphql /gm)].length;
35 const requests = [...output.stderr.matchAll(/^> (?:GET|POST) /gm)].length;
36 const cost = /"rateLimit"\s*:\s*\{[^}]*"cost"\s*:\s*(\d+)/.exec(output.stdout)?.[1];
37 reads.push({
38 graphqlRequests,
39 restRequests: requests - graphqlRequests,
40 cost: Number(cost ?? 0),
41 debug: output.stderr,
42 });
43 }),
44 ),
45 ),
46 });
47 }),
48).pipe(Layer.provide(VcsProcess.layer), Layer.provide(NodeServices.layer));
49
50const services = GitHubPullRequestCli.layer.pipe(
51 Layer.provide(GitHubCli.layer),
52 Layer.provideMerge(measuredProcess),
53);
54
55const decodeJson = Schema.decodeSync(Schema.fromJsonString(Schema.Unknown));
56const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
57const graphqlCost = Effect.fn("measurePrPreview.graphqlCost")(function* (read: Read) {
58 // The REST quota probe synthesizes rateLimit.cost for budget admission.
59 // It does not spend GraphQL points.
60 if (!read.graphqlRequests) return 0;
61 if (read.cost) return read.cost;
62 const vcs = yield* VcsProcess.VcsProcess;
63 let cost = 0;
64 let found = 0;
65 for (const match of read.debug.matchAll(
66 /GraphQL query:\n([\s\S]*?)\nGraphQL variables: (\{[^\n]*\})/g,
67 )) {
68 const query = match[1]!.replace(/(\bquery\b[^{]*\{)/, "$1 rateLimit { cost }");
69 const output = yield* vcs.run({
70 operation: "measurePrPreview.cost",
71 command: "gh",
72 cwd: process.cwd(),
73 args: [
74 "api",
75 "graphql",
76 "--hostname",
77 "github.com",
78 "--input",
79 "-",
80 "--jq",
81 ".data.rateLimit.cost",
82 ],
83 stdin: encodeJson({ query, variables: decodeJson(match[2]!) }),
84 env: { GH_DEBUG: "" },
85 });
86 cost += Number(output.stdout.trim());
87 found++;
88 }
89 if (found !== read.graphqlRequests)
90 throw new Error("Could not account for every GraphQL request");
91 return cost;
92});
93
94for (const mode of ["detail", "preview"] as const) {
95 // Start each side cold, then retain the caches shared across PRs in one session.
96 const rows = await Effect.runPromise(
97 Effect.scoped(
98 Effect.gen(function* () {
99 const provider = yield* GitHubPullRequestProvider.make;
100 return yield* Effect.forEach(numbers.map(Number), (number) =>
101 Effect.gen(function* () {
102 const input = { cwd: process.cwd(), repository, host: "github.com", number };
103 reads.length = 0;
104 const start = performance.now();
105 const value = yield* mode === "detail"
106 ? provider.getChangeRequest(input)
107 : provider.getChangeRequestPreview!(input);
108 const elapsedMs = Math.round(performance.now() - start);
109 const measured = [...reads];
110 const costs = yield* Effect.forEach(measured, graphqlCost);
111 const points = costs.reduce((total, cost) => total + cost, 0);
112 return {
113 repository,
114 number,
115 mode,
116 state: value.state,
117 elapsedMs,
118 points,
119 graphqlRequests: measured.reduce((total, read) => total + read.graphqlRequests, 0),
120 restRequests: measured.reduce((total, read) => total + read.restRequests, 0),
121 };
122 }),
123 );
124 }),
125 ).pipe(Effect.provide(services)),
126 );
127 for (const row of rows) await Effect.runPromise(Console.log(encodeJson(row)));
128}