Skip to content

Commit 90e5150

Browse files
committed
refactor
1 parent 9c3bf1b commit 90e5150

16 files changed

Lines changed: 841 additions & 512 deletions
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { SummaryReport } from "./reliability";
2-
import { analyzeSignificantChanges } from "./reliability";
1+
import type { SummaryReport } from "./reliability/reliability";
2+
import { analyzeSignificantChanges } from "./summary/summary";
33
import {
44
generateMarkdownReport,
55
printSignificantChanges,
@@ -13,8 +13,5 @@ export const analyzeResults = (summary: SummaryReport) => {
1313
const markdownReport = generateMarkdownReport(changeReport);
1414
saveMarkdownReport(markdownReport);
1515

16-
return {
17-
changeReport,
18-
markdownReport,
19-
};
16+
return changeReport;
2017
};

packages/apollo-forest-run-benchmarks/src/benchmark-worker.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.

packages/apollo-forest-run-benchmarks/src/config.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
1-
import type { ForestRunAdditionalConfig } from "@graphitation/apollo-forest-run";
2-
3-
interface CacheConfigTemplate extends ForestRunAdditionalConfig {
4-
name: string;
5-
description: string;
6-
options: ForestRunAdditionalConfig;
7-
}
8-
9-
export type CacheConfig = (typeof CONFIG.cacheConfigurations)[number];
10-
export type TestConfig = typeof CONFIG;
11-
export interface ResultIdentifier {
12-
cacheConfig: CacheConfig["name"];
13-
cacheFactory: (typeof CACHE_FACTORIES)[number]["name"];
14-
}
1+
import type { CacheConfiguration } from "./types";
152

163
export const CONFIG = {
174
cacheConfigurations: [
@@ -23,16 +10,16 @@ export const CONFIG = {
2310
{
2411
name: "Telemetry enabled",
2512
description: "Enable telemetry for cache operations",
26-
options: { logStaleOperations: true, logUpdateStats: true },
13+
options: { logUpdateStats: true, logStaleOperations: true },
2714
},
28-
] satisfies CacheConfigTemplate[],
15+
] as const satisfies CacheConfiguration[],
2916
observerCounts: [0, 50],
3017
targetConfidencePercent: 99.9,
3118
minSamples: 400,
32-
minExecutionTime: 200, //ms
19+
minExecutionTime: 250, //ms
3320
warmupSamples: 50,
3421
batchSize: 100,
35-
reliability: { maxAttempts: 12, minAttempts: 3 },
22+
reliability: { maxAttempts: 6, minAttempts: 3 },
3623
significantChanges: { threshold: 0.05 },
3724
} as const;
3825

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,105 @@
1-
import type { CacheConfig, ResultIdentifier } from "./config";
1+
import type { CacheConfig, WorkerResult, SuiteRawResult } from "./types";
22

3-
import { CACHE_FACTORIES } from "./config";
43
import fs from "fs";
5-
import { isResultReliable, groupResults, getSummary } from "./reliability";
4+
import path from "path";
5+
import { CACHE_FACTORIES } from "./config";
6+
import { getSummary } from "./reliability/reliability";
67
import { log } from "./utils/logger";
78
import { analyzeResults } from "./analyze-results";
89
import { CONFIG } from "./config";
9-
import { scenarios } from "./scenarios";
1010
import { spawn } from "child_process";
11-
import path from "path";
11+
import { mergeResults } from "./utils/merge";
12+
import { isReliable } from "./utils/reliability";
1213

13-
export interface Result extends ResultIdentifier {
14-
scenario: `${(typeof scenarios)[number]["name"]}_${number}`;
15-
samples: number[];
16-
operationName: string;
17-
}
18-
19-
interface BenchmarkJob {
14+
interface BaseSuite {
2015
cacheFactory: (typeof CACHE_FACTORIES)[number];
2116
cacheConfig: CacheConfig;
2217
}
2318

24-
const benchmarkJobs: BenchmarkJob[] = [];
19+
const BASE_SUITES: BaseSuite[] = [];
20+
2521
for (const cacheFactory of CACHE_FACTORIES) {
2622
for (const cacheConfig of CONFIG.cacheConfigurations) {
27-
benchmarkJobs.push({ cacheFactory, cacheConfig });
23+
BASE_SUITES.push({ cacheFactory, cacheConfig });
2824
}
2925
}
3026

31-
function runBenchmarkInIsolatedProcess(job: BenchmarkJob): Promise<Result[]> {
27+
const spawnProcess = (job: BaseSuite): Promise<WorkerResult> => {
3228
return new Promise((resolve) => {
33-
const workerScript = path.join(
34-
__dirname,
35-
"..",
36-
"lib",
37-
"benchmark-worker.js",
38-
);
39-
const child = spawn(process.execPath, [workerScript, JSON.stringify(job)], {
40-
env: {
41-
...process.env,
29+
const workerScript = path.join(__dirname, "..", "lib", "suite-worker.js");
30+
const child = spawn(
31+
process.execPath,
32+
[
33+
"--max-old-space-size=1000",
34+
"--max-semi-space-size=512",
35+
"--noconcurrent_sweeping",
36+
workerScript,
37+
JSON.stringify(job),
38+
],
39+
{
40+
env: {
41+
...process.env,
42+
},
43+
stdio: ["pipe", "pipe"],
4244
},
43-
stdio: ["pipe", "pipe", "pipe"],
44-
});
45+
);
4546

4647
let stdout = "";
4748

4849
child.stdout.on("data", (data) => {
4950
stdout += data.toString();
5051
});
5152

52-
child.stderr.on("data", (data) => {
53-
console.error(`Worker stderr: ${data}`);
54-
});
55-
5653
child.on("close", () => {
5754
const results = JSON.parse(stdout.trim());
5855
resolve(results);
5956
});
6057
});
61-
}
58+
};
6259

63-
async function runBenchmarkSuite(): Promise<Result[]> {
64-
const allResults: Result[] = [];
60+
const runBaseSuites = async (): Promise<WorkerResult[]> => {
61+
const allResults: WorkerResult[] = [];
6562

66-
for (const job of benchmarkJobs) {
63+
for (const suite of BASE_SUITES) {
6764
console.log(
68-
`\n=== Running benchmarks for ${job.cacheFactory.name} with ${job.cacheConfig.name} in isolated process ===`,
65+
`\n=== Running benchmarks for ${suite.cacheFactory.name} with ${suite.cacheConfig.name} in isolated process ===`,
6966
);
7067

71-
const jobResults = await runBenchmarkInIsolatedProcess(job);
72-
allResults.push(...jobResults);
68+
const result = await spawnProcess(suite);
69+
allResults.push(result);
7370
console.log(
74-
`Completed ${job.cacheFactory.name}/${job.cacheConfig.name} with ${jobResults.length} results`,
71+
`Completed ${suite.cacheFactory.name}/${suite.cacheConfig.name} with ${result.results.length} results`,
7572
);
7673
}
7774

7875
return allResults;
79-
}
80-
81-
export interface BenchmarkResult {
82-
[scenarioName: string]: Result[];
83-
}
76+
};
8477

85-
async function runBenchmarks(): Promise<void> {
78+
const runBenchmarks = async (): Promise<void> => {
8679
const { maxAttempts } = CONFIG.reliability;
87-
let prevBenchmarks: BenchmarkResult[] = [];
80+
let prevSuites: SuiteRawResult[] = [];
8881
log.start();
8982
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
9083
log.attempt(attempt);
9184

92-
const currentResult = await runBenchmarkSuite();
93-
const groupedResults = groupResults(currentResult);
94-
const isReliable = isResultReliable(groupedResults, prevBenchmarks);
85+
const results = await runBaseSuites();
86+
const groupedResult = mergeResults(results);
87+
const isSuiteReliable = isReliable(groupedResult, prevSuites);
9588

96-
prevBenchmarks.push(groupedResults);
89+
prevSuites.push(groupedResult);
9790

98-
if (isReliable && attempt > CONFIG.reliability.minAttempts) {
91+
if (isSuiteReliable && attempt > CONFIG.reliability.minAttempts) {
9992
break;
10093
}
10194
}
10295

103-
const summary = getSummary(prevBenchmarks);
104-
analyzeResults(summary);
96+
const summary = getSummary(prevSuites);
97+
const report = analyzeResults(summary);
10598
fs.writeFileSync("benchmark-summary.json", JSON.stringify(summary, null, 2));
106-
}
99+
fs.writeFileSync(
100+
"benchmark-summary-report.json",
101+
JSON.stringify(report, null, 2),
102+
);
103+
};
107104

108105
runBenchmarks();

0 commit comments

Comments
 (0)