|
| 1 | +// @ts-check |
| 2 | +const fs = require("fs"); |
| 3 | +const git = require('simple-git/promise')('.') |
| 4 | +const readline = require('readline') |
| 5 | + |
| 6 | +/** @typedef {{ [s: string]: number}} Histogram */ |
| 7 | + |
| 8 | +async function main() { |
| 9 | + /** @type {Histogram} */ |
| 10 | + const edits = Object.create(null) |
| 11 | + /** @type {Histogram} */ |
| 12 | + const perf = JSON.parse(fs.readFileSync('.parallelperf.json', 'utf8')) |
| 13 | + |
| 14 | + await collectCommits(git, "release-2.3", "master", /*author*/ undefined, files => fillMap(files, edits)) |
| 15 | + |
| 16 | + const totalTime = Object.values(perf).reduce((n,m) => n + m, 0) |
| 17 | + const untouched = Object.values(perf).length - Object.values(edits).length |
| 18 | + const totalEdits = Object.values(edits).reduce((n,m) => n + m, 0) + untouched + Object.values(edits).length |
| 19 | + |
| 20 | + let i = 0 |
| 21 | + /** @type {{ name: string, time: number, edits: number, cost: number }[]} */ |
| 22 | + let data = [] |
| 23 | + for (const k in perf) { |
| 24 | + const otherk = k.replace(/tsrunner-[a-z-]+?:\/\//, '') |
| 25 | + const percentTime = perf[k] / totalTime |
| 26 | + const percentHits = (1 + (edits[otherk] || 0)) / totalEdits |
| 27 | + const cost = 5 + Math.log(percentTime / percentHits) |
| 28 | + // TODO: Write counts instead of numbers to make JSON file smaller |
| 29 | + data.push({ name: otherk, time: perf[k], edits: 1 + (edits[otherk] || 0), cost}) |
| 30 | + if (edits[otherk]) |
| 31 | + i++ |
| 32 | + } |
| 33 | + const output = { |
| 34 | + totalTime, |
| 35 | + totalEdits, |
| 36 | + data: data.sort((x,y) => y.cost - x.cost).map(x => ({ ...x, cost: x.cost.toFixed(2) })) |
| 37 | + } |
| 38 | + |
| 39 | + fs.writeFileSync('.test-cost.json', JSON.stringify(output), 'utf8') |
| 40 | +} |
| 41 | + |
| 42 | +main().catch(e => { |
| 43 | + console.log(e); |
| 44 | + process.exit(1); |
| 45 | +}) |
| 46 | + |
| 47 | +/** |
| 48 | + * @param {string[]} files |
| 49 | + * @param {Histogram} histogram |
| 50 | + */ |
| 51 | +function fillMap(files, histogram) { |
| 52 | + // keep edits to test cases (but not /users), and not file moves |
| 53 | + const tests = files.filter(f => f.startsWith('tests/cases/') && !f.startsWith('tests/cases/user') && !/=>/.test(f)) |
| 54 | + for (const test of tests) { |
| 55 | + histogram[test] = (histogram[test] || 0) + 1 |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * @param {string} s |
| 61 | + */ |
| 62 | +function isSquashMergeMessage(s) { |
| 63 | + return /\(#[0-9]+\)$/.test(s) |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * @param {string} s |
| 68 | + */ |
| 69 | +function isMergeCommit(s) { |
| 70 | + return /Merge pull request #[0-9]+/.test(s) |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * @param {string} s |
| 75 | + */ |
| 76 | +function parseFiles(s) { |
| 77 | + const lines = s.split('\n') |
| 78 | + // Note that slice(2) only works for merge commits, which have an empty newline after the title |
| 79 | + return lines.slice(2, lines.length - 2).map(line => line.split("|")[0].trim()) |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * @param {import('simple-git/promise').SimpleGit} git |
| 84 | + * @param {string} from |
| 85 | + * @param {string} to |
| 86 | + * @param {string | undefined} author - only include commits from this author |
| 87 | + * @param {(files: string[]) => void} update |
| 88 | + */ |
| 89 | + async function collectCommits(git, from, to, author, update) { |
| 90 | + let i = 0 |
| 91 | + for (const commit of (await git.log({ from, to })).all) { |
| 92 | + i++ |
| 93 | + if ((!author || commit.author_name === author) && isMergeCommit(commit.message) || isSquashMergeMessage(commit.message)) { |
| 94 | + readline.clearLine(process.stdout, /*left*/ -1) |
| 95 | + readline.cursorTo(process.stdout, 0) |
| 96 | + process.stdout.write(i + ": " + commit.date) |
| 97 | + const files = parseFiles(await git.show([commit.hash, "--stat=1000,960,40", "--pretty=oneline"])) |
| 98 | + update(files) |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | + |
0 commit comments