print number of completed runs and ETA in gamble.ts (#20920)

This commit is contained in:
190n
2025-07-10 00:11:42 -07:00
committed by GitHub
parent 55a9cccac0
commit 0d8175d4b9

View File

@@ -11,6 +11,23 @@ let numTimedOut = 0;
const signals = new Map<string, number>();
const codes = new Map<number, number>();
let numOk = 0;
const width = attempts.toString().length;
const pad = (num: number): string => num.toString().padStart(width, " ");
const green = (text: string) => console.log(`\x1b[32m${text}\x1b[0m`);
const red = (text: string) => console.log(`\x1b[31m${text}\x1b[0m`);
const formatTime = (ms: number): string => {
if (ms < 0) ms = 0;
const totalSeconds = Math.floor(ms / 1000);
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = totalSeconds % 60;
const padNumber = (n: number) => n.toString().padStart(2, "0");
return `${padNumber(hours)}:${padNumber(minutes)}:${padNumber(seconds)}`;
};
const start = Date.now();
let totalTimeEstimate = -1;
for (let i = 0; i < attempts; i++) {
const proc = Bun.spawn({
@@ -24,31 +41,42 @@ for (let i = 0; i < attempts; i++) {
const errors = await new Response(proc.stderr).text();
const { signalCode: signal, exitCode } = proc;
let description: string;
if (signal === "SIGTERM") {
// sent for timeouts
numTimedOut += 1;
description = "timeout";
} else if (signal) {
const newCount = 1 + (signals.get(signal) ?? 0);
signals.set(signal, newCount);
description = signal;
} else if (exitCode !== 0) {
// if null there should have been a signal
assert(exitCode !== null);
const newCount = 1 + (codes.get(exitCode) ?? 0);
codes.set(exitCode, newCount);
description = `code ${exitCode}`;
} else {
description = "ok";
numOk += 1;
}
if (exitCode !== 0) console.log(errors);
process.stdout.write(exitCode === 0 ? "." : "!");
if (exitCode !== 0) {
red(" " + description);
console.log(errors);
}
const now = Date.now();
const currentTotalTimeEstimate = (now - start) / ((i + 1) / attempts);
if (totalTimeEstimate < 0) {
totalTimeEstimate = currentTotalTimeEstimate;
} else {
totalTimeEstimate = 0.8 * totalTimeEstimate + 0.2 * currentTotalTimeEstimate;
}
const remaining = totalTimeEstimate - (now - start);
process.stdout.write(`\r\x1b[2K${pad(i + 1)}/${attempts} completed, ${formatTime(remaining)} remaining`);
}
process.stdout.write("\n");
const width = attempts.toString().length;
const pad = (num: number): string => num.toString().padStart(width, " ");
const green = (text: string) => console.log(`\x1b[32m${text}\x1b[0m`);
const red = (text: string) => console.log(`\x1b[31m${text}\x1b[0m`);
green(`${pad(numOk)}/${attempts} OK`);
if (numTimedOut > 0) {
red(`${pad(numTimedOut)}/${attempts} timeout`);