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

measure-pr-preview.ts

125 lines4.9 KB

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// Each GraphQL read asks for `rateLimit` here, so its cost is read off its own answer.
5import * as NodeServices from "@effect/platform-node/NodeServices";
6import * as Effect from "effect/Effect";
7import * as Console from "effect/Console";
8import * as Layer from "effect/Layer";
9import * as Option from "effect/Option";
10import * as Schema from "effect/Schema";
11import { FetchHttpClient } from "effect/http";
12​
13import * as GitHubPullRequestApi from "../src/pullRequest/GitHubPullRequestApi.ts";
14import * as GitHubPullRequestProvider from "../src/pullRequest/GitHubPullRequestProvider.ts";
15import * as GitHubApi from "../src/sourceControl/GitHubApi.ts";
16import * as GitHubCredentials from "../src/sourceControl/GitHubCredentials.ts";
17import * as ServerSettings from "../src/serverSettings.ts";
18import * as GitHubQuota from "../src/sourceControl/githubQuota.ts";
19import * as SourceControlRateLimit from "../src/sourceControl/SourceControlRateLimit.ts";
20import * as VcsProcess from "../src/vcs/VcsProcess.ts";
21​
22const [repository, ...numbers] = process.argv.slice(2);
23if (!repository || numbers.length === 0 || numbers.some((number) => !/^\d+$/.test(number))) {
24 throw new Error("Usage: node apps/server/scripts/measure-pr-preview.ts owner/repo number...");
26​
27type Read = { graphqlRequests: number; restRequests: number; cost: number };
28const reads: Read[] = [];
29const decodeCost = Schema.decodeUnknownOption(
30 Schema.fromJsonString(
31 Schema.Struct({
32 data: Schema.Struct({ rateLimit: Schema.Struct({ cost: Schema.Number }) }),
33 }),
34 ),
35);
36/** The query with GitHub's own count of what it cost, for a read rather than a mutation. */
37function withRateLimit(query: string): string {
38 const end = query.lastIndexOf("}");
39 return query.trimStart().startsWith("mutation") || end === -1
40 ? query
41 : `${query.slice(0, end)}\n rateLimit { cost }\n${query.slice(end)}`;
43​
44const measuredApi = Layer.effect(
45 GitHubApi.GitHubApi,
46 Effect.gen(function* () {
47 const api = yield* GitHubApi.make;
48 return GitHubApi.GitHubApi.of({
49 ...api,
50 graphql: (input) =>
51 api.graphql({ ...input, query: withRateLimit(input.query) }).pipe(
52 Effect.tap((body) =>
53 Effect.sync(() =>
54 reads.push({
55 graphqlRequests: 1,
56 restRequests: 0,
57 cost: Option.match(decodeCost(body), {
58 onNone: () => 0,
59 onSome: (decoded) => decoded.data.rateLimit.cost,
60 }),
61 }),
62 ),
63 ),
64 ),
65 rest: (input) =>
66 api
67 .rest(input)
68 .pipe(
69 Effect.tap(() =>
70 Effect.sync(() => reads.push({ graphqlRequests: 0, restRequests: 1, cost: 0 })),
71 ),
72 ),
73 });
74 }),
75).pipe(
76 Layer.provide(GitHubCredentials.layer.pipe(Layer.provide(ServerSettings.layerTest()))),
77 Layer.provide(GitHubQuota.layer),
78 Layer.provide(SourceControlRateLimit.layer),
79 Layer.provide(FetchHttpClient.layer),
80 Layer.provide(VcsProcess.layer),
81 Layer.provide(NodeServices.layer),
82);
83​
84const services = GitHubPullRequestApi.layer.pipe(
85 Layer.provideMerge(measuredApi),
86 Layer.provideMerge(VcsProcess.layer),
87 Layer.provideMerge(NodeServices.layer),
88);
89​
90const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
91​
92for (const mode of ["detail", "preview"] as const) {
93 // Start each side cold, then retain the caches shared across PRs in one session.
94 const rows = await Effect.runPromise(
95 Effect.scoped(
96 Effect.gen(function* () {
97 const provider = yield* GitHubPullRequestProvider.make;
98 return yield* Effect.forEach(numbers.map(Number), (number) =>
99 Effect.gen(function* () {
100 const input = { cwd: process.cwd(), repository, host: "github.com", number };
101 reads.length = 0;
102 const start = performance.now();
103 const value = yield* mode === "detail"
104 ? provider.getChangeRequest(input)
105 : provider.getChangeRequestPreview!(input);
106 const elapsedMs = Math.round(performance.now() - start);
107 const measured = [...reads];
108 const points = measured.reduce((total, read) => total + read.cost, 0);
109 return {
110 repository,
111 number,
112 mode,
113 state: value.state,
114 elapsedMs,
115 points,
116 graphqlRequests: measured.reduce((total, read) => total + read.graphqlRequests, 0),
117 restRequests: measured.reduce((total, read) => total + read.restRequests, 0),
118 };
119 }),
120 );
121 }),
122 ).pipe(Effect.provide(services)),
123 );
124 for (const row of rows) await Effect.runPromise(Console.log(encodeJson(row)));