-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.mjs
182 lines (151 loc) · 5.16 KB
/
bench.mjs
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
import * as cp from 'node:child_process';
import * as fs from 'node:fs';
function cleanOutput() {
cp.execSync(`find packages -name out-tsc | xargs rm -rf`, {
cwd: 'ts-solution',
});
}
function toSeconds(ms) {
return `${(ms / 1000).toFixed(2)}s`;
}
function updatePkg(i) {
const file = `./ts-solution/packages/lib-${i}/src/index.tsx`;
const content = fs.readFileSync(file).toString();
fs.writeFileSync(file, `${content};\nexport const foo${randInt()} = 'foo'`);
}
function updateNestedLeafPkg(i) {
const file = `./ts-solution/packages/nested-1-${i}/src/index.tsx`;
const content = fs.readFileSync(file).toString();
fs.writeFileSync(file, `${content};\nexport const foo${randInt()} = 'foo'`);
}
function updateNestedRootPkg() {
const file = `./ts-solution/packages/nested-1/src/index.tsx`;
const content = fs.readFileSync(file).toString();
fs.writeFileSync(file, `${content};\nexport const foo${randInt()} = 'foo'`);
}
function resetChanges() {
cp.execSync('git checkout ts-solution');
}
function randInt() {
return Math.ceil(Math.random() * 1000);
}
function formatMemory(kb) {
const m = parseInt(kb);
if (m >= 1_000_000) {
return `${(m / 1_000_000).toFixed(2)} GB`;
} else if (m >= 1000) {
return `${(m / 1_000).toFixed(2)} MB`;
} else {
return `${m} KB`;
}
}
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function exec(cmd, opts) {
return new Promise((resolve) => {
let data = '';
const p = cp.exec(cmd, opts);
p.stdout.on('data', (buf) => {
data += buf.toString();
});
p.stderr.on('data', (buf) => {
data += buf.toString();
});
p.on('close', () => {
resolve(data);
});
});
}
process.on('SIGINT', () => {
resetChanges()
process.exit();
});
process.on('exit', () => resetChanges());
let start, end, output, mem;
const timeCmd = process.platform === 'darwin' ? 'gtime' : 'time';
const memRegexp = /Maximum resident set size \(kbytes\): (?<mem>\d+)/;
console.log('Timing how long typechecks take for old vs new TS setup. This will take a few minutes.\n');
console.log('INTEGRATED SETUP (OLD)');
console.log('----------------------');
process.stdout.write('Timing typecheck... ');
cleanOutput();
start = performance.now();
output = await exec(`${timeCmd} -v npx nx typecheck demo --skip-nx-cache`, {
cwd: 'ts-integrated',
});
end = performance.now();
mem = output.match(memRegexp).groups.mem;
process.stdout.write(`${toSeconds(end - start)} (max memory: ${formatMemory(mem)})\n`);
console.log('\nTS SOLUTION SETUP (NEW)');
console.log('-----------------------');
cleanOutput();
process.stdout.write('Timing typecheck (cold)... ')
start = performance.now();
output = await exec(`${timeCmd} -v npx nx typecheck demo --skip-nx-cache`, {
cwd: 'ts-solution',
});
end = performance.now();
mem = output.match(memRegexp).groups.mem;
process.stdout.write(`${toSeconds(end - start)} (max memory: ${formatMemory(mem)})\n`);
await delay(2000);
process.stdout.write('Timing typecheck (hot)... ');
start = performance.now();
output = await exec(`${timeCmd} -v npx nx typecheck demo --skip-nx-cache`, {
cwd: 'ts-solution',
});
end = performance.now();
mem = output.match(memRegexp).groups.mem;
process.stdout.write(`${toSeconds(end - start)} (max memory: ${formatMemory(mem)})\n`);
const benchWarmLeafUpdates = async (n) => {
process.stdout.write(`Timing typecheck (warm - ${n} pkg updated)... `);
for (let i = 1; i <= n; i++) {
updatePkg(i);
}
start = performance.now();
output = await exec(`${timeCmd} -v npx nx typecheck demo --skip-nx-cache`, {
cwd: 'ts-solution',
});
end = performance.now();
mem = output.match(memRegexp).groups.mem;
process.stdout.write(`${toSeconds(end - start)} (max memory: ${formatMemory(mem)})\n`);
};
await delay(2000);
await benchWarmLeafUpdates(1);
await delay(2000);
await benchWarmLeafUpdates(5);
await delay(2000);
await benchWarmLeafUpdates(25);
await delay(2000);
await benchWarmLeafUpdates(100);
await delay(2000);
process.stdout.write('Timing typecheck (warm - 1 nested leaf pkg updated)... ');
updateNestedLeafPkg(1);
start = performance.now();
output = await exec(`${timeCmd} -v npx nx typecheck demo --skip-nx-cache`, {
cwd: 'ts-solution',
});
end = performance.now();
mem = output.match(memRegexp).groups.mem;
process.stdout.write(`${toSeconds(end - start)} (max memory: ${formatMemory(mem)})\n`);
await delay(2000);
process.stdout.write('Timing typecheck (warm - 2 nested leaf pkg updated)... ');
updateNestedLeafPkg(1);
updateNestedLeafPkg(2);
start = performance.now();
output = await exec(`${timeCmd} -v npx nx typecheck demo --skip-nx-cache`, {
cwd: 'ts-solution',
});
end = performance.now();
mem = output.match(memRegexp).groups.mem;
process.stdout.write(`${toSeconds(end - start)} (max memory: ${formatMemory(mem)})\n`);
await delay(2000);
process.stdout.write('Timing typecheck (warm - 1 nested root pkg updated)... ');
updateNestedRootPkg();
start = performance.now();
output = await exec(`${timeCmd} -v npx nx typecheck demo --skip-nx-cache`, {
cwd: 'ts-solution',
});
end = performance.now();
mem = output.match(memRegexp).groups.mem;
process.stdout.write(`${toSeconds(end - start)} (max memory: ${formatMemory(mem)})\n`);