Skip to content

Commit 9cb4192

Browse files
committed
perf(ci): instrument build artifacts phases
1 parent 44027e7 commit 9cb4192

4 files changed

Lines changed: 89 additions & 12 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,6 @@ jobs:
490490
NODE_OPTIONS: --max-old-space-size=8192
491491
run: pnpm build:ci-artifacts
492492

493-
- name: Build Control UI
494-
run: pnpm ui:build
495-
496493
- name: Check Control UI i18n
497494
if: needs.preflight.outputs.run_control_ui_i18n == 'true'
498495
run: pnpm ui:i18n:check

‎scripts/build-all.mjs‎

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { spawnSync } from "node:child_process";
44
import { createHash } from "node:crypto";
55
import fs from "node:fs";
66
import path from "node:path";
7+
import { performance } from "node:perf_hooks";
78
import { pathToFileURL } from "node:url";
89
import { resolvePnpmRunner } from "./pnpm-runner.mjs";
910

@@ -398,6 +399,32 @@ export function restoreBuildAllStepCacheOutputs(cacheState, params = {}) {
398399
return true;
399400
}
400401

402+
export function formatBuildAllDuration(durationMs) {
403+
const clampedMs = Math.max(0, durationMs);
404+
if (clampedMs < 1000) {
405+
return `${Math.round(clampedMs)}ms`;
406+
}
407+
if (clampedMs < 10000) {
408+
return `${(clampedMs / 1000).toFixed(2)}s`;
409+
}
410+
return `${(clampedMs / 1000).toFixed(1)}s`;
411+
}
412+
413+
export function formatBuildAllTimingSummary(timings) {
414+
if (timings.length === 0) {
415+
return "[build-all] phase timings: no phases ran";
416+
}
417+
const totalMs = timings.reduce((sum, timing) => sum + timing.durationMs, 0);
418+
const phases = timings
419+
.toSorted((left, right) => right.durationMs - left.durationMs)
420+
.map((timing) => {
421+
const status = timing.status === "ran" ? "" : ` (${timing.status})`;
422+
return `${timing.label}${status} ${formatBuildAllDuration(timing.durationMs)}`;
423+
})
424+
.join("; ");
425+
return `[build-all] phase timings: total ${formatBuildAllDuration(totalMs)}; slowest ${phases}`;
426+
}
427+
401428
function isMainModule() {
402429
const argv1 = process.argv[1];
403430
if (!argv1) {
@@ -408,23 +435,43 @@ function isMainModule() {
408435

409436
if (isMainModule()) {
410437
const profile = process.argv[2] ?? "full";
438+
const timings = [];
439+
let exitCode = 0;
411440
for (const step of resolveBuildAllSteps(profile)) {
441+
const startedAt = performance.now();
412442
const cacheState = resolveBuildAllStepCacheState(step);
413443
if (process.env.OPENCLAW_BUILD_CACHE !== "0" && cacheState.fresh) {
414444
restoreBuildAllStepCacheOutputs(cacheState);
415-
console.error(`[build-all] ${step.label} (cached)`);
445+
const durationMs = performance.now() - startedAt;
446+
timings.push({ label: step.label, status: "cached", durationMs });
447+
console.error(`[build-all] ${step.label} (cached) ${formatBuildAllDuration(durationMs)}`);
416448
continue;
417449
}
418450
console.error(`[build-all] ${step.label}`);
419451
const invocation = resolveBuildAllStep(step);
420452
const result = spawnSync(invocation.command, invocation.args, invocation.options);
453+
const durationMs = performance.now() - startedAt;
421454
if (typeof result.status === "number") {
422455
if (result.status !== 0) {
423-
process.exit(result.status);
456+
timings.push({ label: step.label, status: "failed", durationMs });
457+
console.error(
458+
`[build-all] ${step.label} failed after ${formatBuildAllDuration(durationMs)}`,
459+
);
460+
exitCode = result.status;
461+
break;
424462
}
425463
writeBuildAllStepCacheStamp(step, resolveBuildAllStepCacheState(step));
464+
timings.push({ label: step.label, status: "ran", durationMs });
465+
console.error(`[build-all] ${step.label} done in ${formatBuildAllDuration(durationMs)}`);
426466
continue;
427467
}
428-
process.exit(1);
468+
timings.push({ label: step.label, status: "failed", durationMs });
469+
console.error(`[build-all] ${step.label} failed after ${formatBuildAllDuration(durationMs)}`);
470+
exitCode = 1;
471+
break;
472+
}
473+
console.error(formatBuildAllTimingSummary(timings));
474+
if (exitCode !== 0) {
475+
process.exit(exitCode);
429476
}
430477
}

‎test/scripts/build-all.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { describe, expect, it } from "vitest";
55
import {
66
BUILD_ALL_PROFILES,
77
BUILD_ALL_STEPS,
8+
formatBuildAllDuration,
9+
formatBuildAllTimingSummary,
810
resolveBuildAllStepCacheState,
911
resolveBuildAllStep,
1012
resolveBuildAllSteps,
@@ -258,6 +260,26 @@ describe("resolveBuildAllSteps", () => {
258260
});
259261
});
260262

263+
describe("build-all timing output", () => {
264+
it("formats short and long phase durations compactly", () => {
265+
expect(formatBuildAllDuration(42.4)).toBe("42ms");
266+
expect(formatBuildAllDuration(1234)).toBe("1.23s");
267+
expect(formatBuildAllDuration(12345)).toBe("12.3s");
268+
});
269+
270+
it("summarizes phases slowest first with total time and status", () => {
271+
expect(
272+
formatBuildAllTimingSummary([
273+
{ label: "tsdown", status: "ran", durationMs: 99000 },
274+
{ label: "plugins:assets:copy", status: "cached", durationMs: 12 },
275+
{ label: "build:plugin-sdk:dts", status: "ran", durationMs: 34567 },
276+
]),
277+
).toBe(
278+
"[build-all] phase timings: total 133.6s; slowest tsdown 99.0s; build:plugin-sdk:dts 34.6s; plugins:assets:copy (cached) 12ms",
279+
);
280+
});
281+
});
282+
261283
describe("resolveBuildAllStepCacheState", () => {
262284
it("marks cacheable steps fresh when the input signature matches", () => {
263285
withBuildCacheFixture(({ rootDir, step }) => {

‎test/scripts/ci-workflow-guards.test.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { readFileSync } from "node:fs";
22
import { describe, expect, it } from "vitest";
3+
import { parse } from "yaml";
4+
5+
function readCiWorkflow() {
6+
return parse(readFileSync(".github/workflows/ci.yml", "utf8"));
7+
}
38

49
describe("ci workflow guards", () => {
510
it("kills timed manual checkout fetches after the grace period", () => {
@@ -11,15 +16,11 @@ describe("ci workflow guards", () => {
1116

1217
for (const workflowPath of workflowPaths) {
1318
const workflow = readFileSync(workflowPath, "utf8");
14-
const fetchTimeouts = workflow.match(
15-
/timeout --signal=TERM[^\n]* 30s git -C "\$workdir"/g,
16-
);
19+
const fetchTimeouts = workflow.match(/timeout --signal=TERM[^\n]* 30s git -C "\$workdir"/g);
1720

1821
expect(fetchTimeouts?.length, workflowPath).toBeGreaterThan(0);
1922
expect(fetchTimeouts, workflowPath).toEqual(
20-
fetchTimeouts?.map(
21-
() => 'timeout --signal=TERM --kill-after=10s 30s git -C "$workdir"',
22-
),
23+
fetchTimeouts?.map(() => 'timeout --signal=TERM --kill-after=10s 30s git -C "$workdir"'),
2324
);
2425
}
2526
});
@@ -36,6 +37,16 @@ describe("ci workflow guards", () => {
3637
expect(preflightGuards).toContain("pnpm deps:patches:check");
3738
});
3839

40+
it("does not rebuild Control UI after build:ci-artifacts", () => {
41+
const workflow = readCiWorkflow();
42+
const buildArtifactSteps = workflow.jobs["build-artifacts"].steps;
43+
const buildDistStep = buildArtifactSteps.find((step) => step.name === "Build dist");
44+
45+
expect(buildDistStep.run).toBe("pnpm build:ci-artifacts");
46+
expect(buildArtifactSteps.map((step) => step.name)).not.toContain("Build Control UI");
47+
expect(buildArtifactSteps.some((step) => step.run === "pnpm ui:build")).toBe(false);
48+
});
49+
3950
it("keeps push docs validation ClawHub-backed", () => {
4051
const workflow = readFileSync(".github/workflows/docs.yml", "utf8");
4152

0 commit comments

Comments
 (0)