Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | 2x 2x 2x 3x 3x 2x 2x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 7x 2x 2x 2x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 30x 30x 30x 24x 6x 12x 12x 12x 18x 30x 18x 30x 30x 18x 2x 2x 2x 6x 2x 2x 2x 6x 6x 6x 6x 2x | import { execFile } from "node:child_process";
import os from "node:os";
import { promisify } from "node:util";
const execFileAsync = promisify(execFile);
type CommandResult = { stdout: string; stderr?: string };
type CommandRunner = (file: string, args: string[]) => Promise<CommandResult>;
type OptionalHostSignals = {
physicalMemoryBytes: number | null;
loadAverage: number[] | null;
availableMemoryBytes: number | null;
swapUsedBytes: number | null;
iowaitPercent: number | null;
thermalPressure: string | null;
};
export type HostProbeDependencies = {
run: CommandRunner;
logicalCpus: () => number;
architecture: string;
nodeVersion: string;
optional?: OptionalHostSignals;
};
export type HostProbe = {
required: { gitSha: string; gitDirty: boolean; nodeVersion: string; architecture: string; logicalCpus: number };
optional: OptionalHostSignals;
safetySignals: { cpuAvailable: boolean; memoryAvailable: boolean };
};
export type DockerProbeDependencies = {
run: (args: string[]) => Promise<CommandResult>;
};
export type DockerProbe = {
clientVersion: string;
serverVersion: string;
architecture: string;
cpus: number;
memoryBytes: number;
storageDriver: string;
daemonHealthy: boolean;
image: { reference: string; id: string; digest: string | null };
runningContainers: Array<{ id: string; name: string; image: string; status: string; labels: Record<string, string> }>;
};
export type ControlLatencySample = {
command: string;
condition: "idle" | "parallel";
durationsMs: number[];
error: string | null;
};
export type ControlDependencies = {
run: (args: string[]) => Promise<CommandResult>;
now?: () => number;
};
const defaultRun: CommandRunner = async (file, args) => {
const result = await execFileAsync(file, args, { encoding: "utf8", maxBuffer: 4 * 1024 * 1024 });
return { stdout: result.stdout, stderr: result.stderr };
};
function defaultHostDependencies(): HostProbeDependencies {
return {
run: defaultRun,
logicalCpus: () => os.cpus().length,
architecture: process.arch,
nodeVersion: process.version,
optional: {
physicalMemoryBytes: os.totalmem(),
loadAverage: os.loadavg(),
availableMemoryBytes: os.freemem(),
swapUsedBytes: null,
iowaitPercent: null,
thermalPressure: null,
},
};
}
function parseJson<T>(stdout: string, label: string): T {
try {
return JSON.parse(stdout) as T;
} catch (error) {
throw new Error(`Invalid ${label} JSON: ${error instanceof Error ? error.message : String(error)}`);
}
}
export async function collectHostProbe(deps: Partial<HostProbeDependencies> = {}): Promise<HostProbe> {
const resolved = { ...defaultHostDependencies(), ...deps } as HostProbeDependencies;
const gitSha = (await resolved.run("git", ["rev-parse", "HEAD"])).stdout.trim();
Iif (!gitSha) throw new Error("Git SHA probe returned no value");
const gitStatus = (await resolved.run("git", ["status", "--porcelain"])).stdout;
const logicalCpus = resolved.logicalCpus();
Iif (!Number.isInteger(logicalCpus) || logicalCpus < 1) throw new Error("Logical CPU probe returned an invalid value");
const optional = resolved.optional ?? defaultHostDependencies().optional!;
return {
required: {
gitSha,
gitDirty: gitStatus.trim().length > 0,
nodeVersion: resolved.nodeVersion,
architecture: resolved.architecture,
logicalCpus,
},
optional,
safetySignals: {
cpuAvailable: logicalCpus > 0,
memoryAvailable: optional.availableMemoryBytes !== null && optional.availableMemoryBytes > 0,
},
};
}
function parseLabels(value: unknown): Record<string, string> {
Iif (typeof value !== "string") return {};
return Object.fromEntries(value.split(",").map((entry) => {
const separator = entry.indexOf("=");
return separator < 0 ? [entry, ""] : [entry.slice(0, separator), entry.slice(separator + 1)];
}).filter(([key]) => key.length > 0));
}
function stringField(value: unknown, fallback: string): string {
return typeof value === "string" ? value : fallback;
}
export async function collectDockerProbe(
image: string,
deps: Partial<DockerProbeDependencies> = {},
): Promise<DockerProbe> {
const run = deps.run ?? (async (args) => {
const result = await execFileAsync("docker", args, { encoding: "utf8", maxBuffer: 4 * 1024 * 1024 });
return { stdout: result.stdout, stderr: result.stderr };
});
const version = (await run(["version", "--format", "{{.Client.Version}}|{{.Server.Version}}"])).stdout.trim().split("|");
const info = parseJson<Record<string, unknown>>((await run(["info", "--format", "{{json .}}"])).stdout, "docker info");
const imageRows = parseJson<unknown>((await run(["image", "inspect", image, "--format", "{{json .}}"])).stdout, "docker image inspect");
const inspectedImage = Array.isArray(imageRows) ? imageRows[0] as Record<string, unknown> : imageRows as Record<string, unknown>;
const containers = (await run(["ps", "--format", "{{json .}}"])).stdout
.split("\n")
.map((line) => line.trim())
.filter(Boolean)
.map((line) => parseJson<Record<string, unknown>>(line, "docker ps"))
.map((row) => ({
id: stringField(row.ID, ""),
name: stringField(row.Names, ""),
image: stringField(row.Image, ""),
status: stringField(row.Status, ""),
labels: parseLabels(row.Labels),
}));
const cpus = Number(info.NCPU);
const memoryBytes = Number(info.MemTotal);
Iif (!Number.isFinite(cpus) || cpus < 1 || !Number.isFinite(memoryBytes) || memoryBytes <= 0) {
throw new Error("Docker info returned invalid CPU or memory values");
}
const id = stringField(inspectedImage.Id, "");
Iif (!id) throw new Error(`Docker image ${image} has no image ID`);
const digests = Array.isArray(inspectedImage.RepoDigests) ? inspectedImage.RepoDigests : [];
return {
clientVersion: version[0] ?? "unknown",
serverVersion: version[1] ?? "unknown",
architecture: stringField(info.Architecture, "unknown"),
cpus,
memoryBytes,
storageDriver: stringField(info.Driver, "unknown"),
daemonHealthy: true,
image: { reference: image, id, digest: digests.length > 0 ? String(digests[0]) : null },
runningContainers: containers,
};
}
function controlCommands(image: string): Array<{ name: string; args: string[] }> {
return [
{ name: "docker --version", args: ["--version"] },
{ name: "docker info", args: ["info"] },
{ name: `docker image inspect ${image}`, args: ["image", "inspect", image] },
];
}
async function timedControlCall(
command: { name: string; args: string[] },
run: ControlDependencies["run"],
now: () => number,
): Promise<{ durationMs: number | null; error: string | null }> {
const startedAt = now();
try {
await run(command.args);
return { durationMs: Math.max(0, now() - startedAt), error: null };
} catch (error) {
return { durationMs: null, error: error instanceof Error ? error.message : String(error) };
}
}
async function measureControlSeries(
command: { name: string; args: string[] },
count: number,
parallelism: number,
run: ControlDependencies["run"],
now: () => number,
): Promise<{ durationsMs: number[]; error: string | null }> {
const durationsMs: number[] = [];
let error: string | null = null;
for (let sample = 0; sample < count; sample += 1) {
const measurements = await Promise.all(
Array.from({ length: parallelism }, () => timedControlCall(command, run, now)),
);
for (const measurement of measurements) {
if (measurement.durationMs !== null) durationsMs.push(measurement.durationMs);
error ??= measurement.error;
}
}
return { durationsMs, error };
}
export async function measureDockerControlLatency(
image: string,
parallelism: number,
samples: number,
deps: Partial<ControlDependencies> = {},
): Promise<ControlLatencySample[]> {
Iif (!Number.isInteger(parallelism) || parallelism < 1) throw new Error("parallelism must be a positive integer");
Iif (!Number.isInteger(samples) || samples < 1) throw new Error("samples must be a positive integer");
const run = deps.run ?? (async (args) => {
const result = await execFileAsync("docker", args, { encoding: "utf8", maxBuffer: 4 * 1024 * 1024 });
return { stdout: result.stdout, stderr: result.stderr };
});
const now = deps.now ?? (() => Number(process.hrtime.bigint()) / 1_000_000);
const commands = controlCommands(image);
const result: ControlLatencySample[] = [];
for (const command of commands) {
const idle = await measureControlSeries(command, samples, 1, run, now);
result.push({ command: command.name, condition: "idle", ...idle });
const parallel = await measureControlSeries(command, samples, parallelism, run, now);
result.push({ command: command.name, condition: "parallel", ...parallel });
}
return result;
}
|