UNPKG

@dapplion/benchmark

Version:

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

108 lines (107 loc) 3.7 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LocalHistoryProvider = void 0; const node_fs_1 = __importDefault(require("node:fs")); const node_path_1 = __importDefault(require("node:path")); const provider_js_1 = require("./provider.js"); const file_js_1 = require("../utils/file.js"); const extension = ".csv"; const historyDir = "history"; const latestDir = "latest"; /** * Persist results in CSV, one benchmark result per file * * ``` * /$dirpath/ * history/ * 52b8122daa9b7a3d0ea0ecfc1ff9eda79a201eb8.csv * c0203c527c9a2269f1f289fad2c8e25afdc2c169.csv * latest/ * main.csv * dev.csv * ``` * * ```csv * id,averageNs,runsDone,totalMs * sum array with raw for loop,1348118,371,501 * sum array with reduce,16896469,128,2163 * ``` */ class LocalHistoryProvider { dirpath; type = provider_js_1.HistoryProviderType.Local; constructor(dirpath) { this.dirpath = dirpath; } providerInfo() { return `LocalHistoryProvider, dirpath ${this.dirpath}`; } async readLatestInBranch(branch) { const filepath = this.getLatestInBranchFilepath(branch); return this.readBenchFileIfExists(filepath); } async writeLatestInBranch(branch, benchmark) { const filepath = this.getLatestInBranchFilepath(branch); this.writeBenchFile(filepath, benchmark); } async readHistory() { const historyDirpath = this.getHistoryDirpath(); let files; try { files = node_fs_1.default.readdirSync(historyDirpath); } catch (e) { if (e.code === "ENOENT") return []; else throw e; } return files.map((file) => this.readBenchFile(node_path_1.default.join(historyDirpath, file))); } async readHistoryCommit(commitSha) { const filepath = this.getHistoryCommitPath(commitSha); return this.readBenchFileIfExists(filepath); } async writeToHistory(benchmark) { const filepath = this.getHistoryCommitPath(benchmark.commitSha); this.writeBenchFile(filepath, benchmark); } readBenchFileIfExists(filepath) { try { return this.readBenchFile(filepath); } catch (e) { if (e.code === "ENOENT") return null; else throw e; } } /** Read result from CSV + metadata as Embedded Metadata */ readBenchFile(filepath) { const str = node_fs_1.default.readFileSync(filepath, "utf8"); const { data, metadata } = (0, file_js_1.fromCsv)(str); const csvMeta = metadata; return { commitSha: csvMeta.commit, results: data }; } /** Write result to CSV + metadata as Embedded Metadata */ writeBenchFile(filepath, benchmark) { const csvMeta = { commit: benchmark.commitSha }; const str = (0, file_js_1.toCsv)(benchmark.results, csvMeta); node_fs_1.default.mkdirSync(node_path_1.default.dirname(filepath), { recursive: true }); node_fs_1.default.writeFileSync(filepath, str); } getLatestInBranchFilepath(branch) { return node_path_1.default.join(this.dirpath, latestDir, branch) + extension; } getHistoryCommitPath(commitSha) { return node_path_1.default.join(this.getHistoryDirpath(), commitSha) + extension; } getHistoryDirpath() { return node_path_1.default.join(this.dirpath, historyDir); } } exports.LocalHistoryProvider = LocalHistoryProvider;