/** * Three-phase search for optimal streamFlushMillis values. * * Phase 1 (Coarse): Binary search with few tasks per step to quickly find the * approximate threshold where noTaskData events start appearing. * * Phase 2 (Validate): Run many trials with more tasks around the coarse * threshold to confirm the exact minimum reliable value. * * Phase 3 (Confirm): Hammer the found minimum with many more trials to ensure * stability. * * The exported function returns the minimum reliable value; callers should * apply their own safety margin. * * @module */ import type child_process from "node:child_process"; import type { Logger } from "./Logger"; import type { Task } from "./Task"; /** * Options for {@link findStreamFlushMillis}. * * @typeParam T - The resolved type of the tasks produced by * {@link taskFactory}. The search ignores task results (only * `noTaskData` events matter), so this can be anything. */ export interface FindFlushMillisOptions { /** Factory that spawns child processes. */ processFactory: () => child_process.ChildProcess | Promise; /** Low-overhead startup command to verify the child process started. */ versionCommand: string; /** Pattern indicating a task passed. */ pass: string | RegExp; /** Pattern indicating a task failed. */ fail: string | RegExp; /** Command to gracefully shut down a child process. */ exitCommand?: string; /** * Creates a fresh {@link Task} for each trial iteration. Called with a * 0-based index. Callers should vary commands across calls to exercise * different code paths (clean successes, stderr-emitting commands, * writes, etc.). */ taskFactory: (taskIndex: number) => Task; /** * Lower bound of the search range in milliseconds. * @default 0 */ lo?: number; /** * Upper bound of the search range in milliseconds. * @default 500 */ hi?: number; /** * Maximum concurrent child processes during trials. * @default 4 */ maxProcs?: number; /** * Tasks per binary-search step in the coarse phase. * @default 10 */ coarseTasks?: number; /** * Tasks per trial in the validation phase. * @default 50 */ validationTasks?: number; /** * Number of trials at each candidate value during validation. * @default 5 */ validationTrials?: number; /** * How many ms above/below the coarse result to validate. * @default 3 */ validationRadius?: number; /** * Number of trials at the found minimum during confirmation. * @default 20 */ confirmationTrials?: number; /** * Tasks per trial in the confirmation phase. * @default 50 */ confirmationTasks?: number; /** * Logger for progress messages. Progress goes to `info`, failures to * `warn`. When not provided, all output is silently discarded. */ logger?: () => Logger; } /** * Find the minimum reliable `streamFlushMillis` value for the given child * process on the current hardware. * * Returns the minimum `streamFlushMillis` value that produced zero * `noTaskData` events across all confirmation trials. Because the * confirmation phase already tests the worst case observed during * discovery, the returned value is a near-ceiling, not a mean — similar in * spirit to how TCP's RTO (RFC 6298) is `SRTT + 4*RTTVAR` rather than a * flat multiplier. Callers needing additional headroom for transient load * spikes should add a small additive constant (e.g. `result + 5`), since * OS scheduling jitter is an additive phenomenon independent of the * baseline value. * * @example * ```typescript * import { findStreamFlushMillis, Task } from "batch-cluster"; * * const minReliable = await findStreamFlushMillis({ * processFactory: () => spawn("perl", [exiftoolPath, "-stay_open", "True", "-@", "-"]), * versionCommand: "-ver\n-execute\n", * pass: "{ready}", * fail: "{ready}", * exitCommand: "-stay_open\nFalse\n", * taskFactory: (i) => { * const cmds = ["-json\n/clean.jpg\n-execute\n", "-validate\n/warn.jpg\n-execute\n"]; * return new Task(cmds[i % cmds.length], (stdout) => stdout); * }, * }); * const streamFlushMillis = minReliable + 5; // additive headroom for load spikes * ``` */ export declare function findStreamFlushMillis(options: FindFlushMillisOptions): Promise;