Skip to content

Commit 4dddbc9

Browse files
authored
benchmark: cache revision archives (#4688)
1 parent 88c4543 commit 4dddbc9

2 files changed

Lines changed: 24 additions & 13 deletions

File tree

resources/benchmark/projects.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,25 @@ import { git, localRepoPath, makeTmpDir, npm } from '../utils.js';
66
import { LOCAL } from './config.js';
77
import type { BenchmarkProject } from './types.js';
88

9-
// Build a benchmark-friendly environment for each revision.
9+
// Build a benchmark-friendly install for each revision.
1010
export function prepareBenchmarkProjects(
1111
revisionList: ReadonlyArray<string>,
1212
): Array<BenchmarkProject> {
1313
const { tmpDirPath } = makeTmpDir('graphql-js-benchmark');
14+
const { tmpDirPath: benchmarkCachePath } = makeTmpDir(
15+
'graphql-js-benchmark-cache',
16+
false,
17+
);
1418

1519
return revisionList.map((revision) => {
20+
// Resolve refs like "main" to full SHAs so equivalent revisions reuse setup.
21+
const hash = revision === LOCAL ? LOCAL : git().revParse(revision);
22+
const projectPath = tmpDirPath('setup', hash);
23+
if (fs.existsSync(projectPath)) {
24+
return { revision, projectPath };
25+
}
26+
1627
console.log(`\u{1F373} Preparing ${revision}...`);
17-
const projectPath = tmpDirPath('setup', revision);
18-
fs.rmSync(projectPath, { recursive: true, force: true });
1928
fs.mkdirSync(projectPath, { recursive: true });
2029

2130
fs.cpSync(localRepoPath('benchmark'), path.join(projectPath, 'benchmark'), {
@@ -29,7 +38,7 @@ export function prepareBenchmarkProjects(
2938
private: true,
3039
type: 'module',
3140
dependencies: {
32-
graphql: prepareNPMPackage(revision),
41+
graphql: prepareNPMPackage(hash),
3342
},
3443
},
3544
null,
@@ -41,18 +50,15 @@ export function prepareBenchmarkProjects(
4150
return { revision, projectPath };
4251
});
4352

44-
function prepareNPMPackage(revision: string): string {
45-
if (revision === LOCAL) {
53+
function prepareNPMPackage(hash: string): string {
54+
if (hash === LOCAL) {
4655
const repoDir = localRepoPath();
4756
const archivePath = tmpDirPath('graphql-local.tgz');
4857
fs.renameSync(buildNPMArchive(repoDir), archivePath);
4958
return archivePath;
5059
}
5160

52-
// Returns the complete git hash for a given git revision reference.
53-
const hash = git().revParse(revision);
54-
55-
const archivePath = tmpDirPath(`graphql-${hash}.tgz`);
61+
const archivePath = benchmarkCachePath(`graphql-${hash}.tgz`);
5662
if (fs.existsSync(archivePath)) {
5763
return archivePath;
5864
}

resources/utils.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ interface MakeTmpDirReturn {
1818
tmpDirPath: (...paths: ReadonlyArray<string>) => string;
1919
}
2020

21-
export function makeTmpDir(name: string): MakeTmpDirReturn {
21+
export function makeTmpDir(
22+
name: string,
23+
clear: boolean = true,
24+
): MakeTmpDirReturn {
2225
const tmpDir = path.join(os.tmpdir(), name);
23-
fs.rmSync(tmpDir, { recursive: true, force: true });
24-
fs.mkdirSync(tmpDir);
26+
if (clear) {
27+
fs.rmSync(tmpDir, { recursive: true, force: true });
28+
}
29+
fs.mkdirSync(tmpDir, { recursive: true });
2530

2631
return {
2732
tmpDirPath: (...paths) => path.join(tmpDir, ...paths),

0 commit comments

Comments
 (0)