Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions resources/benchmark/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,25 @@ import { git, localRepoPath, makeTmpDir, npm } from '../utils.js';
import { LOCAL } from './config.js';
import type { BenchmarkProject } from './types.js';

// Build a benchmark-friendly environment for each revision.
// Build a benchmark-friendly install for each revision.
export function prepareBenchmarkProjects(
revisionList: ReadonlyArray<string>,
): Array<BenchmarkProject> {
const { tmpDirPath } = makeTmpDir('graphql-js-benchmark');
const { tmpDirPath: benchmarkCachePath } = makeTmpDir(
'graphql-js-benchmark-cache',
false,
);

return revisionList.map((revision) => {
// Resolve refs like "main" to full SHAs so equivalent revisions reuse setup.
const hash = revision === LOCAL ? LOCAL : git().revParse(revision);
const projectPath = tmpDirPath('setup', hash);
if (fs.existsSync(projectPath)) {
return { revision, projectPath };
}

console.log(`\u{1F373} Preparing ${revision}...`);
const projectPath = tmpDirPath('setup', revision);
fs.rmSync(projectPath, { recursive: true, force: true });
fs.mkdirSync(projectPath, { recursive: true });

fs.cpSync(localRepoPath('benchmark'), path.join(projectPath, 'benchmark'), {
Expand All @@ -29,7 +38,7 @@ export function prepareBenchmarkProjects(
private: true,
type: 'module',
dependencies: {
graphql: prepareNPMPackage(revision),
graphql: prepareNPMPackage(hash),
},
},
null,
Expand All @@ -41,18 +50,15 @@ export function prepareBenchmarkProjects(
return { revision, projectPath };
});

function prepareNPMPackage(revision: string): string {
if (revision === LOCAL) {
function prepareNPMPackage(hash: string): string {
if (hash === LOCAL) {
const repoDir = localRepoPath();
const archivePath = tmpDirPath('graphql-local.tgz');
fs.renameSync(buildNPMArchive(repoDir), archivePath);
return archivePath;
}

// Returns the complete git hash for a given git revision reference.
const hash = git().revParse(revision);

const archivePath = tmpDirPath(`graphql-${hash}.tgz`);
const archivePath = benchmarkCachePath(`graphql-${hash}.tgz`);
if (fs.existsSync(archivePath)) {
return archivePath;
}
Expand Down
11 changes: 8 additions & 3 deletions resources/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ interface MakeTmpDirReturn {
tmpDirPath: (...paths: ReadonlyArray<string>) => string;
}

export function makeTmpDir(name: string): MakeTmpDirReturn {
export function makeTmpDir(
name: string,
clear: boolean = true,
): MakeTmpDirReturn {
const tmpDir = path.join(os.tmpdir(), name);
fs.rmSync(tmpDir, { recursive: true, force: true });
fs.mkdirSync(tmpDir);
if (clear) {
fs.rmSync(tmpDir, { recursive: true, force: true });
}
fs.mkdirSync(tmpDir, { recursive: true });

return {
tmpDirPath: (...paths) => path.join(tmpDir, ...paths),
Expand Down
Loading