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
|
import cp from "child_process";
import fs from "fs";
import fse from "fs-extra";
import path from "path";
import ndjson from "ndjson";
const REPO_ROOT = "large-monorepo";
const REPO_ORIGIN = "https://github.com/gsoltis/large-monorepo.git";
const REPO_PATH = path.join(process.cwd(), REPO_ROOT);
const REPETITIONS = 5;
const DEFAULT_EXEC_OPTS = { stdio: "ignore" as const, cwd: REPO_PATH };
const TURBO_BIN = path.resolve(path.join("..", "target", "release", "turbo"));
const DEFAULT_CACHE_PATH = path.join(
REPO_PATH,
"node_modules",
".cache",
"turbo"
);
const ALT_CACHE_PATH = path.join(
REPO_PATH,
"node_modules",
".cache",
"turbo-benchmark"
);
type Timing = number;
type Benchmark = {
name: string;
unit: string;
value: number;
range?: string;
extra?: string;
};
type TBirdEvent = {
commitSha: string;
commitTimestamp: Date;
platform: string;
benchmark: string;
durationMs: number;
};
function setup(): void {
// Clone repo if it doesn't exist, run clean
if (fs.existsSync(REPO_ROOT)) {
// reset the repo, remove all changed or untracked files
cp.execSync(
`cd ${REPO_ROOT} && git reset --hard HEAD && git clean -f -d -X`,
{
stdio: "inherit",
}
);
} else {
cp.execSync(`git clone ${REPO_ORIGIN}`, { stdio: "ignore" });
}
// Run install so we aren't benchmarking node_modules ...
cp.execSync("yarn install", DEFAULT_EXEC_OPTS);
}
function cleanTurboCache(): void {
if (fs.existsSync(DEFAULT_CACHE_PATH)) {
console.log("clearing cache");
fs.rmSync(DEFAULT_CACHE_PATH, { recursive: true });
}
}
function cleanBuild(): Timing[] {
const timings: Timing[] = [];
const isLocal = process.argv[process.argv.length - 1] == "--local";
// We aren't really benchmarking this one, it OOMs if run in full parallel
// on GH actions
const repetitions = isLocal ? REPETITIONS : 1;
const concurrency = isLocal ? "" : " --concurrency=1";
for (let i = 0; i < repetitions; i++) {
// clean first, we'll leave the cache in place for subsequent builds
cleanTurboCache();
const start = new Date().getTime();
cp.execSync(`${TURBO_BIN} run build${concurrency}`, DEFAULT_EXEC_OPTS);
const end = new Date().getTime();
const timing = end - start;
timings.push(timing);
}
return timings;
}
function cachedBuild(): Timing[] {
const timings: Timing[] = [];
for (let i = 0; i < REPETITIONS; i++) {
const start = new Date().getTime();
cp.execSync(`${TURBO_BIN} run build`, DEFAULT_EXEC_OPTS);
const end = new Date().getTime();
const timing = end - start;
timings.push(timing);
}
return timings;
}
function saveCache() {
// Remove any existing backup
if (fs.existsSync(ALT_CACHE_PATH)) {
fs.rmSync(ALT_CACHE_PATH, { recursive: true });
}
// copy the current cache to the backup
if (fs.existsSync(DEFAULT_CACHE_PATH)) {
fse.copySync(DEFAULT_CACHE_PATH, ALT_CACHE_PATH, { recursive: true });
} else {
// make an empty cache
fs.mkdirSync(ALT_CACHE_PATH, { recursive: true });
}
}
function restoreSavedCache() {
// Remove any existing cache
if (fs.existsSync(DEFAULT_CACHE_PATH)) {
fs.rmSync(DEFAULT_CACHE_PATH, { recursive: true });
}
// Copy the backed-up cache to the real cache
fse.copySync(ALT_CACHE_PATH, DEFAULT_CACHE_PATH, { recursive: true });
}
function cachedBuildWithDelta(): Timing[] {
// Save existing cache just once, we'll restore from it each time
saveCache();
// Edit a file in place
const file = path.join(
REPO_PATH,
"packages",
"crew",
"important-feature-0",
"src",
"lib",
"important-component-0",
"important-component-0.tsx"
);
const contents = fs.readFileSync(file).toString("utf-8");
// make a small edit
const updated = contents.replace("-0!", "-0!!");
fs.writeFileSync(file, updated);
const timings: Timing[] = [];
for (let i = 0; i < REPETITIONS; i++) {
// Make sure we're starting with the cache from before we make the source code edit
restoreSavedCache();
const start = new Date().getTime();
cp.execSync(`${TURBO_BIN} run build`, DEFAULT_EXEC_OPTS);
const end = new Date().getTime();
const timing = end - start;
timings.push(timing);
}
return timings;
}
function cachedBuildWithDependencyChange(): Timing[] {
// Save existing cache just once, we'll restore from it each time
saveCache();
// Edit a dependency
const file = path.join(REPO_PATH, "apps", "navigation", "package.json");
const contents = JSON.parse(fs.readFileSync(file).toString("utf-8"));
contents.dependencies["crew-important-feature-0"] = "*";
fs.writeFileSync(file, JSON.stringify(contents, null, 2));
const timings: Timing[] = [];
for (let i = 0; i < REPETITIONS; i++) {
// Make sure we're starting with the cache from before we made the dependency edit
restoreSavedCache();
const start = new Date().getTime();
cp.execSync(`${TURBO_BIN} run build`, DEFAULT_EXEC_OPTS);
const end = new Date().getTime();
const timing = end - start;
timings.push(timing);
}
return timings;
}
class Benchmarks {
private readonly benchmarks: Benchmark[] = [];
private readonly tbirdEvents: TBirdEvent[] = [];
constructor(
private readonly benchmarkFile: string,
private readonly tinybirdFile: string,
private readonly commitSha: string,
private readonly commitTimestamp: Date,
private readonly platform: string
) {}
run(name: string, b: () => Timing[]) {
console.log(name);
const timings = b();
const max = Math.max(...timings);
const min = Math.min(...timings);
const avg = timings.reduce((a, b) => a + b, 0) / timings.length;
this.benchmarks.push({
name,
value: avg,
unit: "ms",
range: String(max - min),
});
timings.forEach((t) => {
this.tbirdEvents.push({
commitSha: this.commitSha,
commitTimestamp: this.commitTimestamp,
platform: this.platform,
benchmark: name,
durationMs: t,
});
});
}
flush() {
console.log(JSON.stringify(this.benchmarks, null, 2));
fs.writeFileSync(
this.benchmarkFile,
JSON.stringify(this.benchmarks, null, 2)
);
const stream = ndjson.stringify();
const fd = fs.openSync(this.tinybirdFile, "w");
stream.on("data", (line) => {
fs.writeSync(fd, line);
});
this.tbirdEvents.forEach((t) => {
stream.write(t);
});
stream.end();
fs.closeSync(fd);
}
}
cp.execSync(`${TURBO_BIN} --version`, { stdio: "inherit" });
function getCommitDetails(): { commitSha: string; commitTimestamp: Date } {
const envSha = process.env["GITHUB_SHA"];
if (envSha === undefined) {
return {
commitSha: "unknown sha",
commitTimestamp: new Date(),
};
}
const buf = cp.execSync(`git show -s --format=%ci ${envSha}`);
const dateString = String(buf).trim();
const commitTimestamp = new Date(dateString);
return {
commitSha: envSha,
commitTimestamp,
};
}
const { commitSha, commitTimestamp } = getCommitDetails();
const platform = process.env["RUNNER_OS"] ?? "unknown";
console.log("setup");
setup();
const benchmark = new Benchmarks(
"benchmarks.json",
"tinybird.ndjson",
commitSha,
commitTimestamp,
platform
);
benchmark.run("Clean Build", cleanBuild);
benchmark.run("Cached Build - No Change", cachedBuild);
benchmark.run("Cached Build - Code Change", cachedBuildWithDelta);
benchmark.run(
"Cached Build - Dependency Change",
cachedBuildWithDependencyChange
);
benchmark.flush();
|