11// Run with: node apps/server/scripts/measure-pr-preview.ts owner/repo 123 456
22// Numbers form a session with shared repository-permission caches. Browser and
33// 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.
4+ // Every GraphQL read carries `rateLimit`, so its cost is read off its own answer.
75import * as NodeServices from "@effect/platform-node/NodeServices" ;
86import * as Effect from "effect/Effect" ;
97import * as Console from "effect/Console" ;
108import * as Layer from "effect/Layer" ;
9+ import * as Option from "effect/Option" ;
1110import * as Schema from "effect/Schema" ;
11+ import { FetchHttpClient } from "effect/http" ;
1212
1313import * as GitHubPullRequestCli from "../src/pullRequest/GitHubPullRequestCli.ts" ;
1414import * as GitHubPullRequestProvider from "../src/pullRequest/GitHubPullRequestProvider.ts" ;
15- import * as GitHubCli from "../src/sourceControl/GitHubCli.ts" ;
15+ import * as GitHubApi from "../src/sourceControl/GitHubApi.ts" ;
16+ import * as GitHubCredentials from "../src/sourceControl/GitHubCredentials.ts" ;
17+ import * as GitHubGraphQlBudget from "../src/sourceControl/githubGraphQlBudget.ts" ;
18+ import * as SourceControlRateLimit from "../src/sourceControl/SourceControlRateLimit.ts" ;
1619import * as VcsProcess from "../src/vcs/VcsProcess.ts" ;
1720
1821const [ repository , ...numbers ] = process . argv . slice ( 2 ) ;
1922if ( ! repository || numbers . length === 0 || numbers . some ( ( number ) => ! / ^ \d + $ / . test ( number ) ) ) {
2023 throw new Error ( "Usage: node apps/server/scripts/measure-pr-preview.ts owner/repo number..." ) ;
2124}
2225
23- type Read = { graphqlRequests : number ; restRequests : number ; cost : number ; debug : string } ;
26+ type Read = { graphqlRequests : number ; restRequests : number ; cost : number } ;
2427const reads : Read [ ] = [ ] ;
25- const layerMeasuredProcess = Layer . effect (
26- VcsProcess . VcsProcess ,
28+ const decodeCost = Schema . decodeUnknownOption (
29+ Schema . fromJsonString (
30+ Schema . Struct ( {
31+ data : Schema . Struct ( { rateLimit : Schema . Struct ( { cost : Schema . Number } ) } ) ,
32+ } ) ,
33+ ) ,
34+ ) ;
35+ const measuredApi = Layer . effect (
36+ GitHubApi . GitHubApi ,
2737 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 ( / ^ > P O S T \/ g r a p h q l / gm) ] . length ;
35- const requests = [ ...output . stderr . matchAll ( / ^ > (?: G E T | P O S T ) / gm) ] . length ;
36- const cost = / " r a t e L i m i t " \s * : \s * \{ [ ^ } ] * " c o s t " \s * : \s * ( \d + ) / . exec ( output . stdout ) ?. [ 1 ] ;
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 ( ( ) =>
3745 reads . push ( {
38- graphqlRequests,
39- restRequests : requests - graphqlRequests ,
40- cost : Number ( cost ?? 0 ) ,
41- debug : output . stderr ,
42- } ) ;
43- } ) ,
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+ ) ,
4454 ) ,
4555 ) ,
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+ ) ,
4664 } ) ;
4765 } ) ,
48- ) . pipe ( Layer . provide ( VcsProcess . layer ) , Layer . provide ( NodeServices . layer ) ) ;
49-
50- const layerServices = GitHubPullRequestCli . layer . pipe (
51- Layer . provide ( GitHubCli . layer ) ,
52- Layer . provideMerge ( layerMeasuredProcess ) ,
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 ) ,
5372 Layer . provide ( NodeServices . layer ) ,
5473) ;
5574
56- const decodeJson = Schema . decodeSync ( Schema . fromJsonString ( Schema . Unknown ) ) ;
75+ const services = GitHubPullRequestCli . layer . pipe ( Layer . provideMerge ( measuredApi ) ) ;
76+
5777const encodeJson = Schema . encodeSync ( Schema . fromJsonString ( Schema . Unknown ) ) ;
58- const graphqlCost = Effect . fn ( "measurePrPreview.graphqlCost" ) ( function * ( read : Read ) {
59- // The REST quota probe synthesizes rateLimit.cost for budget admission.
60- // It does not spend GraphQL points.
61- if ( ! read . graphqlRequests ) return 0 ;
62- if ( read . cost ) return read . cost ;
63- const vcs = yield * VcsProcess . VcsProcess ;
64- let cost = 0 ;
65- let found = 0 ;
66- for ( const match of read . debug . matchAll (
67- / G r a p h Q L q u e r y : \n ( [ \s \S ] * ?) \n G r a p h Q L v a r i a b l e s : ( \{ [ ^ \n ] * \} ) / g,
68- ) ) {
69- const query = match [ 1 ] ! . replace ( / ( \b q u e r y \b [ ^ { ] * \{ ) / , "$1 rateLimit { cost }" ) ;
70- const output = yield * vcs . run ( {
71- operation : "measurePrPreview.cost" ,
72- command : "gh" ,
73- cwd : process . cwd ( ) ,
74- args : [
75- "api" ,
76- "graphql" ,
77- "--hostname" ,
78- "github.com" ,
79- "--input" ,
80- "-" ,
81- "--jq" ,
82- ".data.rateLimit.cost" ,
83- ] ,
84- stdin : encodeJson ( { query, variables : decodeJson ( match [ 2 ] ! ) } ) ,
85- env : { GH_DEBUG : "" } ,
86- } ) ;
87- cost += Number ( output . stdout . trim ( ) ) ;
88- found ++ ;
89- }
90- if ( found !== read . graphqlRequests )
91- throw new Error ( "Could not account for every GraphQL request" ) ;
92- return cost ;
93- } ) ;
9478
9579for ( const mode of [ "detail" , "preview" ] as const ) {
9680 // Start each side cold, then retain the caches shared across PRs in one session.
@@ -108,8 +92,7 @@ for (const mode of ["detail", "preview"] as const) {
10892 : provider . getChangeRequestPreview ! ( input ) ;
10993 const elapsedMs = Math . round ( performance . now ( ) - start ) ;
11094 const measured = [ ...reads ] ;
111- const costs = yield * Effect . forEach ( measured , graphqlCost ) ;
112- const points = costs . reduce ( ( total , cost ) => total + cost , 0 ) ;
95+ const points = measured . reduce ( ( total , read ) => total + read . cost , 0 ) ;
11396 return {
11497 repository,
11598 number,
@@ -123,7 +106,7 @@ for (const mode of ["detail", "preview"] as const) {
123106 } ) ,
124107 ) ;
125108 } ) ,
126- ) . pipe ( Effect . provide ( layerServices ) ) ,
109+ ) . pipe ( Effect . provide ( services ) ) ,
127110 ) ;
128111 for ( const row of rows ) await Effect . runPromise ( Console . log ( encodeJson ( row ) ) ) ;
129112}
0 commit comments