Skip to content

Commit d4b59d8

Browse files
committed
Add --duration flag for zero-overhead duration measurement
1 parent c11d62f commit d4b59d8

3 files changed

Lines changed: 9 additions & 3 deletions

File tree

packages/knip/src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const main = async () => {
9595
if (perfObserver.isMemoryUsageEnabled && !args['memory-realtime'])
9696
console.log(`\n${perfObserver.getMemoryUsageTable()}`);
9797

98-
if (perfObserver.isEnabled) {
98+
if (perfObserver.isEnabled || perfObserver.isDurationEnabled) {
9999
const duration = perfObserver.getCurrentDurationInMs();
100100
console.log('\nTotal running time:', prettyMilliseconds(duration));
101101
perfObserver.reset();

packages/knip/src/util/Performance.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ const { values } = parseArgs({
1111
'performance-fn': { type: 'string', multiple: true },
1212
memory: { type: 'boolean' },
1313
'memory-realtime': { type: 'boolean' },
14+
duration: { type: 'boolean' },
1415
},
1516
});
1617

1718
const timerifyOnlyFnName = values['performance-fn'];
1819
const isMemoryRealtime = !!values['memory-realtime'];
1920
const isTimerifyFunctions = !!values.performance || !!timerifyOnlyFnName;
2021
const isMemoryUsageEnabled = !!values.memory || isMemoryRealtime;
22+
const isDurationEnabled = !!values.duration;
2123

2224
export const timerify = <T extends (...params: any[]) => any>(fn: T, name: string = fn.name): T => {
2325
if (!isTimerifyFunctions) return fn;
@@ -56,6 +58,7 @@ class Performance {
5658
isEnabled: boolean;
5759
isTimerifyFunctions: boolean;
5860
isMemoryUsageEnabled: boolean;
61+
isDurationEnabled: boolean;
5962
startTime = 0;
6063
endTime = 0;
6164
perfEntries: PerformanceEntry[] = [];
@@ -65,10 +68,11 @@ class Performance {
6568
fnObserver?: PerformanceObserver;
6669
memObserver?: PerformanceObserver;
6770

68-
constructor({ isTimerifyFunctions = false, isMemoryUsageEnabled = false }) {
71+
constructor({ isTimerifyFunctions = false, isMemoryUsageEnabled = false, isDurationEnabled = false }) {
6972
this.isEnabled = isTimerifyFunctions || isMemoryUsageEnabled;
7073
this.isTimerifyFunctions = isTimerifyFunctions;
7174
this.isMemoryUsageEnabled = isMemoryUsageEnabled;
75+
this.isDurationEnabled = isDurationEnabled;
7276

7377
this.startTime = performance.now();
7478
const instanceId = Math.floor(performance.now() * 100);
@@ -206,4 +210,4 @@ class Performance {
206210
}
207211
}
208212

209-
export const perfObserver = new Performance({ isTimerifyFunctions, isMemoryUsageEnabled });
213+
export const perfObserver = new Performance({ isTimerifyFunctions, isMemoryUsageEnabled, isDurationEnabled });

packages/knip/src/util/cli-arguments.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Troubleshooting
5555
--memory-realtime Log memory usage in realtime
5656
--performance Measure count and running time of key functions and display stats table
5757
--performance-fn [name] Measure only function [name]
58+
--duration Print total running time (zero overhead, no instrumentation)
5859
--trace Show trace output
5960
--trace-dependency [name] Show files that import the named dependency
6061
--trace-export [name] Show trace output for named export(s)
@@ -115,6 +116,7 @@ export default function parseCLIArgs() {
115116
'preprocessor-options': { type: 'string' },
116117
reporter: { type: 'string', multiple: true },
117118
'reporter-options': { type: 'string' },
119+
duration: { type: 'boolean' },
118120
strict: { type: 'boolean' },
119121
trace: { type: 'boolean' },
120122
'trace-dependency': { type: 'string' },

0 commit comments

Comments
 (0)