feat(comparecache): add -sample flag to pick random extra packages

Usage: go run ./cmd/comparecache -sample 8 -diffs
Picks 8 random packages beyond any explicitly named ones, logs which
ones were sampled for reproducibility.
This commit is contained in:
AJ ONeal
2026-03-11 11:20:43 -06:00
parent 5606773945
commit 2b488693b0

View File

@@ -18,6 +18,7 @@ import (
"flag"
"fmt"
"log"
"math/rand/v2"
"os"
"path/filepath"
"slices"
@@ -62,6 +63,7 @@ func main() {
diffsOnly := flag.Bool("diffs", false, "only show packages with asset differences (skip matches)")
latest := flag.Bool("latest", false, "only compare latest version in each cache")
windowed := flag.Bool("windowed", false, "limit Go versions to the Node.js version range (2nd to 2nd-to-last)")
sample := flag.Int("sample", 0, "pick N extra random packages beyond any named ones")
flag.Parse()
filterPkgs := flag.Args()
@@ -83,18 +85,39 @@ func main() {
// Discover all packages across both caches.
discoverStart := time.Now()
allPkgs := discoverPackages(livePath, goPath)
if len(filterPkgs) > 0 {
if len(filterPkgs) > 0 || *sample > 0 {
nameSet := make(map[string]bool, len(filterPkgs))
for _, n := range filterPkgs {
nameSet[n] = true
}
var filtered []string
var selected []string
var pool []string
for _, p := range allPkgs {
if nameSet[p] {
filtered = append(filtered, p)
selected = append(selected, p)
} else {
pool = append(pool, p)
}
}
allPkgs = filtered
// Pick random extras from the remaining pool.
if *sample > 0 && len(pool) > 0 {
rand.Shuffle(len(pool), func(i, j int) {
pool[i], pool[j] = pool[j], pool[i]
})
n := *sample
if n > len(pool) {
n = len(pool)
}
extras := pool[:n]
sort.Strings(extras)
selected = append(selected, extras...)
log.Printf("sampled %d extra: %s", n, strings.Join(extras, ", "))
}
sort.Strings(selected)
allPkgs = selected
}
log.Printf("discovered %d packages in %s", len(allPkgs), time.Since(discoverStart))