Vendor OpenClaw source as Adolf fork baseline
Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11), free to diverge. Tree copied sans upstream .git; upstream remote added for future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19. Preserves docs/ARCHITECTURE.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
This commit is contained in:
557
scripts/perf/issue-78851-model-resolution.ts
Normal file
557
scripts/perf/issue-78851-model-resolution.ts
Normal file
@@ -0,0 +1,557 @@
|
||||
// Issue 78851 Model Resolution script supports OpenClaw repository automation.
|
||||
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||
import * as inspector from "node:inspector";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import { monitorEventLoopDelay, performance } from "node:perf_hooks";
|
||||
import { resolveModelAsync } from "../../src/agents/embedded-agent-runner/model.js";
|
||||
import {
|
||||
ensureOpenClawModelsJson,
|
||||
resetModelsJsonReadyCacheForTest,
|
||||
} from "../../src/agents/models-config.js";
|
||||
import type { OpenClawConfig } from "../../src/config/types.openclaw.js";
|
||||
|
||||
type Options = {
|
||||
agentCount: number;
|
||||
cpuProfDir?: string;
|
||||
cpuProfOutput?: string;
|
||||
json: boolean;
|
||||
keepTemp: boolean;
|
||||
lookupsPerRun: number;
|
||||
modelsPerProvider: number;
|
||||
output?: string;
|
||||
providers: number;
|
||||
runs: number;
|
||||
runtimeHooks: boolean;
|
||||
warmup: number;
|
||||
};
|
||||
|
||||
const BOOLEAN_FLAGS = new Set(["--help", "-h", "--json", "--keep-temp", "--runtime-hooks"]);
|
||||
const VALUE_FLAGS = new Set([
|
||||
"--agents",
|
||||
"--cpu-prof-dir",
|
||||
"--cpu-prof-output",
|
||||
"--lookups",
|
||||
"--models-per-provider",
|
||||
"--output",
|
||||
"--providers",
|
||||
"--runs",
|
||||
"--warmup",
|
||||
]);
|
||||
|
||||
class CliArgumentError extends Error {
|
||||
override name = "CliArgumentError";
|
||||
}
|
||||
|
||||
type PhaseSample = {
|
||||
ensureMs: number;
|
||||
resolveMs: number;
|
||||
totalMs: number;
|
||||
wrote: boolean;
|
||||
};
|
||||
|
||||
type RunSample = {
|
||||
cold: PhaseSample;
|
||||
eventLoopDelayMaxMs: number;
|
||||
eventLoopDelayMeanMs: number;
|
||||
index: number;
|
||||
rssMb: number;
|
||||
warm: PhaseSample;
|
||||
};
|
||||
|
||||
type SummaryStats = {
|
||||
avg: number;
|
||||
max: number;
|
||||
min: number;
|
||||
p50: number;
|
||||
p95: number;
|
||||
};
|
||||
|
||||
type Report = {
|
||||
scenario: string;
|
||||
options: Omit<Options, "json" | "keepTemp">;
|
||||
samples: RunSample[];
|
||||
summary: {
|
||||
coldEnsureMs: SummaryStats;
|
||||
coldResolveMs: SummaryStats;
|
||||
coldTotalMs: SummaryStats;
|
||||
warmEnsureMs: SummaryStats;
|
||||
warmResolveMs: SummaryStats;
|
||||
warmTotalMs: SummaryStats;
|
||||
eventLoopDelayMaxMs: SummaryStats;
|
||||
rssMb: SummaryStats;
|
||||
};
|
||||
tempRoot: string;
|
||||
cpuProfilePath?: string;
|
||||
};
|
||||
|
||||
function parseFlagValue(flag: string, args = process.argv.slice(2)): string | undefined {
|
||||
const index = args.indexOf(flag);
|
||||
if (index === -1) {
|
||||
return undefined;
|
||||
}
|
||||
const value = args[index + 1];
|
||||
if (!value || value.startsWith("-")) {
|
||||
throw new CliArgumentError(`${flag} requires a value`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function hasFlag(flag: string, args = process.argv.slice(2)): boolean {
|
||||
return args.includes(flag);
|
||||
}
|
||||
|
||||
function parsePositiveInt(flag: string, fallback: number, args = process.argv.slice(2)): number {
|
||||
const raw = parseFlagValue(flag, args);
|
||||
if (!raw) {
|
||||
return fallback;
|
||||
}
|
||||
const value = Number(raw);
|
||||
if (!Number.isFinite(value) || value <= 0) {
|
||||
throw new CliArgumentError(`${flag} must be a positive integer`);
|
||||
}
|
||||
if (!Number.isInteger(value)) {
|
||||
throw new CliArgumentError(`${flag} must be a positive integer`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function parseNonNegativeInt(flag: string, fallback: number, args = process.argv.slice(2)): number {
|
||||
const raw = parseFlagValue(flag, args);
|
||||
if (!raw) {
|
||||
return fallback;
|
||||
}
|
||||
const value = Number(raw);
|
||||
if (!Number.isFinite(value) || value < 0) {
|
||||
throw new CliArgumentError(`${flag} must be a non-negative integer`);
|
||||
}
|
||||
if (!Number.isInteger(value)) {
|
||||
throw new CliArgumentError(`${flag} must be a non-negative integer`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function validateCliArgs(args = process.argv.slice(2)): void {
|
||||
const seenValueFlags = new Set<string>();
|
||||
for (let index = 0; index < args.length; index += 1) {
|
||||
const arg = args[index] ?? "";
|
||||
if (BOOLEAN_FLAGS.has(arg)) {
|
||||
continue;
|
||||
}
|
||||
if (VALUE_FLAGS.has(arg)) {
|
||||
if (seenValueFlags.has(arg)) {
|
||||
throw new CliArgumentError(`${arg} was provided more than once`);
|
||||
}
|
||||
seenValueFlags.add(arg);
|
||||
const value = args[index + 1];
|
||||
if (!value || value.startsWith("-")) {
|
||||
throw new CliArgumentError(`${arg} requires a value`);
|
||||
}
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
throw new CliArgumentError(`Unknown argument: ${arg}`);
|
||||
}
|
||||
}
|
||||
|
||||
function parseOptions(args = process.argv.slice(2)): Options {
|
||||
validateCliArgs(args);
|
||||
return {
|
||||
agentCount: parsePositiveInt("--agents", 8, args),
|
||||
cpuProfDir: parseFlagValue("--cpu-prof-dir", args),
|
||||
cpuProfOutput: parseFlagValue("--cpu-prof-output", args),
|
||||
json: hasFlag("--json", args),
|
||||
keepTemp: hasFlag("--keep-temp", args),
|
||||
lookupsPerRun: parsePositiveInt("--lookups", 32, args),
|
||||
modelsPerProvider: parsePositiveInt("--models-per-provider", 16, args),
|
||||
output: parseFlagValue("--output", args),
|
||||
providers: parsePositiveInt("--providers", 48, args),
|
||||
runs: parsePositiveInt("--runs", 8, args),
|
||||
runtimeHooks: hasFlag("--runtime-hooks", args),
|
||||
warmup: parseNonNegativeInt("--warmup", 1, args),
|
||||
};
|
||||
}
|
||||
|
||||
function printUsage(): void {
|
||||
process.stdout.write(`OpenClaw issue #78851 model-resolution profiler
|
||||
|
||||
Usage:
|
||||
pnpm perf:issue-78851 -- [options]
|
||||
node --import tsx scripts/perf/issue-78851-model-resolution.ts [options]
|
||||
|
||||
Options:
|
||||
--providers <n> Synthetic configured providers (default: 48)
|
||||
--models-per-provider <n> Models per provider (default: 16)
|
||||
--agents <n> Agent configs/fallback chains (default: 8)
|
||||
--lookups <n> resolveModelAsync calls per phase (default: 32)
|
||||
--runs <n> Measured runs (default: 8)
|
||||
--warmup <n> Warmup runs before measurement (default: 1)
|
||||
--cpu-prof-dir <dir> Write a V8 .cpuprofile for the measured loop
|
||||
--cpu-prof-output <path> Write the V8 .cpuprofile to this exact path
|
||||
--runtime-hooks Include provider runtime hook resolution
|
||||
--output <path> Write JSON report
|
||||
--json Print JSON report
|
||||
--keep-temp Keep generated temp state
|
||||
--help, -h Show this text
|
||||
`);
|
||||
}
|
||||
|
||||
function round(value: number): number {
|
||||
return Math.round(value * 100) / 100;
|
||||
}
|
||||
|
||||
function percentile(values: number[], p: number): number {
|
||||
if (values.length === 0) {
|
||||
return 0;
|
||||
}
|
||||
const sorted = values.toSorted((a, b) => a - b);
|
||||
const index = Math.min(sorted.length - 1, Math.floor((sorted.length - 1) * p));
|
||||
return round(sorted[index] ?? 0);
|
||||
}
|
||||
|
||||
function stats(values: number[]): SummaryStats {
|
||||
if (values.length === 0) {
|
||||
return { avg: 0, max: 0, min: 0, p50: 0, p95: 0 };
|
||||
}
|
||||
const total = values.reduce((sum, value) => sum + value, 0);
|
||||
return {
|
||||
avg: round(total / values.length),
|
||||
max: round(Math.max(...values)),
|
||||
min: round(Math.min(...values)),
|
||||
p50: percentile(values, 0.5),
|
||||
p95: percentile(values, 0.95),
|
||||
};
|
||||
}
|
||||
|
||||
function modelRef(providerIndex: number, modelIndex: number): string {
|
||||
return `perf-${providerIndex}/perf-model-${modelIndex}`;
|
||||
}
|
||||
|
||||
function buildConfig(options: Options, workspaceDir: string): OpenClawConfig {
|
||||
const providers: NonNullable<NonNullable<OpenClawConfig["models"]>["providers"]> = {};
|
||||
for (let providerIndex = 0; providerIndex < options.providers; providerIndex += 1) {
|
||||
providers[`perf-${providerIndex}`] = {
|
||||
api: providerIndex % 2 === 0 ? "openai-responses" : "openai-completions",
|
||||
apiKey: "perf-key",
|
||||
baseUrl: `http://127.0.0.1:${20_000 + providerIndex}/v1`,
|
||||
models: Array.from({ length: options.modelsPerProvider }, (_, modelIndex) => ({
|
||||
api: modelIndex % 2 === 0 ? "openai-responses" : "openai-completions",
|
||||
baseUrl: `http://127.0.0.1:${20_000 + providerIndex}/v1`,
|
||||
contextWindow: 128_000 + modelIndex,
|
||||
cost: { cacheRead: 0, cacheWrite: 0, input: 0, output: 0 },
|
||||
id: `perf-model-${modelIndex}`,
|
||||
input: modelIndex % 5 === 0 ? ["text", "image"] : ["text"],
|
||||
maxTokens: 8192,
|
||||
name: `Perf Model ${providerIndex}.${modelIndex}`,
|
||||
params: {
|
||||
cacheRetention: modelIndex % 3 === 0 ? "ephemeral" : undefined,
|
||||
syntheticRank: providerIndex * options.modelsPerProvider + modelIndex,
|
||||
},
|
||||
reasoning: modelIndex % 3 === 0,
|
||||
})),
|
||||
params: {
|
||||
syntheticProviderRank: providerIndex,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const fallbacks = Array.from({ length: Math.min(12, options.providers) }, (_, index) =>
|
||||
modelRef(index, index % options.modelsPerProvider),
|
||||
);
|
||||
|
||||
return {
|
||||
browser: { enabled: false },
|
||||
agents: {
|
||||
defaults: {
|
||||
contextInjection: "never",
|
||||
model: {
|
||||
primary: modelRef(0, 0),
|
||||
fallbacks,
|
||||
},
|
||||
skipBootstrap: true,
|
||||
workspace: workspaceDir,
|
||||
},
|
||||
list: Array.from({ length: options.agentCount }, (_, index) => ({
|
||||
default: index === 0,
|
||||
id: `agent-${index}`,
|
||||
model: {
|
||||
primary: modelRef(index % options.providers, index % options.modelsPerProvider),
|
||||
fallbacks: fallbacks.toReversed(),
|
||||
},
|
||||
workspace: path.join(workspaceDir, `agent-${index}`),
|
||||
})),
|
||||
},
|
||||
gateway: {
|
||||
auth: { mode: "none" },
|
||||
bind: "loopback",
|
||||
controlUi: { enabled: false },
|
||||
mode: "local",
|
||||
},
|
||||
memory: {
|
||||
active: {
|
||||
allowedChatTypes: ["direct"],
|
||||
agents: ["main"],
|
||||
logging: false,
|
||||
maxSummaryChars: 220,
|
||||
persistTranscripts: false,
|
||||
promptStyle: "balanced",
|
||||
queryMode: "recent",
|
||||
timeoutMs: 15_000,
|
||||
},
|
||||
},
|
||||
models: {
|
||||
mode: "replace",
|
||||
providers,
|
||||
},
|
||||
plugins: {
|
||||
enabled: true,
|
||||
entries: {
|
||||
browser: { enabled: false },
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function startCpuProfile(params: { dir?: string; output?: string }): Promise<{
|
||||
stop: () => Promise<string>;
|
||||
}> {
|
||||
const fallbackDir = ".artifacts/perf/issue-78851/cpu";
|
||||
const cpuProfDir = params.dir ?? path.dirname(params.output ?? fallbackDir);
|
||||
await mkdir(cpuProfDir, { recursive: true });
|
||||
const session = new inspector.Session();
|
||||
session.connect();
|
||||
const post = <T>(method: string, paramsLocal?: Record<string, unknown>) =>
|
||||
new Promise<T>((resolve, reject) => {
|
||||
session.post(method, paramsLocal ?? {}, (error, result) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(result as T);
|
||||
}
|
||||
});
|
||||
});
|
||||
await post("Profiler.enable");
|
||||
await post("Profiler.start");
|
||||
return {
|
||||
async stop() {
|
||||
const result = await post<{ profile: unknown }>("Profiler.stop");
|
||||
session.disconnect();
|
||||
const profilePath =
|
||||
params.output ??
|
||||
path.join(cpuProfDir, `issue-78851-${process.pid}-${Date.now()}.cpuprofile`);
|
||||
await mkdir(path.dirname(profilePath), { recursive: true });
|
||||
await writeFile(profilePath, JSON.stringify(result.profile));
|
||||
return profilePath;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function measurePhase(params: {
|
||||
agentDir: string;
|
||||
config: OpenClawConfig;
|
||||
lookups: number;
|
||||
modelIndexOffset: number;
|
||||
providerCount: number;
|
||||
modelsPerProvider: number;
|
||||
workspaceDir: string;
|
||||
runtimeHooks: boolean;
|
||||
}): Promise<PhaseSample> {
|
||||
const started = performance.now();
|
||||
const ensureStarted = performance.now();
|
||||
const ensureResult = await ensureOpenClawModelsJson(params.config, params.agentDir, {
|
||||
// Keep this harness deterministic by measuring configured-model scale.
|
||||
// Live provider catalog timing belongs in a separate Crabbox lane with secrets.
|
||||
providerDiscoveryProviderIds: [],
|
||||
providerDiscoveryTimeoutMs: 5_000,
|
||||
workspaceDir: params.workspaceDir,
|
||||
});
|
||||
const ensureMs = performance.now() - ensureStarted;
|
||||
const resolveStarted = performance.now();
|
||||
for (let lookupIndex = 0; lookupIndex < params.lookups; lookupIndex += 1) {
|
||||
const providerIndex = lookupIndex % params.providerCount;
|
||||
const modelIndex = (lookupIndex + params.modelIndexOffset) % params.modelsPerProvider;
|
||||
const resolved = await resolveModelAsync(
|
||||
`perf-${providerIndex}`,
|
||||
`perf-model-${modelIndex}`,
|
||||
params.agentDir,
|
||||
params.config,
|
||||
{
|
||||
skipProviderRuntimeHooks: !params.runtimeHooks,
|
||||
workspaceDir: params.workspaceDir,
|
||||
},
|
||||
);
|
||||
if (!resolved.model) {
|
||||
throw new Error(resolved.error ?? `failed to resolve ${modelRef(providerIndex, modelIndex)}`);
|
||||
}
|
||||
}
|
||||
const resolveMs = performance.now() - resolveStarted;
|
||||
return {
|
||||
ensureMs: round(ensureMs),
|
||||
resolveMs: round(resolveMs),
|
||||
totalMs: round(performance.now() - started),
|
||||
wrote: ensureResult.wrote,
|
||||
};
|
||||
}
|
||||
|
||||
async function runOne(params: {
|
||||
config: OpenClawConfig;
|
||||
index: number;
|
||||
options: Options;
|
||||
tempRoot: string;
|
||||
workspaceDir: string;
|
||||
}): Promise<RunSample> {
|
||||
const agentDir = path.join(params.tempRoot, `agent-state-${params.index}`);
|
||||
await mkdir(agentDir, { recursive: true });
|
||||
resetModelsJsonReadyCacheForTest();
|
||||
const histogram = monitorEventLoopDelay({ resolution: 10 });
|
||||
histogram.enable();
|
||||
const cold = await measurePhase({
|
||||
agentDir,
|
||||
config: params.config,
|
||||
lookups: params.options.lookupsPerRun,
|
||||
modelIndexOffset: params.index,
|
||||
modelsPerProvider: params.options.modelsPerProvider,
|
||||
providerCount: params.options.providers,
|
||||
workspaceDir: params.workspaceDir,
|
||||
runtimeHooks: params.options.runtimeHooks,
|
||||
});
|
||||
const warm = await measurePhase({
|
||||
agentDir,
|
||||
config: params.config,
|
||||
lookups: params.options.lookupsPerRun,
|
||||
modelIndexOffset: params.index + 1,
|
||||
modelsPerProvider: params.options.modelsPerProvider,
|
||||
providerCount: params.options.providers,
|
||||
workspaceDir: params.workspaceDir,
|
||||
runtimeHooks: params.options.runtimeHooks,
|
||||
});
|
||||
histogram.disable();
|
||||
return {
|
||||
cold,
|
||||
eventLoopDelayMaxMs: round(histogram.max / 1_000_000),
|
||||
eventLoopDelayMeanMs: round(histogram.mean / 1_000_000),
|
||||
index: params.index,
|
||||
rssMb: round(process.memoryUsage().rss / 1024 / 1024),
|
||||
warm,
|
||||
};
|
||||
}
|
||||
|
||||
function summarize(samples: RunSample[]): Report["summary"] {
|
||||
return {
|
||||
coldEnsureMs: stats(samples.map((sample) => sample.cold.ensureMs)),
|
||||
coldResolveMs: stats(samples.map((sample) => sample.cold.resolveMs)),
|
||||
coldTotalMs: stats(samples.map((sample) => sample.cold.totalMs)),
|
||||
eventLoopDelayMaxMs: stats(samples.map((sample) => sample.eventLoopDelayMaxMs)),
|
||||
rssMb: stats(samples.map((sample) => sample.rssMb)),
|
||||
warmEnsureMs: stats(samples.map((sample) => sample.warm.ensureMs)),
|
||||
warmResolveMs: stats(samples.map((sample) => sample.warm.resolveMs)),
|
||||
warmTotalMs: stats(samples.map((sample) => sample.warm.totalMs)),
|
||||
};
|
||||
}
|
||||
|
||||
function printHuman(report: Report, cpuProfilePath?: string): void {
|
||||
const lines = [
|
||||
`scenario: ${report.scenario}`,
|
||||
`providers: ${report.options.providers}`,
|
||||
`modelsPerProvider: ${report.options.modelsPerProvider}`,
|
||||
`agents: ${report.options.agentCount}`,
|
||||
`lookups: ${report.options.lookupsPerRun}`,
|
||||
`runs: ${report.options.runs}`,
|
||||
`runtimeHooks: ${report.options.runtimeHooks}`,
|
||||
`coldTotalMs: avg=${report.summary.coldTotalMs.avg} p50=${report.summary.coldTotalMs.p50} p95=${report.summary.coldTotalMs.p95} max=${report.summary.coldTotalMs.max}`,
|
||||
`coldEnsureMs: avg=${report.summary.coldEnsureMs.avg} p50=${report.summary.coldEnsureMs.p50} p95=${report.summary.coldEnsureMs.p95} max=${report.summary.coldEnsureMs.max}`,
|
||||
`coldResolveMs: avg=${report.summary.coldResolveMs.avg} p50=${report.summary.coldResolveMs.p50} p95=${report.summary.coldResolveMs.p95} max=${report.summary.coldResolveMs.max}`,
|
||||
`warmTotalMs: avg=${report.summary.warmTotalMs.avg} p50=${report.summary.warmTotalMs.p50} p95=${report.summary.warmTotalMs.p95} max=${report.summary.warmTotalMs.max}`,
|
||||
`warmEnsureMs: avg=${report.summary.warmEnsureMs.avg} p50=${report.summary.warmEnsureMs.p50} p95=${report.summary.warmEnsureMs.p95} max=${report.summary.warmEnsureMs.max}`,
|
||||
`warmResolveMs: avg=${report.summary.warmResolveMs.avg} p50=${report.summary.warmResolveMs.p50} p95=${report.summary.warmResolveMs.p95} max=${report.summary.warmResolveMs.max}`,
|
||||
`eventLoopDelayMaxMs: avg=${report.summary.eventLoopDelayMaxMs.avg} max=${report.summary.eventLoopDelayMaxMs.max}`,
|
||||
`rssMb: avg=${report.summary.rssMb.avg} max=${report.summary.rssMb.max}`,
|
||||
];
|
||||
if (report.options.output) {
|
||||
lines.push(`output: ${report.options.output}`);
|
||||
}
|
||||
if (report.cpuProfilePath ?? cpuProfilePath) {
|
||||
lines.push(`cpuProfile: ${report.cpuProfilePath ?? cpuProfilePath}`);
|
||||
}
|
||||
process.stdout.write(`${lines.join("\n")}\n`);
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const args = process.argv.slice(2);
|
||||
validateCliArgs(args);
|
||||
if (hasFlag("--help", args) || hasFlag("-h", args)) {
|
||||
printUsage();
|
||||
return;
|
||||
}
|
||||
const options = parseOptions(args);
|
||||
const tempRoot = await mkdtemp(path.join(tmpdir(), "openclaw-issue-78851-"));
|
||||
const workspaceDir = path.join(tempRoot, "workspace");
|
||||
await mkdir(workspaceDir, { recursive: true });
|
||||
const config = buildConfig(options, workspaceDir);
|
||||
let profiler: Awaited<ReturnType<typeof startCpuProfile>> | undefined;
|
||||
let cpuProfilePath: string | undefined;
|
||||
try {
|
||||
if (options.cpuProfDir ?? options.cpuProfOutput) {
|
||||
profiler = await startCpuProfile({
|
||||
dir: options.cpuProfDir,
|
||||
output: options.cpuProfOutput,
|
||||
});
|
||||
}
|
||||
for (let index = 0; index < options.warmup; index += 1) {
|
||||
await runOne({ config, index: -index - 1, options, tempRoot, workspaceDir });
|
||||
}
|
||||
const samples: RunSample[] = [];
|
||||
for (let index = 0; index < options.runs; index += 1) {
|
||||
samples.push(await runOne({ config, index, options, tempRoot, workspaceDir }));
|
||||
}
|
||||
if (profiler) {
|
||||
cpuProfilePath = await profiler.stop();
|
||||
profiler = undefined;
|
||||
}
|
||||
const report: Report = {
|
||||
options: {
|
||||
agentCount: options.agentCount,
|
||||
cpuProfDir: options.cpuProfDir,
|
||||
cpuProfOutput: options.cpuProfOutput,
|
||||
lookupsPerRun: options.lookupsPerRun,
|
||||
modelsPerProvider: options.modelsPerProvider,
|
||||
output: options.output,
|
||||
providers: options.providers,
|
||||
runs: options.runs,
|
||||
runtimeHooks: options.runtimeHooks,
|
||||
warmup: options.warmup,
|
||||
},
|
||||
samples,
|
||||
scenario: "issue-78851-model-resolution",
|
||||
summary: summarize(samples),
|
||||
tempRoot,
|
||||
...(cpuProfilePath ? { cpuProfilePath } : {}),
|
||||
};
|
||||
if (options.output) {
|
||||
await mkdir(path.dirname(options.output), { recursive: true });
|
||||
await writeFile(options.output, `${JSON.stringify(report, null, 2)}\n`);
|
||||
}
|
||||
if (options.json) {
|
||||
process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
|
||||
} else {
|
||||
printHuman(report, cpuProfilePath);
|
||||
}
|
||||
} finally {
|
||||
if (profiler) {
|
||||
await profiler.stop().catch(() => undefined);
|
||||
}
|
||||
if (!options.keepTemp) {
|
||||
await rm(tempRoot, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((error: unknown) => {
|
||||
if (error instanceof CliArgumentError) {
|
||||
process.stderr.write(`${error.message}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
const message = error instanceof Error ? (error.stack ?? error.message) : String(error);
|
||||
process.stderr.write(`${message}\n`);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user