Skip to content

Commit 6293b54

Browse files
authored
perf(cli): split plugin registry startup timing (#119318)
* perf(cli): split plugin registry startup timing Punchcard-Session: coral-workshop-workshop-3f * test(cli): use managed temp directory cleanup Punchcard-Session: coral-workshop-workshop-3f
1 parent aa07d55 commit 6293b54

2 files changed

Lines changed: 56 additions & 16 deletions

File tree

‎src/cli/plugin-registry-loader.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
// Plugin registry loader tests cover CLI plugin registry loading and cache reset behavior.
2+
import { readFile } from "node:fs/promises";
3+
import { join } from "node:path";
24
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
5+
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
6+
import { measureCliCommandStartup } from "./command-startup-timing.js";
37

48
const ensurePluginRegistryLoadedMock = vi.hoisted(() => vi.fn());
9+
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
510

611
vi.mock("./plugin-registry.js", () => ({
712
ensurePluginRegistryLoaded: ensurePluginRegistryLoadedMock,
@@ -25,6 +30,7 @@ describe("plugin-registry-loader", () => {
2530

2631
afterEach(() => {
2732
loggingState.forceConsoleToStderr = originalForceStderr;
33+
vi.unstubAllEnvs();
2834
});
2935

3036
it("routes plugin load logs to stderr and restores state", async () => {
@@ -85,4 +91,32 @@ describe("plugin-registry-loader", () => {
8591
scope: "configured-channels",
8692
});
8793
});
94+
95+
it("attributes module import separately from runtime loading", async () => {
96+
const dir = tempDirs.make("openclaw-plugin-registry-startup-");
97+
const timelinePath = join(dir, "timeline.jsonl");
98+
vi.stubEnv("OPENCLAW_DIAGNOSTICS", "timeline");
99+
vi.stubEnv("OPENCLAW_DIAGNOSTICS_TIMELINE_PATH", timelinePath);
100+
101+
await measureCliCommandStartup("plugin-registry", () =>
102+
ensureCliPluginRegistryLoaded({
103+
scope: "all",
104+
}),
105+
);
106+
107+
const events = (await readFile(timelinePath, "utf8"))
108+
.trim()
109+
.split("\n")
110+
.map((line) => JSON.parse(line) as Record<string, unknown>);
111+
const starts = events.filter((event) => event.type === "span.start");
112+
const outer = starts.find(
113+
(event) => (event.attributes as { stage?: string } | undefined)?.stage === "plugin-registry",
114+
);
115+
expect(outer).toBeDefined();
116+
expect(
117+
starts
118+
.filter((event) => event.parentSpanId === outer?.spanId)
119+
.map((event) => (event.attributes as { stage?: string } | undefined)?.stage),
120+
).toEqual(["plugin-registry-module-import", "plugin-registry-runtime-load"]);
121+
});
88122
});

‎src/cli/plugin-registry-loader.ts‎

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { OpenClawConfig } from "../config/types.openclaw.js";
33
import { loggingState } from "../logging/state.js";
44
import { createLazyImportLoader } from "../shared/lazy-promise.js";
55
import type { CliPluginRegistryScope } from "./command-catalog.js";
6+
import { measureCliCommandStartup } from "./command-startup-timing.js";
67

78
const pluginRegistryModuleLoader = createLazyImportLoader(() => import("./plugin-registry.js"));
89

@@ -17,20 +18,25 @@ export async function ensureCliPluginRegistryLoaded(params: {
1718
config?: OpenClawConfig;
1819
activationSourceConfig?: OpenClawConfig;
1920
}) {
20-
const { ensurePluginRegistryLoaded } = await loadPluginRegistryModule();
21-
const previousForceStderr = loggingState.forceConsoleToStderr;
22-
if (params.routeLogsToStderr) {
23-
loggingState.forceConsoleToStderr = true;
24-
}
25-
try {
26-
ensurePluginRegistryLoaded({
27-
scope: params.scope,
28-
...(params.config ? { config: params.config } : {}),
29-
...(params.activationSourceConfig
30-
? { activationSourceConfig: params.activationSourceConfig }
31-
: {}),
32-
});
33-
} finally {
34-
loggingState.forceConsoleToStderr = previousForceStderr;
35-
}
21+
const { ensurePluginRegistryLoaded } = await measureCliCommandStartup(
22+
"plugin-registry-module-import",
23+
loadPluginRegistryModule,
24+
);
25+
await measureCliCommandStartup("plugin-registry-runtime-load", () => {
26+
const previousForceStderr = loggingState.forceConsoleToStderr;
27+
if (params.routeLogsToStderr) {
28+
loggingState.forceConsoleToStderr = true;
29+
}
30+
try {
31+
ensurePluginRegistryLoaded({
32+
scope: params.scope,
33+
...(params.config ? { config: params.config } : {}),
34+
...(params.activationSourceConfig
35+
? { activationSourceConfig: params.activationSourceConfig }
36+
: {}),
37+
});
38+
} finally {
39+
loggingState.forceConsoleToStderr = previousForceStderr;
40+
}
41+
});
3642
}

0 commit comments

Comments
 (0)