-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathbenchmarkFixture.js
More file actions
420 lines (365 loc) · 14.6 KB
/
Copy pathbenchmarkFixture.js
File metadata and controls
420 lines (365 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
'use strict'
import path from 'path'
import pathKey from 'path-key'
import spawn from "cross-spawn"
import { promises as fs, cpSync } from 'fs'
import getFolderSize from 'get-folder-size'
import rimraf from 'rimraf'
import { fileURLToPath } from 'url'
import tempy from 'tempy'
const DIRNAME = path.dirname(fileURLToPath(import.meta.url))
const FIXTURES_DIR = path.join(DIRNAME, 'fixtures')
const TMP = tempy.directory()
const lockfileNameByPM = {
npm: 'package-lock.json',
pnpm: 'pnpm-lock.yaml',
yarn: 'yarn.lock',
bun: 'bun.lock'
}
export function createEnv (managersDir) {
const pathEnv = pathKey()
const env = Object.create(process.env)
env[pathEnv] = [
// Where pnpm 12 links the executables of the package managers it
// provisions: the directory is the manager's own pnpm home, and `bin` is
// its global bin directory.
path.join(managersDir, 'bin'),
// Where tools installed as packages keep theirs — pnpr today.
path.join(managersDir, 'node_modules/.bin'),
path.dirname(process.execPath),
process.env[pathEnv]
].join(path.delimiter)
return env
}
/**
* Makes the pnpr server resolve next to the registry instead of across the
* emulated link.
*
* pnpr resolves against the registry URL the client sends, and the client's
* configured registry sits behind the latency proxy — so without this, the
* server would fetch every packument by going out through the emulated link
* and back into its own front door, paying the very round trips per graph
* level that server-side resolution exists to remove. These are the
* benchmark-only hooks pnpm's client reads (`PnprBenchmarkRegistryOverride`
* in the pnpm monorepo, put there for its `integrated-benchmark` task, which
* models the same co-located server): the resolve request carries this
* registry — the un-proxied one — and any URL in the answer that names it is
* rewritten back to the client's own registry, so tarballs still cross the
* emulated link like every other scenario's do.
*/
export function applyPnprServerRegistry (env, registry) {
env.PACQUET_BENCHMARK_PNPR_SERVER_REGISTRY = registry
env.PACQUET_BENCHMARK_PNPR_TARBALL_REWRITE_FROM = registry
}
function cleanLockfile (pm, cwd, env) {
const lockfileName = lockfileNameByPM[pm.name]
rimraf.sync(path.join(cwd, lockfileName))
if (pm.name === 'yarn') {
// This ensures yarn berry to install under a nested folder
spawnSyncOrThrow({ name: 'nodetouch', args: [lockfileName] }, { env, cwd, stdio: "inherit" })
}
}
/**
* The dependencies the `updatedDependencies` scenario adds on top of the
* fixture's own. Exported so the registry warm-up can install this graph
* untimed: it is a different set of packages than the fixture's, and a
* registry that has never served it would make whoever asks first pay to
* pull it from npmjs inside a measured run.
*/
export const UPDATED_DEPENDENCIES = {
"babel-core": "^6.4.0",
"babel-eslint": "^6.1.2",
"babel-loader": "^6.2.1",
"babel-plugin-lodash": "^3.2.11",
"babel-plugin-module-resolver": "^2.2.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-runtime": "^6.4.3",
"babel-polyfill": "^6.23.0",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-react-hmre": "^1.0.1",
"babel-preset-stage-1": "^6.3.13",
"babel-runtime": "^6.3.19",
"clean-webpack-plugin": "^0.1.16",
"core-decorators": "^0.12.3",
"css-loader": "^0.23.1",
"css-mqpacker": "^4.0.0",
"react": "^15.4.1",
"react-addons-css-transition-group": "^15.3.0",
"react-addons-shallow-compare": "^15.3.0",
"react-dnd": "^2.1.4",
"react-dnd-html5-backend": "^2.1.2",
"react-dom": "^15.4.1",
"react-draft-wysiwyg": "^1.6.5",
"react-dropzone": "^3.5.3",
"react-grid-layout": "^0.12.6",
"react-highcharts": "^11.5.0",
"react-hot-loader": "v3.0.0-beta.6",
"react-input-calendar": "^0.3.14",
"react-lazyload": "^2.2.5",
"react-measure": "^1.4.6",
"react-mixin": "^3.0.3",
"react-responsive": "^1.2.5",
"react-responsive-tabs": "^0.5.3",
"react-router": "^4.0.0",
"react-router-dom": "^4.0.0",
"react-select-plus": "^1.0.0-rc",
"react-skylight": "^0.3.0",
"react-sortablejs": "^1.2.1",
"react-tappable": "^0.8.4",
"react-tooltip": "3.11.2",
"react-virtualized": "^7.19.4",
"react-waypoint": "^5.2.0",
}
async function updateDependenciesInPackageJson (cwd) {
const packageJsonPath = path.join(cwd, 'package.json')
const buf = await fs.readFile(packageJsonPath)
const originalAsString = buf.toString()
const parsed = JSON.parse(originalAsString)
parsed.dependencies = {
...parsed.dependencies,
...UPDATED_DEPENDENCIES,
}
const modifiedAsString = JSON.stringify(parsed)
await fs.writeFile(packageJsonPath, modifiedAsString)
// return the original file so that we can replace it when done
return originalAsString
}
/**
* Points a package manager at a registry other than the public one: every
* install is measured against the benchmark's own pnpr.
*
* Reads are anonymous, so no token is needed to fetch packages. The token is
* only for pnpr's resolver, which does require authentication, and it is
* written as `_authToken` rather than `_auth` because Basic credentials would
* cost a password hash on every request inside the measured loop.
*/
async function writeRegistryConfig (pm, cwd, opts) {
let npmrc = `registry=${opts.registry}\n`
if (opts.authToken) {
// The resolver is reached on a link of its own, so it is a different host
// and port than the registry. pnpm sends whichever credential is
// configured for the URL it is talking to, and the resolver is the one
// that demands one, so both are declared.
const hosts = new Set([opts.registry, opts.pnprServer].filter(Boolean).map((url) => new URL(url).host))
for (const host of hosts) {
npmrc += `//${host}/:_authToken=${opts.authToken}\n`
}
}
await fs.writeFile(path.join(cwd, '.npmrc'), npmrc)
if (pm.name === 'pnpm') {
await fs.writeFile(path.join(cwd, 'pnpm-workspace.yaml'), pnpmWorkspaceYaml(opts))
}
}
/**
* The workspace manifest every pnpm scenario installs under. `packages`
* declares the fixture its own workspace root, so pnpm never walks up into a
* directory the benchmark doesn't control. When the scenario offloads
* resolution, `pnprServer` names the server it happens on.
*
* `minimumReleaseAge` is deliberately left at pnpm's default here, unlike
* the directories the managers are installed into. The hold — and the
* lockfile verification pass it drives — is pnpm's default behavior, so it
* belongs in the measurement; the client sends the setting along with its
* resolve and verify-lockfile requests, so the server applies the same
* cutoff and both pnpm columns still install the same graph. Setting it to
* zero would leave no verifier configured at all, silently deleting the
* verification work whose offload to the server the accelerated column
* exists to measure.
*/
export function pnpmWorkspaceYaml (opts = {}) {
let yaml = "packages:\n - '.'\n"
if (opts.pnprServer) {
yaml += `pnprServer: ${opts.pnprServer}\n`
}
return yaml
}
/**
* Undoes an install. Yarn PnP writes no `node_modules` at all, so removing only
* that would leave its whole installation in place and hand the scenario that
* follows a project that is already installed.
*/
function removeInstallOutput (cwd, modules) {
if (modules) {
rimraf.sync(modules)
}
for (const name of ['.pnp.cjs', '.pnp.loader.mjs', '.yarn']) {
rimraf.sync(path.join(cwd, name))
}
}
export default async function benchmark (pm, fixture, opts) {
const cwd = path.join(TMP, pm.scenario, fixture)
const env = createEnv(opts.managersDir)
if (pm.rustEngine) {
// pnpm 12's Rust engine uses the store layout at $PNPM_HOME/store, which lines
// up with the cache/ directory the rest of the flow cleans between scenarios.
env.PNPM_HOME = path.join(cwd, 'cache')
// $PNPM_HOME moves only the store: the engine's cacheDir — the packument
// mirror and the lockfile-verification verdict file — defaults to the
// machine's own ~/.cache/pnpm, where it would survive every wipe. Left
// there, no measured install ever resolves or verifies cold: the untimed
// warm-up fills the mirror and the "no cache" rows silently read it,
// an advantage no other column gets (their caches all live under the
// cache/ directory the scenarios delete). Pin it inside cache/ like
// pnpm 11's --cache-dir, so wiping cache/ means what it says.
env.PNPM_CONFIG_CACHE_DIR = path.join(cwd, 'cache', 'cache')
}
if (opts.pnprServerRegistry) {
applyPnprServerRegistry(env, opts.pnprServerRegistry)
}
cpSync(path.join(FIXTURES_DIR, fixture), cwd, { recursive: true })
const modules = opts.hasNodeModules ? path.join(cwd, 'node_modules') : null
cleanLockfile(pm, cwd, env)
if (opts.registry) {
await writeRegistryConfig(pm, cwd, opts)
}
if (pm.name === 'yarn') {
// Every store Yarn keeps has to sit under `cache/`, the directory the
// scenarios below delete to go back to a cold cache. `enableMirror` alone
// stopped being enough once Yarn started caching globally by default:
// without `enableGlobalCache`, packages land in `globalFolder` instead and
// survive every scenario, so no run ever measures a cold cache.
let yarnRc =
'enableImmutableInstalls: false\n'
+ 'enableGlobalCache: false\n'
+ 'enableMirror: false\n'
+ `cacheFolder: ${path.join(cwd, 'cache')}\n`
+ `globalFolder: ${path.join(cwd, 'cache', 'global')}\n`
+ 'enableScripts: false\n'
// Yarn reads none of `.npmrc`, so its registry has to be set here.
+ (opts.registry ? `npmRegistryServer: ${opts.registry}\nunsafeHttpWhitelist:\n - 127.0.0.1\n` : '')
/**
* @see https://yarnpkg.com/configuration/yarnrc#nodeLinker
*/
switch (pm.scenario) {
case 'yarn':
yarnRc += 'nodeLinker: pnpm\n'
+ 'nmMode: hardlinks-local\n'
+ 'compressionLevel: 0\n'
break
case 'yarn_pnp':
yarnRc += 'nodeLinker: pnp\n'
break
}
await fs.writeFile(path.join(cwd, '.yarnrc.yml'), yarnRc)
}
// Installs that nothing is measured from, to keep whatever only the very
// first install of a run pays for out of the numbers: a registry answering a
// question it has never been asked, a package manager's own start-up, a
// filesystem that has not seen these paths yet.
//
// It runs three times because an install without a lockfile, an install with
// one, and an install whose manifest the lockfile no longer matches are
// different questions, and a server that caches answers has to have been
// asked all three. The first warm-up produces the lockfile the second one
// sends, and `node_modules` goes away in between: a package manager that
// already has the right tree installed skips resolving altogether, so leaving
// it in place means the second warm-up asks nothing and the question it
// exists to ask stays cold until a scenario that is being measured asks it.
// The third asks the `updatedDependencies` question — without it, that
// scenario would be the only measured install whose resolution the server
// (and, for the accelerated column, pnpr's resolver) had never seen, and it
// would pay first-ever costs inside the one run that is timed.
console.log('# warm-up (not measured)')
measureInstall(pm, cwd, env)
removeInstallOutput(cwd, modules)
measureInstall(pm, cwd, env)
removeInstallOutput(cwd, modules)
const warmUpPackageJson = await updateDependenciesInPackageJson(cwd)
measureInstall(withUpdateArgs(pm), cwd, env)
await fs.writeFile(path.join(cwd, 'package.json'), warmUpPackageJson)
removeInstallOutput(cwd, modules)
rimraf.sync(path.join(cwd, 'cache'))
cleanLockfile(pm, cwd, env)
console.log(`# first install`)
const firstInstall = measureInstall(pm, cwd, env)
let repeatInstall
if (modules) {
console.log(`# repeat install`)
repeatInstall = measureInstall(pm, cwd, env)
rimraf.sync(modules)
} else {
repeatInstall = 0
}
console.log(`# with warm cache and lockfile`)
const withWarmCacheAndLockfile = measureInstall(pm, cwd, env)
if (modules) {
rimraf.sync(modules)
}
cleanLockfile(pm, cwd)
console.log('# with warm cache')
const withWarmCache = measureInstall(pm, cwd, env)
if (modules) {
rimraf.sync(modules)
}
rimraf.sync(path.join(cwd, 'cache'))
console.log('# with lockfile')
const withLockfile = measureInstall(pm, cwd, env)
cleanLockfile(pm, cwd)
let withWarmCacheAndModules
let withWarmModulesAndLockfile
let withWarmModules
let size
if (modules) {
console.log('# with warm cache and modules')
withWarmCacheAndModules = measureInstall(pm, cwd, env)
rimraf.sync(path.join(cwd, 'cache'))
console.log('# with warm modules and lockfile')
withWarmModulesAndLockfile = measureInstall(pm, cwd, env)
rimraf.sync(path.join(cwd, 'cache'))
cleanLockfile(pm, cwd)
console.log('# with warm modules')
withWarmModules = measureInstall(pm, cwd, env)
size = await getFolderSize.loose(modules)
} else {
withWarmCacheAndModules =
withWarmModulesAndLockfile =
withWarmModules = 0
size = await getFolderSize.loose(path.join(cwd, 'cache'))
}
console.log('# with updated dependencies')
// update all dependency versions to '*' and install again
const originalPackageJson = await updateDependenciesInPackageJson(cwd)
const updatedDependencies = measureInstall(withUpdateArgs(pm), cwd, env)
// revert `package.json` back to its original state, just in case
await fs.writeFile(path.join(cwd, 'package.json'), originalPackageJson)
rimraf.sync(cwd)
return {
firstInstall,
repeatInstall,
withWarmCacheAndLockfile,
withWarmCache,
withLockfile,
withWarmCacheAndModules,
withWarmModulesAndLockfile,
withWarmModules,
updatedDependencies,
size
}
}
/**
* The command for an install whose `package.json` changed under an existing
* lockfile. pnpm turns on `--frozen-lockfile` when CI is set, which refuses
* exactly that state, so the flag has to be lifted for it.
*/
function withUpdateArgs (pm) {
if (pm.name !== 'pnpm') return pm
return {
...pm,
args: [...pm.args, '--no-frozen-lockfile'],
}
}
function measureInstall (cmd, cwd, env) {
const startTime = Date.now()
console.log(`> ${cmd.name} ${cmd.args.join(' ')}`)
spawnSyncOrThrow(cmd, { env, cwd, stdio: "inherit" });
const endTime = Date.now()
return endTime - startTime
}
function spawnSyncOrThrow (cmd, opts) {
const result = spawn.sync(cmd.name, cmd.args, opts);
if (result.status !== 0) {
throw new Error(`${cmd.name} failed with status code ${result.status}`)
}
return result;
}