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

measure-pr-preview.ts

112 lines4.4 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// Every GraphQL read carries `rateLimit`, 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 GitHubPullRequestCli from "../src/pullRequest/GitHubPullRequestCli.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 GitHubGraphQlBudget from "../src/sourceControl/githubGraphQlBudget.ts";
18import * as SourceControlRateLimit from "../src/sourceControl/SourceControlRateLimit.ts";
19import * as VcsProcess from "../src/vcs/VcsProcess.ts";
20​
21const [repository, ...numbers] = process.argv.slice(2);
22if (!repository || numbers.length === 0 || numbers.some((number) => !/^\d+$/.test(number))) {
23 throw new Error("Usage: node apps/server/scripts/measure-pr-preview.ts owner/repo number...");
25​
26type Read = { graphqlRequests: number; restRequests: number; cost: number };
27const reads: Read[] = [];
28const decodeCost = Schema.decodeUnknownOption(
29 Schema.fromJsonString(
30 Schema.Struct({
31 data: Schema.Struct({ rateLimit: Schema.Struct({ cost: Schema.Number }) }),
32 }),
33 ),
34);
35const measuredApi = Layer.effect(
36 GitHubApi.GitHubApi,
37 Effect.gen(function* () {
38 const api = yield* GitHubApi.make;
39 return GitHubApi.GitHubApi.of({
40 ...api,
41 graphql: (input) =>
42 api.graphql(input).pipe(
43 Effect.tap((body) =>
44 Effect.sync(() =>
45 reads.push({
46 graphqlRequests: 1,
47 restRequests: 0,
48 cost: Option.match(decodeCost(body), {
49 onNone: () => 0,
50 onSome: (decoded) => decoded.data.rateLimit.cost,
51 }),
52 }),
53 ),
54 ),
55 ),
56 rest: (input) =>
57 api
58 .rest(input)
59 .pipe(
60 Effect.tap(() =>
61 Effect.sync(() => reads.push({ graphqlRequests: 0, restRequests: 1, cost: 0 })),
62 ),
63 ),
64 });
65 }),
66).pipe(
67 Layer.provide(GitHubCredentials.layer),
68 Layer.provide(GitHubGraphQlBudget.layer),
69 Layer.provide(SourceControlRateLimit.layer),
70 Layer.provide(FetchHttpClient.layer),
71 Layer.provide(VcsProcess.layer),
72 Layer.provide(NodeServices.layer),
73);
74​
75const services = GitHubPullRequestCli.layer.pipe(Layer.provideMerge(measuredApi));
76​
77const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
78​
79for (const mode of ["detail", "preview"] as const) {
80 // Start each side cold, then retain the caches shared across PRs in one session.
81 const rows = await Effect.runPromise(
82 Effect.scoped(
83 Effect.gen(function* () {
84 const provider = yield* GitHubPullRequestProvider.make;
85 return yield* Effect.forEach(numbers.map(Number), (number) =>
86 Effect.gen(function* () {
87 const input = { cwd: process.cwd(), repository, host: "github.com", number };
88 reads.length = 0;
89 const start = performance.now();
90 const value = yield* mode === "detail"
91 ? provider.getChangeRequest(input)
92 : provider.getChangeRequestPreview!(input);
93 const elapsedMs = Math.round(performance.now() - start);
94 const measured = [...reads];
95 const points = measured.reduce((total, read) => total + read.cost, 0);
96 return {
97 repository,
98 number,
99 mode,
100 state: value.state,
101 elapsedMs,
102 points,
103 graphqlRequests: measured.reduce((total, read) => total + read.graphqlRequests, 0),
104 restRequests: measured.reduce((total, read) => total + read.restRequests, 0),
105 };
106 }),
107 );
108 }),
109 ).pipe(Effect.provide(services)),
110 );
111 for (const row of rows) await Effect.runPromise(Console.log(encodeJson(row)));