|
| 1 | +import {Filename, npath, ppath, xfs} from '@yarnpkg/fslib'; |
| 2 | +import chalk from 'chalk'; |
| 3 | +import cp from 'child_process'; |
| 4 | + |
| 5 | +const repoDir = ppath.dirname(npath.toPortablePath(__dirname)); |
| 6 | + |
| 7 | +xfs.mktempSync(temp => { |
| 8 | + const nextEnv = {...process.env}; |
| 9 | + |
| 10 | + nextEnv.YARN_IGNORE_PATH = `1`; |
| 11 | + nextEnv.NODE_OPTIONS = ``; |
| 12 | + |
| 13 | + function exec(arg0: string, argv: Array<string>, opts: cp.ExecFileSyncOptions) { |
| 14 | + try { |
| 15 | + cp.execFileSync(arg0, argv, {cwd: npath.fromPortablePath(temp), env: nextEnv, ...opts}); |
| 16 | + } catch (err) { |
| 17 | + if (err.status !== 128) { |
| 18 | + throw err; |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + const binaryDir = ppath.join(temp, `bin`); |
| 24 | + xfs.mkdirSync(binaryDir); |
| 25 | + nextEnv.PATH = `${npath.fromPortablePath(binaryDir)}:${process.env.PATH}`; |
| 26 | + |
| 27 | + // We create two binary folders; each benchmark will use one or the other |
| 28 | + xfs.mkdirSync(ppath.join(binaryDir, `before`)); |
| 29 | + xfs.symlinkSync(ppath.join(binaryDir, `yarn-before`), ppath.join(binaryDir, `before`, `yarn`)); |
| 30 | + xfs.symlinkSync(ppath.join(repoDir, `packages/yarnpkg-cli/bundles/yarn.js`), ppath.join(binaryDir, `yarn-after`)); |
| 31 | + |
| 32 | + xfs.mkdirSync(ppath.join(binaryDir, `after`)); |
| 33 | + xfs.symlinkSync(ppath.join(binaryDir, `yarn-after`), ppath.join(binaryDir, `after`, `yarn`)); |
| 34 | + |
| 35 | + // We create a few helper scripts to make the benchmarking easier |
| 36 | + xfs.writeFileSync(ppath.join(binaryDir, `yarn-repo`), [ |
| 37 | + `#!/bin/bash\n`, |
| 38 | + `cd ${npath.fromPortablePath(repoDir)}\n`, |
| 39 | + `unset YARN_IGNORE_PATH\n`, |
| 40 | + `export PATH="${process.env.PATH}"\n`, |
| 41 | + `exec yarn $@\n`, |
| 42 | + ].join(``)); |
| 43 | + |
| 44 | + xfs.writeFileSync(ppath.join(binaryDir, `bench-commit`), [ |
| 45 | + `#!/bin/bash\n`, |
| 46 | + `cd ${npath.fromPortablePath(temp)}\n`, |
| 47 | + `git add . && (git diff-index --quiet HEAD || git commit -m Commit > /dev/null)\n`, |
| 48 | + ].join(``)); |
| 49 | + |
| 50 | + xfs.writeFileSync(ppath.join(binaryDir, `bench-reset`), [ |
| 51 | + `#!/bin/bash\n`, |
| 52 | + `cd ${npath.fromPortablePath(temp)}\n`, |
| 53 | + `git reset --hard HEAD && git clean -fdx\n`, |
| 54 | + ].join(``)); |
| 55 | + |
| 56 | + xfs.writeFileSync(ppath.join(binaryDir, `bench-run`), [ |
| 57 | + `#!/bin/bash\n`, |
| 58 | + `cd ${npath.fromPortablePath(temp)}\n`, |
| 59 | + `\n`, |
| 60 | + `PATH="$(pwd)/bin/hyperfine:$PATH" hyperfine --warmup 1 \\\n`, |
| 61 | + ` --export-markdown=.git/yarn-bench \\\n`, |
| 62 | + ` --prepare 'bench-reset && bash "${npath.fromPortablePath(temp)}"/bench-prepare.sh' \\\n`, |
| 63 | + ` 'before' \\\n`, |
| 64 | + ` 'after'\n`, |
| 65 | + `\n`, |
| 66 | + `if which pbcopy >/dev/null 2>&1; then\n`, |
| 67 | + ` pbcopy < .git/yarn-bench\n`, |
| 68 | + `fi\n`, |
| 69 | + ].join(``)); |
| 70 | + |
| 71 | + // We don't want Yarn to be called directly, since it'd be a global copy we don't control |
| 72 | + xfs.writeFileSync(ppath.join(binaryDir, `yarn`), [ |
| 73 | + `#!/bin/bash\n`, |
| 74 | + `echo "Don't run Yarn directly:"\n`, |
| 75 | + `echo\n`, |
| 76 | + `echo " - if the output is the same between both versions, use yarn-before or yarn-after instead"\n`, |
| 77 | + `echo " - otherwise, add the call to the bench-prepare.sh script"\n`, |
| 78 | + `exit 1\n`, |
| 79 | + ].join(``)); |
| 80 | + |
| 81 | + // We also create another binary dir, just to clean up the command line we show in Hyperfine |
| 82 | + xfs.mkdirSync(ppath.join(binaryDir, `hyperfine`)); |
| 83 | + |
| 84 | + xfs.writeFileSync(ppath.join(binaryDir, `hyperfine/before`), [ |
| 85 | + `#!/bin/bash\n`, |
| 86 | + `cd ${npath.fromPortablePath(temp)}\n`, |
| 87 | + `PATH="$(pwd)/bin/before:$PATH" bash bench-script.sh\n`, |
| 88 | + ].join(``)); |
| 89 | + |
| 90 | + xfs.writeFileSync(ppath.join(binaryDir, `hyperfine/after`), [ |
| 91 | + `#!/bin/bash\n`, |
| 92 | + `cd ${npath.fromPortablePath(temp)}\n`, |
| 93 | + `PATH="$(pwd)/bin/after:$PATH" bash bench-script.sh\n`, |
| 94 | + ].join(``)); |
| 95 | + |
| 96 | + // Those two scripts are meant to be written by the user (but not run directly, so not executable and no shebang) |
| 97 | + xfs.writeFileSync(ppath.join(temp, `bench-prepare.sh`), ``); |
| 98 | + xfs.writeFileSync(ppath.join(temp, `bench-script.sh`), `yarn install\n`); |
| 99 | + |
| 100 | + // General Yarn configuration |
| 101 | + xfs.writeJsonSync(ppath.join(temp, Filename.rc), { |
| 102 | + globalFolder: `.yarn/global`, |
| 103 | + }); |
| 104 | + |
| 105 | + xfs.writeJsonSync(ppath.join(temp, Filename.manifest), { |
| 106 | + name: `benchmark`, |
| 107 | + private: true, |
| 108 | + }); |
| 109 | + |
| 110 | + // We retrieve the latest Yarn version from master, and add it to the PATH |
| 111 | + exec(`yarn-after`, [`set`, `version`, `from`, `sources`], {stdio: `inherit`}); |
| 112 | + |
| 113 | + const releaseFolder = ppath.join(temp, `.yarn/releases`); |
| 114 | + xfs.moveSync(ppath.join(releaseFolder, xfs.readdirSync(releaseFolder)[0]), ppath.join(binaryDir, `yarn-before`)); |
| 115 | + |
| 116 | + // All binaries should be executable |
| 117 | + for (const name of xfs.readdirSync(binaryDir, {recursive: true})) |
| 118 | + xfs.chmodSync(ppath.join(binaryDir, name), 0o755); |
| 119 | + |
| 120 | + exec(`git`, [`init`], {stdio: `ignore`}); |
| 121 | + exec(`git`, [`config`, `core.hooksPath`, npath.fromPortablePath(temp)], {stdio: `ignore`}); |
| 122 | + exec(`git`, [`add`, `.`], {stdio: `ignore`}); |
| 123 | + exec(`git`, [`commit`, `-m`, `First commit`, `--allow-empty`], {stdio: `ignore`}); |
| 124 | + exec(`bench-commit`, [], {stdio: `ignore`}); |
| 125 | + |
| 126 | + process.stdout.write(`\x1bc`); |
| 127 | + |
| 128 | + console.log(`You're now in the benchmarking environment. Here is how it works:`); |
| 129 | + console.log(); |
| 130 | + console.log(` - Setup the initial state of your repository, then run ${chalk.magenta(`bench-commit`)} to persist it in the temporary repository.`); |
| 131 | + console.log(` (note that this repository intentionally lacks a gitignore - feel free to commit archives, metadata, etc)`); |
| 132 | + console.log(` - If you want to run some code before the benchmark, add it to the ${chalk.yellow(`bench-prepare.sh`)} script in this folder.`); |
| 133 | + console.log(` - By default the benchmark will run ${chalk.magenta(`yarn install`)}; you can change that by editing ${chalk.yellow(`bench-script.sh`)}.`); |
| 134 | + console.log(` - When you want to run the benchmark, run ${chalk.magenta(`bench-run`)}. The repository will be reset between each run.`); |
| 135 | + console.log(` - If using OSX, the results will be automatically copied to your clipboard. Otherwise, they'll be available in ${chalk.yellow(`.git/yarn-bench`)}.`); |
| 136 | + console.log(); |
| 137 | + console.log(`Once you're done, exit the shell and the temporary environment will be removed.`); |
| 138 | + |
| 139 | + nextEnv.PS1 = `\\[\x1b[94m\\](Yarn benchmarking tool)\\[\x1b[39m\\] \\[\x1b[1m\\]$\\[\x1b[22m\\] `; |
| 140 | + nextEnv.PROMPT_COMMAND = `echo; trap 'echo; trap - DEBUG' DEBUG`; |
| 141 | + |
| 142 | + exec(`bash`, [], {stdio: `inherit`}); |
| 143 | +}); |
0 commit comments