Skip to content

Commit

Permalink
make extractor more declarative
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Dec 17, 2024
1 parent fea5fea commit 4234f76
Showing 1 changed file with 26 additions and 29 deletions.
55 changes: 26 additions & 29 deletions scripts/analyze_benchmarks/extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ export type BenchmarkEntry = {
timestamp: { __bigint__: string };
};

type VersionBenchmarks = {
version: string;
benchmarks: BenchmarkEntry[];
};

type CanisterBenchmark = {
current?: {
version: string;
benchmarks: BenchmarkEntry[];
};
previous: {
version: string;
benchmarks: BenchmarkEntry[];
};
current: VersionBenchmarks;
previous: VersionBenchmarks;
};

type BenchmarksJson = {
Expand All @@ -29,10 +28,9 @@ type BenchmarksJson = {
export async function extractBenchmarksEntriesFromFiles(
files: string[]
): Promise<Record<string, BenchmarkEntry[]>> {
const versionEntriesArrays = await Promise.all(
files.map(extractBenchmarkEntries)
);
const versionEntries = versionEntriesArrays.flat();
const versionEntries = (
await Promise.all(files.map(extractBenchmarkEntries))
).flat();

return groupEntriesByVersion(versionEntries);
}
Expand All @@ -48,28 +46,27 @@ async function extractBenchmarkEntries(
const data: BenchmarksJson = JSON.parse(await readFile(file, 'utf-8'));

return Object.values(data).flatMap((canisterData) => {
const currentEntries = canisterData.current
? canisterData.current.benchmarks.map(
(benchmark) =>
[canisterData.current!.version, benchmark] as [
string,
BenchmarkEntry
]
)
: [];

const previousEntries = canisterData.previous.benchmarks.map(
(benchmark) =>
[canisterData.previous.version, benchmark] as [
string,
BenchmarkEntry
]
);
const currentEntries = getBenchmarkEntries(canisterData.current);
const previousEntries = getBenchmarkEntries(canisterData.previous);

return [...currentEntries, ...previousEntries];
});
}

/**
* Extracts benchmark entries with their versions from a VersionBenchmarks object
* @param versionBenchmarks Object containing version and its benchmark entries
* @returns Array of tuples containing version and benchmark entry pairs
*/
function getBenchmarkEntries(
versionBenchmarks: VersionBenchmarks
): [string, BenchmarkEntry][] {
return versionBenchmarks.benchmarks.map((benchmark) => [
versionBenchmarks.version,
benchmark
]);
}

/**
* Groups benchmark entries by their version
* @param entries Array of version and benchmark entry pairs
Expand Down

0 comments on commit 4234f76

Please sign in to comment.