Skip to content

Commit 47efa9b

Browse files
committed
chore: refactors
`src/cli/handlers.ts`: Replaced nested ternary with readable switch statement for signal exit codes `src/cli/commands/baseline.ts`: Removed duplicate formatDuration and formatOpsPerSec functions, now imports from shared utils `src/cli/commands/history.ts`: Removed duplicate formatBytes function, now imports from shared utils `src/errors/base.ts`: Consolidated isError type guard to use the one in `type-guards.ts`
1 parent e012834 commit 47efa9b

File tree

4 files changed

+18
-61
lines changed

4 files changed

+18
-61
lines changed

src/cli/commands/baseline.ts

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import type { CliContext } from '../index.js';
1111

1212
import { BaselineStorageService } from '../../services/baseline-storage.js';
1313
import { createTaskId } from '../../types/index.js';
14+
import {
15+
formatDuration,
16+
formatOpsPerSecond,
17+
} from '../../utils/reporter-utils.js';
1418

1519
/**
1620
* Options for baseline analyze command
@@ -62,38 +66,6 @@ interface BaselineShowOptions extends BaselineBaseOptions {
6266
name: string;
6367
}
6468

65-
/**
66-
* Format duration in human-readable format
67-
*/
68-
const formatDuration = (nanoseconds: number): string => {
69-
if (nanoseconds < 1000) {
70-
return `${nanoseconds.toFixed(2)}ns`;
71-
}
72-
if (nanoseconds < 1_000_000) {
73-
return `${(nanoseconds / 1000).toFixed(2)}μs`;
74-
}
75-
if (nanoseconds < 1_000_000_000) {
76-
return `${(nanoseconds / 1_000_000).toFixed(2)}ms`;
77-
}
78-
return `${(nanoseconds / 1_000_000_000).toFixed(2)}s`;
79-
};
80-
81-
/**
82-
* Format operations per second
83-
*/
84-
const formatOpsPerSec = (ops: number): string => {
85-
if (ops < 1000) {
86-
return `${ops.toFixed(2)} ops/sec`;
87-
}
88-
if (ops < 1_000_000) {
89-
return `${(ops / 1000).toFixed(2)}K ops/sec`;
90-
}
91-
if (ops < 1_000_000_000) {
92-
return `${(ops / 1_000_000).toFixed(2)}M ops/sec`;
93-
}
94-
return `${(ops / 1_000_000_000).toFixed(2)}B ops/sec`;
95-
};
96-
9769
/**
9870
* Format date in readable format
9971
*/
@@ -337,7 +309,9 @@ export const handleShowCommand = async (
337309
for (const [taskId, data] of tasks) {
338310
console.log(` ${taskId}`);
339311
console.log(` Mean: ${formatDuration(data.mean)}`);
340-
console.log(` Ops/sec: ${formatOpsPerSec(data.opsPerSecond)}`);
312+
console.log(
313+
` Ops/sec: ${formatOpsPerSecond(data.opsPerSecond)}`,
314+
);
341315
if (data.p99) {
342316
console.log(` P99: ${formatDuration(data.p99)}`);
343317
}

src/cli/commands/history.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
parseDate,
2323
} from '../../services/history/query.js';
2424
import { TrendAnalysisService } from '../../services/history/trend-analysis.js';
25+
import { formatBytes } from '../../utils/reporter-utils.js';
2526

2627
/**
2728
* Base options shared by all history subcommands
@@ -94,22 +95,6 @@ interface HistoryTrendsOptions extends BaseHistoryOptions {
9495
until?: string | undefined;
9596
}
9697

97-
/**
98-
* Format bytes in human-readable format
99-
*/
100-
const formatBytes = (bytes: number): string => {
101-
const units = ['B', 'KB', 'MB', 'GB'];
102-
let size = bytes;
103-
let unitIndex = 0;
104-
105-
while (size >= 1024 && unitIndex < units.length - 1) {
106-
size /= 1024;
107-
unitIndex++;
108-
}
109-
110-
return `${size.toFixed(1)} ${units[unitIndex]}`;
111-
};
112-
11398
/**
11499
* Resolve a partial run ID to a full ID by checking prefix match
115100
*

src/cli/handlers.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
*/
99

1010
import { ABORT_TIMEOUT, ExitCodes } from '../constants.js';
11-
import { isError } from '../errors/base.js';
1211
import { isModestBenchError, UnknownError } from '../errors/index.js';
12+
import { isError } from '../utils/type-guards.js';
1313

1414
/**
1515
* Handle process signals gracefully
@@ -65,5 +65,12 @@ export const setupSignalHandlers = (abortController: AbortController): void => {
6565
* @returns The exit code
6666
*/
6767
const computeExitCode = (signal: NodeJS.Signals): number => {
68-
return 128 + (signal === 'SIGINT' ? 2 : signal === 'SIGQUIT' ? 3 : 15);
68+
switch (signal) {
69+
case 'SIGINT':
70+
return 130; // 128 + 2
71+
case 'SIGQUIT':
72+
return 131; // 128 + 3
73+
default:
74+
return 143; // 128 + 15 (SIGTERM)
75+
}
6976
};

src/errors/base.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import { SITE_URL } from '../constants.js';
9+
import { isError } from '../utils/type-guards.js';
910

1011
/**
1112
* Base URL for error documentation
@@ -150,13 +151,3 @@ export const isModestBenchError = (
150151
(error as { code: string }).code.startsWith('ERR_MB_')
151152
);
152153
};
153-
154-
/**
155-
* Type guard to check if an error is a standard Error
156-
*
157-
* @param error - The error to check
158-
* @returns `true` if the error is an `Error`
159-
*/
160-
export const isError = (error: unknown): error is Error => {
161-
return error instanceof Error;
162-
};

0 commit comments

Comments
 (0)