1
1
import 'zx/globals' ;
2
2
import os from 'node:os' ;
3
3
import { join } from 'node:path' ;
4
- import chalk from 'chalk ' ;
4
+ import { readFile , writeFile } from 'node:fs/promises ' ;
5
5
6
6
const ROOT = new URL ( '..' , import . meta. url ) . pathname ;
7
7
$ . verbose = true ;
8
+
8
9
const REUSE_CONTROL = ! ! process . env [ 'REUSE_CONTROL' ] ;
9
10
10
11
/*
@@ -56,7 +57,6 @@ const markers = (process.env['MARKERS'] || appMarkers)
56
57
. join ( ',' ) ;
57
58
const fidelity = process . env [ 'FIDELITY' ] || '20' ;
58
59
const throttleRate = process . env [ 'THROTTLE' ] || '2' ;
59
- const FORK_NAME = process . env [ 'FORK_NAME' ] || '' ;
60
60
61
61
const tempDir = os . tmpdir ( ) ;
62
62
@@ -84,81 +84,51 @@ if (!REUSE_CONTROL) {
84
84
await $ `rm -rf ${ EXPERIMENT_DIR } ` ;
85
85
await $ `mkdir ${ EXPERIMENT_DIR } ` ;
86
86
87
- const isMacOs = os . platform ( ) === 'darwin' ;
88
-
87
+ // Intentionally use the same folder for both experiment and control to make it easier to
88
+ // make changes to the benchmark suite itself and compare the results.
89
89
const BENCHMARK_FOLDER = join ( pwd , benchmarkFolder ) ;
90
90
91
91
const CONTROL_PORT = 4020 ;
92
92
const EXPERIMENT_PORT = 4021 ;
93
93
const CONTROL_URL = `http://localhost:${ CONTROL_PORT } ` ;
94
94
const EXPERIMENT_URL = `http://localhost:${ EXPERIMENT_PORT } ` ;
95
95
96
- // we can't do it in parallel on CI,
97
-
98
- if ( ! REUSE_CONTROL ) {
99
- // setup control
100
- await within ( async ( ) => {
101
- await $ `git fetch origin` ;
102
- const mainRef = await $ `git rev-parse origin/main` ;
103
- await cd ( CONTROL_DIR ) ;
104
- await $ `git clone ${ join ( ROOT , '.git' ) } .` ;
105
- await $ `git reset --hard ${ mainRef } ` ;
106
- await $ `rm -rf ./benchmark` ;
107
- await $ `cp -r ${ BENCHMARK_FOLDER } ./benchmark` ;
108
-
109
- console . info ( `$ pnpm install --no-frozen-lockfile ${ chalk . gray ( '[control]' ) } ` ) ;
110
-
111
- await $ `pwd` ;
112
- const result = await $ `pnpm install` ;
113
- console . log ( result ) ;
114
-
115
- console . info ( `$ pnpm build ${ chalk . gray ( '[control]' ) } ` ) ;
116
-
117
- await $ `pnpm build` ;
118
-
119
- if ( isMacOs ) {
120
- await $ `find ./packages -name 'package.json' -exec sed -i '' 's|"main": "index.ts",|"main": "./dist/prod/index.js","module": "./dist/prod/index.js",|g' {} \\;` ;
121
- await $ `find ./packages -name 'package.json' -exec sed -i '' 's|"main": "./dist/index.js",|"main": "./dist/prod/index.js","module": "./dist/prod/index.js",|g' {} \\;` ;
122
- await $ `find ./packages -name 'package.json' -exec sed -i '' 's|"import": "./dist/index.js"|"import": "./dist/prod/index.js"|g' {} \\;` ;
123
- } else {
124
- await $ `find ./packages -name 'package.json' -exec sed -i 's|"main": "index.ts",|"main": "./dist/prod/index.js","module": "./dist/prod/index.js",|g' {} \\;` ;
125
- await $ `find ./packages -name 'package.json' -exec sed -i 's|"main": "./dist/index.js",|"main": "./dist/prod/index.js","module": "./dist/prod/index.js",|g' {} \\;` ;
126
- await $ `find ./packages -name 'package.json' -exec sed -i 's|"import": "./dist/index.js"|"import": "./dist/prod/index.js"|g' {} \\;` ;
127
- }
96
+ // make sure that the origin is up to date so we get the right control
97
+ await $ `git fetch origin` ;
128
98
129
- await cd ( CONTROL_BENCH_DIR ) ;
130
- await $ `pnpm vite build` ;
131
- } ) ;
132
- }
99
+ // now we can get the ref of the control branch so we can check it out later
100
+ const controlRef = ( await $ `git rev-parse origin/main` ) . stdout . trim ( ) ;
133
101
134
- // setup experiment
135
- await within ( async ( ) => {
136
- await cd ( EXPERIMENT_DIR ) ;
137
- await $ `git clone ${ join ( ROOT , '.git' ) } .` ;
138
- await $ `git checkout --force ${ experimentRef } ` ;
139
- await $ `rm -rf ./benchmark` ;
140
- await $ `cp -r ${ BENCHMARK_FOLDER } ./benchmark` ;
102
+ // we can't do it in parallel on CI,
141
103
142
- console . info ( `$ pnpm install --no-frozen-lockfile ${ chalk . gray ( '[experiment]' ) } ` ) ;
143
- const install = ( ) => $ `pnpm install --no-frozen-lockfile` . pipe ( process . stderr ) ;
144
- await spinner ( install ) ;
145
- console . info ( `$ pnpm build ${ chalk . gray ( '[experiment]' ) } ` ) ;
146
- const build = ( ) => $ `pnpm build` . pipe ( process . stderr ) ;
147
- await spinner ( build ) ;
148
-
149
- if ( isMacOs ) {
150
- await $ `find ./packages -name 'package.json' -exec sed -i '' 's|"main": "index.ts",|"main": "./dist/prod/index.js","module": "./dist/prod/index.js",|g' {} \\;` ;
151
- await $ `find ./packages -name 'package.json' -exec sed -i '' 's|"main": "./dist/index.js",|"main": "./dist/prod/index.js","module": "./dist/prod/index.js",|g' {} \\;` ;
152
- await $ `find ./packages -name 'package.json' -exec sed -i '' 's|"import": "./dist/index.js"|"import": "./dist/prod/index.js"|g' {} \\;` ;
153
- } else {
154
- await $ `find ./packages -name 'package.json' -exec sed -i 's|"main": "index.ts",|"main": "./dist/prod/index.js","module": "./dist/prod/index.js",|g' {} \\;` ;
155
- await $ `find ./packages -name 'package.json' -exec sed -i 's|"main": "./dist/index.js",|"main": "./dist/prod/index.js","module": "./dist/prod/index.js",|g' {} \\;` ;
156
- await $ `find ./packages -name 'package.json' -exec sed -i 's|"import": "./dist/index.js"|"import": "./dist/prod/index.js"|g' {} \\;` ;
104
+ /**
105
+ * Rewrite all `package.json`s with a `publishConfig` field with the fields specified in
106
+ * `publishConfig`.
107
+ */
108
+ async function rewritePackageJson ( ) {
109
+ // limit to `@glimmer/*` packages
110
+ const packages = await $ `find ./packages/@glimmer -name 'package.json'` ;
111
+
112
+ for ( const pkg of packages . stdout . trim ( ) . split ( '\n' ) ) {
113
+ const packageJson = JSON . parse ( await readFile ( pkg , { encoding : 'utf8' } ) ) ;
114
+ const publishConfig = packageJson [ 'publishConfig' ] ;
115
+
116
+ // assume that the presence of a `publishConfig` field means that the package is
117
+ // a published package and needs its package.json updated to behave like a published
118
+ // package in the benchmark environment.
119
+ if ( publishConfig ) {
120
+ const updatedPkg = { ...packageJson , ...publishConfig } ;
121
+
122
+ for ( const [ key , value ] of Object . entries ( publishConfig ) ) {
123
+ if ( value === null ) {
124
+ delete updatedPkg [ key ] ;
125
+ }
126
+ }
127
+
128
+ await writeFile ( pkg , JSON . stringify ( updatedPkg , null , 2 ) , { encoding : 'utf8' } ) ;
129
+ }
157
130
}
158
-
159
- await cd ( EXPERIMENT_BENCH_DIR ) ;
160
- await $ `pnpm vite build` ;
161
- } ) ;
131
+ }
162
132
163
133
console . info ( {
164
134
control : controlBranchName ,
@@ -167,6 +137,18 @@ console.info({
167
137
CONTROL_DIR ,
168
138
} ) ;
169
139
140
+ // setup experiment
141
+ await within ( async ( ) => {
142
+ await buildRepo ( EXPERIMENT_DIR , experimentRef ) ;
143
+ } ) ;
144
+
145
+ if ( ! REUSE_CONTROL ) {
146
+ // setup control
147
+ await within ( async ( ) => {
148
+ await buildRepo ( CONTROL_DIR , controlRef ) ;
149
+ } ) ;
150
+ }
151
+
170
152
// start build assets
171
153
$ `cd ${ CONTROL_BENCH_DIR } && pnpm vite preview --port ${ CONTROL_PORT } ` ;
172
154
$ `cd ${ EXPERIMENT_BENCH_DIR } && pnpm vite preview --port ${ EXPERIMENT_PORT } ` ;
@@ -194,3 +176,40 @@ try {
194
176
}
195
177
196
178
process . exit ( 0 ) ;
179
+
180
+ /**
181
+ * @param {string } directory the directory to clone into
182
+ * @param {string } ref the ref to checkout
183
+ */
184
+ async function buildRepo ( directory , ref ) {
185
+ // the benchmark directory is located in `packages/@glimmer/benchmark` in each of the
186
+ // experiment and control checkouts
187
+ const benchDir = join ( directory , 'benchmark' , 'benchmarks' , 'krausest' ) ;
188
+
189
+ await cd ( directory ) ;
190
+
191
+ // write the `pwd` to the output to make it easier to debug if something goes wrong
192
+ await $ `pwd` ;
193
+
194
+ // clone the raw git repo for the experiment
195
+ await $ `git clone ${ join ( ROOT , '.git' ) } .` ;
196
+
197
+ // checkout the repo to the HEAD of the current branch
198
+ await $ `git checkout --force ${ ref } ` ;
199
+
200
+ // recreate the benchmark directory
201
+ await $ `rm -rf ./benchmark` ;
202
+ // intentionally use the same folder for both experiment and control
203
+ await $ `cp -r ${ BENCHMARK_FOLDER } ./benchmark` ;
204
+
205
+ // `pnpm install` and build the repo
206
+ await $ `pnpm install --no-frozen-lockfile` ;
207
+ await $ `pnpm build` ;
208
+
209
+ // rewrite all `package.json`s to behave like published packages
210
+ await rewritePackageJson ( ) ;
211
+
212
+ // build the benchmarks using vite
213
+ await cd ( benchDir ) ;
214
+ await $ `pnpm vite build` ;
215
+ }
0 commit comments