|
| 1 | +import { appendFileSync, readFileSync } from "node:fs"; |
| 2 | +import { execFileSync } from "node:child_process"; |
| 3 | + |
| 4 | +const rustShard = ({ |
| 5 | + label, |
| 6 | + cacheKey, |
| 7 | + packageName, |
| 8 | + bench, |
| 9 | + features = "codspeed", |
| 10 | + fixtures = false, |
| 11 | +}) => ({ |
| 12 | + kind: "rust", |
| 13 | + mode: "simulation", |
| 14 | + label, |
| 15 | + cache_key: cacheKey, |
| 16 | + package: packageName, |
| 17 | + bench, |
| 18 | + features, |
| 19 | + fixtures, |
| 20 | +}); |
| 21 | + |
| 22 | +const jsShard = ({ label, cacheKey, command, fixtures = true }) => ({ |
| 23 | + kind: "node", |
| 24 | + mode: "walltime", |
| 25 | + label, |
| 26 | + cache_key: cacheKey, |
| 27 | + command, |
| 28 | + fixtures, |
| 29 | +}); |
| 30 | + |
| 31 | +const SHARDS = { |
| 32 | + codec: rustShard({ |
| 33 | + label: "codec vlq", |
| 34 | + cacheKey: "codec-vlq", |
| 35 | + packageName: "srcmap-codec", |
| 36 | + bench: "vlq", |
| 37 | + }), |
| 38 | + codecParallel: rustShard({ |
| 39 | + label: "codec vlq parallel", |
| 40 | + cacheKey: "codec-vlq-parallel", |
| 41 | + packageName: "srcmap-codec", |
| 42 | + bench: "vlq", |
| 43 | + features: "codspeed,parallel", |
| 44 | + }), |
| 45 | + sourcemap: rustShard({ |
| 46 | + label: "sourcemap parse", |
| 47 | + cacheKey: "sourcemap-parse", |
| 48 | + packageName: "srcmap-sourcemap", |
| 49 | + bench: "parse", |
| 50 | + fixtures: true, |
| 51 | + }), |
| 52 | + generator: rustShard({ |
| 53 | + label: "generator", |
| 54 | + cacheKey: "generator", |
| 55 | + packageName: "srcmap-generator", |
| 56 | + bench: "generate", |
| 57 | + }), |
| 58 | + generatorParallel: rustShard({ |
| 59 | + label: "generator parallel", |
| 60 | + cacheKey: "generator-parallel", |
| 61 | + packageName: "srcmap-generator", |
| 62 | + bench: "generate", |
| 63 | + features: "codspeed,parallel", |
| 64 | + }), |
| 65 | + remapping: rustShard({ |
| 66 | + label: "remapping", |
| 67 | + cacheKey: "remapping", |
| 68 | + packageName: "srcmap-remapping", |
| 69 | + bench: "remap", |
| 70 | + fixtures: true, |
| 71 | + }), |
| 72 | + packages: jsShard({ |
| 73 | + label: "package runtime", |
| 74 | + cacheKey: "package-runtime", |
| 75 | + command: "corepack pnpm --dir benchmarks run bench:codspeed:packages", |
| 76 | + }), |
| 77 | +}; |
| 78 | + |
| 79 | +const allShards = () => [ |
| 80 | + SHARDS.codec, |
| 81 | + SHARDS.codecParallel, |
| 82 | + SHARDS.sourcemap, |
| 83 | + SHARDS.generator, |
| 84 | + SHARDS.generatorParallel, |
| 85 | + SHARDS.remapping, |
| 86 | + SHARDS.packages, |
| 87 | +]; |
| 88 | + |
| 89 | +const commandOutput = (command, args) => { |
| 90 | + try { |
| 91 | + return execFileSync(command, args, { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }); |
| 92 | + } catch { |
| 93 | + return ""; |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +const changedFilesForEvent = () => { |
| 98 | + const eventName = process.env.GITHUB_EVENT_NAME ?? ""; |
| 99 | + if (eventName === "workflow_dispatch") return null; |
| 100 | + |
| 101 | + if (eventName === "pull_request") { |
| 102 | + const baseRef = process.env.GITHUB_BASE_REF; |
| 103 | + if (!baseRef) return null; |
| 104 | + commandOutput("git", ["fetch", "--no-tags", "--depth=1", "origin", baseRef]); |
| 105 | + const diff = commandOutput("git", ["diff", "--name-only", `origin/${baseRef}...HEAD`]); |
| 106 | + return diff.trim() ? diff.trim().split("\n") : []; |
| 107 | + } |
| 108 | + |
| 109 | + if (eventName === "push") { |
| 110 | + const eventPath = process.env.GITHUB_EVENT_PATH; |
| 111 | + if (!eventPath) return null; |
| 112 | + const event = JSON.parse(readFileSync(eventPath, "utf8")); |
| 113 | + const before = event.before; |
| 114 | + const after = event.after ?? process.env.GITHUB_SHA; |
| 115 | + if (!before || /^0+$/.test(before) || !after) return null; |
| 116 | + const diff = commandOutput("git", ["diff", "--name-only", `${before}..${after}`]); |
| 117 | + return diff.trim() ? diff.trim().split("\n") : []; |
| 118 | + } |
| 119 | + |
| 120 | + return null; |
| 121 | +}; |
| 122 | + |
| 123 | +const includeShard = (selected, shard) => { |
| 124 | + selected.set(shard.label, shard); |
| 125 | +}; |
| 126 | + |
| 127 | +const selectShards = (files) => { |
| 128 | + if (files === null) return allShards(); |
| 129 | + |
| 130 | + const selected = new Map(); |
| 131 | + |
| 132 | + for (const file of files) { |
| 133 | + if ( |
| 134 | + file === ".github/workflows/bench.yml" || |
| 135 | + file === ".github/scripts/generate-benchmark-matrix.mjs" || |
| 136 | + file === "Cargo.toml" || |
| 137 | + file === "Cargo.lock" || |
| 138 | + file === "package.json" || |
| 139 | + file === "pnpm-lock.yaml" || |
| 140 | + file === "pnpm-workspace.yaml" |
| 141 | + ) { |
| 142 | + return allShards(); |
| 143 | + } |
| 144 | + |
| 145 | + if (file.startsWith("crates/codec/")) { |
| 146 | + includeShard(selected, SHARDS.codec); |
| 147 | + includeShard(selected, SHARDS.codecParallel); |
| 148 | + } |
| 149 | + if (file.startsWith("crates/sourcemap/")) includeShard(selected, SHARDS.sourcemap); |
| 150 | + if (file.startsWith("crates/generator/")) { |
| 151 | + includeShard(selected, SHARDS.generator); |
| 152 | + includeShard(selected, SHARDS.generatorParallel); |
| 153 | + } |
| 154 | + if (file.startsWith("crates/remapping/")) includeShard(selected, SHARDS.remapping); |
| 155 | + if (file.startsWith("benchmarks/download-fixtures.mjs")) { |
| 156 | + includeShard(selected, SHARDS.sourcemap); |
| 157 | + includeShard(selected, SHARDS.remapping); |
| 158 | + includeShard(selected, SHARDS.packages); |
| 159 | + } |
| 160 | + if ( |
| 161 | + file.startsWith("benchmarks/") || |
| 162 | + file.startsWith("packages/") || |
| 163 | + file === "benchmarks/package.json" |
| 164 | + ) { |
| 165 | + includeShard(selected, SHARDS.packages); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + return selected.size === 0 ? allShards() : [...selected.values()]; |
| 170 | +}; |
| 171 | + |
| 172 | +const include = selectShards(changedFilesForEvent()); |
| 173 | +const json = JSON.stringify(include); |
| 174 | + |
| 175 | +if (process.env.GITHUB_OUTPUT) { |
| 176 | + appendFileSync(process.env.GITHUB_OUTPUT, `include=${json}\n`); |
| 177 | +} else { |
| 178 | + console.log(json); |
| 179 | +} |
0 commit comments