Skip to content

Commit 2c77149

Browse files
thorn0fisker
andauthored
Add benchmark script (#13224)
Co-authored-by: fisker <lionkay@gmail.com>
1 parent bbbeeb0 commit 2c77149

8 files changed

Lines changed: 74 additions & 4 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ package-lock.json
2828
.pnp.*
2929
.nyc_output
3030
.devcontainer
31+
# When installing software on gitpod.io, `core.*` files are generated
32+
/core.*

.gitpod.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
tasks:
44
- init: |
5+
brew install hyperfine
56
yarn
6-
yarn build
77
88
github:
99
# https://www.gitpod.io/docs/prebuilds/#configure-prebuilds

CONTRIBUTING.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ In the above commands:
9595

9696
In addition to the options above, you can use [`node --prof` and `node --prof-process`](https://nodejs.org/en/docs/guides/simple-profiling/), as well as `node --trace-opt --trace-deopt`, to get more advanced performance insights.
9797

98+
The script `scripts/benchmark/compare.sh` can be used to compare performance of two or more commits/branches using [hyperfine](https://github.com/sharkdp/hyperfine). Usage (don't forget to install hyperfine):
99+
100+
```sh
101+
PRETTIER_PERF_FILENAME=my.js ./compare.sh main some-pr-branch
102+
```
103+
104+
Without `PRETTIER_PERF_FILENAME`, the script uses a default JS file as input for Prettier:
105+
106+
```sh
107+
./compare.sh main next
108+
```
109+
98110
## Regression testing
99111

100112
We have a cool tool for regression testing that runs on GitHub Actions. Have a look: https://github.com/prettier/prettier-regression-testing

cspell.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@
222222
"neoclide",
223223
"neoformat",
224224
"neovim",
225-
"nocheck",
226225
"nnoremap",
226+
"nocheck",
227227
"nonenumerable",
228228
"Nonspacing",
229229
"noopener",
@@ -271,9 +271,9 @@
271271
"Pschera",
272272
"quasis",
273273
"Raghuvir",
274+
"raquo",
274275
"Rasmus",
275276
"Rattray",
276-
"raquo",
277277
"rattrayalex",
278278
"readline",
279279
"readlines",
@@ -291,8 +291,8 @@
291291
"sandhose",
292292
"Sapegin",
293293
"sbdchd",
294-
"sdbm",
295294
"scandir",
295+
"sdbm",
296296
"Serializers",
297297
"setlocal",
298298
"setq",
@@ -330,6 +330,7 @@
330330
"testname",
331331
"tldr",
332332
"Tomasek",
333+
"toplevel",
333334
"Tradeshift",
334335
"Transloadit",
335336
"trippable",

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
"perf": "yarn && yarn build && cross-env NODE_ENV=production node ./dist/bin-prettier.js",
151151
"perf:inspect": "yarn && yarn build && cross-env NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js",
152152
"perf:benchmark": "yarn perf --debug-benchmark",
153+
"perf:compare": "./scripts/benchmark/compare.sh",
153154
"lint": "run-p lint:*",
154155
"lint:typecheck": "tsc",
155156
"lint:eslint": "cross-env EFF_NO_LINK_RULES=true eslint . --format friendly",

scripts/benchmark/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*/

scripts/benchmark/bench.mjs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { readFileSync } from "node:fs";
2+
3+
const [, , version, method, groupCountString = 100, groupSizeString = 10] =
4+
process.argv;
5+
const groupCount = Number(groupCountString);
6+
const groupSize = Number(groupSizeString);
7+
const { format } = await import(`./${version}/dist/index.js`);
8+
9+
const sourceText = readFileSync(
10+
process.env.PRETTIER_PERF_FILENAME || "../../src/language-js/utils/index.js",
11+
"utf8"
12+
);
13+
14+
for (let i = 0; i < groupCount; i++) {
15+
if (method === "serial") {
16+
for (let i = 0; i < groupSize; i++) {
17+
await format(sourceText, { parser: "typescript" });
18+
}
19+
}
20+
21+
if (method === "parallel") {
22+
await Promise.allSettled(
23+
Array.from({ length: groupSize }, () =>
24+
format(sourceText, { parser: "typescript" })
25+
)
26+
);
27+
}
28+
}

scripts/benchmark/compare.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
3+
commits=("${@}")
4+
5+
root=`git rev-parse --show-toplevel`
6+
cd "$root/scripts/benchmark"
7+
8+
function cleanup {
9+
echo Cleaning up...
10+
rm -rf "${commits[@]}"
11+
echo Done!
12+
}
13+
trap cleanup EXIT
14+
15+
args=()
16+
17+
for commit in "${commits[@]}"; do
18+
rm -rf $commit
19+
git -C "$root" archive $commit --prefix $commit/ ':!tests*' ':!website' ':!docs' | tar -x
20+
(cd $commit; yarn; yarn build)
21+
args+=("node ./bench.mjs $commit serial")
22+
args+=("node ./bench.mjs $commit parallel")
23+
done
24+
25+
hyperfine --warmup 3 "${args[@]}"

0 commit comments

Comments
 (0)