UNPKG

@dapplion/benchmark

Version:

Ensures that new code does not introduce performance regressions with CI. Tracks:

67 lines (66 loc) 2.02 kB
import fs from "node:fs"; import * as cache from "@actions/cache"; import { LocalHistoryProvider } from "./local.js"; import { HistoryProviderType } from "./provider.js"; /** * Persist results in CSV, one benchmark result per file * * ``` * /$dirpath/52b8122daa9b7a3d0ea0ecfc1ff9eda79a201eb8.csv * ``` * * ```csv * id,averageNs,runsDone,totalMs * sum array with raw for loop,1348118,371,501 * sum array with reduce,16896469,128,2163 * ``` */ export function getGaCacheHistoryProvider(cacheKey) { const tmpDir = fs.mkdtempSync("ga-cache-download"); return new GaCacheHistoryProvider(tmpDir, cacheKey); } class GaCacheHistoryProvider extends LocalHistoryProvider { tmpDir; cacheKey; type = HistoryProviderType.GaCache; initializePromise = null; constructor(tmpDir, cacheKey) { super(tmpDir); this.tmpDir = tmpDir; this.cacheKey = cacheKey; } providerInfo() { return `GaCacheHistoryProvider: cacheKey ${this.cacheKey}`; } async readLatestInBranch(branch) { await this.initialize(); return super.readLatestInBranch(branch); } async writeLatestInBranch(branch, benchmark) { await super.writeLatestInBranch(branch, benchmark); await this.persist(); } async readHistory() { await this.initialize(); return super.readHistory(); } async readHistoryCommit(commitSha) { await this.initialize(); return super.readHistoryCommit(commitSha); } async writeToHistory(benchmark) { await super.writeToHistory(benchmark); await this.persist(); } async initialize() { // eslint-disable-next-line no-console console.log("ENV", process.env); if (this.initializePromise === null) { this.initializePromise = cache.restoreCache([this.tmpDir], this.cacheKey); } return this.initializePromise; } async persist() { await cache.saveCache([this.tmpDir], this.cacheKey); } }