diff --git a/src/cli/commands/baseline.ts b/src/cli/commands/baseline.ts index 556f20c..e26b0cf 100644 --- a/src/cli/commands/baseline.ts +++ b/src/cli/commands/baseline.ts @@ -11,6 +11,10 @@ import type { CliContext } from '../index.js'; import { BaselineStorageService } from '../../services/baseline-storage.js'; import { createTaskId } from '../../types/index.js'; +import { + formatDuration, + formatOpsPerSecond, +} from '../../utils/reporter-utils.js'; /** * Options for baseline analyze command @@ -62,38 +66,6 @@ interface BaselineShowOptions extends BaselineBaseOptions { name: string; } -/** - * Format duration in human-readable format - */ -const formatDuration = (nanoseconds: number): string => { - if (nanoseconds < 1000) { - return `${nanoseconds.toFixed(2)}ns`; - } - if (nanoseconds < 1_000_000) { - return `${(nanoseconds / 1000).toFixed(2)}μs`; - } - if (nanoseconds < 1_000_000_000) { - return `${(nanoseconds / 1_000_000).toFixed(2)}ms`; - } - return `${(nanoseconds / 1_000_000_000).toFixed(2)}s`; -}; - -/** - * Format operations per second - */ -const formatOpsPerSec = (ops: number): string => { - if (ops < 1000) { - return `${ops.toFixed(2)} ops/sec`; - } - if (ops < 1_000_000) { - return `${(ops / 1000).toFixed(2)}K ops/sec`; - } - if (ops < 1_000_000_000) { - return `${(ops / 1_000_000).toFixed(2)}M ops/sec`; - } - return `${(ops / 1_000_000_000).toFixed(2)}B ops/sec`; -}; - /** * Format date in readable format */ @@ -337,7 +309,9 @@ export const handleShowCommand = async ( for (const [taskId, data] of tasks) { console.log(` ${taskId}`); console.log(` Mean: ${formatDuration(data.mean)}`); - console.log(` Ops/sec: ${formatOpsPerSec(data.opsPerSecond)}`); + console.log( + ` Ops/sec: ${formatOpsPerSecond(data.opsPerSecond)}`, + ); if (data.p99) { console.log(` P99: ${formatDuration(data.p99)}`); } diff --git a/src/cli/commands/history.ts b/src/cli/commands/history.ts index 827eb5c..a20a714 100644 --- a/src/cli/commands/history.ts +++ b/src/cli/commands/history.ts @@ -22,6 +22,7 @@ import { parseDate, } from '../../services/history/query.js'; import { TrendAnalysisService } from '../../services/history/trend-analysis.js'; +import { formatBytes } from '../../utils/reporter-utils.js'; /** * Base options shared by all history subcommands @@ -94,22 +95,6 @@ interface HistoryTrendsOptions extends BaseHistoryOptions { until?: string | undefined; } -/** - * Format bytes in human-readable format - */ -const formatBytes = (bytes: number): string => { - const units = ['B', 'KB', 'MB', 'GB']; - let size = bytes; - let unitIndex = 0; - - while (size >= 1024 && unitIndex < units.length - 1) { - size /= 1024; - unitIndex++; - } - - return `${size.toFixed(1)} ${units[unitIndex]}`; -}; - /** * Resolve a partial run ID to a full ID by checking prefix match * diff --git a/src/cli/handlers.ts b/src/cli/handlers.ts index e0d7507..f909827 100644 --- a/src/cli/handlers.ts +++ b/src/cli/handlers.ts @@ -8,8 +8,8 @@ */ import { ABORT_TIMEOUT, ExitCodes } from '../constants.js'; -import { isError } from '../errors/base.js'; import { isModestBenchError, UnknownError } from '../errors/index.js'; +import { isError } from '../utils/type-guards.js'; /** * Handle process signals gracefully @@ -65,5 +65,12 @@ export const setupSignalHandlers = (abortController: AbortController): void => { * @returns The exit code */ const computeExitCode = (signal: NodeJS.Signals): number => { - return 128 + (signal === 'SIGINT' ? 2 : signal === 'SIGQUIT' ? 3 : 15); + switch (signal) { + case 'SIGINT': + return 130; // 128 + 2 + case 'SIGQUIT': + return 131; // 128 + 3 + default: + return 143; // 128 + 15 (SIGTERM) + } }; diff --git a/src/errors/base.ts b/src/errors/base.ts index 8020b7f..6cbb599 100644 --- a/src/errors/base.ts +++ b/src/errors/base.ts @@ -6,6 +6,7 @@ */ import { SITE_URL } from '../constants.js'; +import { isError } from '../utils/type-guards.js'; /** * Base URL for error documentation @@ -150,13 +151,3 @@ export const isModestBenchError = ( (error as { code: string }).code.startsWith('ERR_MB_') ); }; - -/** - * Type guard to check if an error is a standard Error - * - * @param error - The error to check - * @returns `true` if the error is an `Error` - */ -export const isError = (error: unknown): error is Error => { - return error instanceof Error; -};