-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecutionTimer.ts
More file actions
137 lines (123 loc) · 4.92 KB
/
executionTimer.ts
File metadata and controls
137 lines (123 loc) · 4.92 KB
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
import { TimerDetailsModel } from '../common/models/timer.model';
/**
* A class for measuring the execution time of code blocks.
*/
export class ExecutionTimer {
private timer: { [key: string]: { startTime: number; endTime: number } } = {};
/**
* Creates an instance of ExecutionTimer.
* @param executionId - An optional identifier for the execution timer. Defaults to 'default'.
*/
constructor(executionId?: string) {
this.timer[executionId ?? 'default'] = {
startTime: 0,
endTime: 0
};
}
/**
* Starts the execution timer.
* @param executionId - An optional identifier for the execution timer. Defaults to 'default'.
*/
start(executionId?: string): void {
this.timer[executionId ?? 'default'] = {
startTime: performance.now(),
endTime: 0
};
}
/**
* Stops the execution timer.
* @param executionId - An optional identifier for the execution timer. Defaults to 'default'.
*/
stop(executionId?: string): void {
if (this.timer[executionId ?? 'default']?.startTime) {
this.timer[executionId ?? 'default'].endTime = performance.now();
}
}
/**
* Gets the duration of the execution timer in milliseconds.
* @param executionId - An optional identifier for the execution timer. Defaults to 'default'.
* @param fractionDigits – The number of digits to appear after the decimal point; should be a value between 0 and 100, inclusive.
* @returns The duration of the execution timer in milliseconds.
*/
getDuration(executionId?: string, fractionDigits?: number): number {
const timerId = executionId ?? 'default';
if (this.timer[executionId ?? 'default']?.startTime) {
if (!this.timer[executionId ?? 'default'].endTime) {
this.stop(timerId);
}
const duration = this.timer[executionId ?? 'default'].endTime - this.timer[executionId ?? 'default'].startTime;
return Number.isFinite(fractionDigits) ? Number(duration?.toFixed(fractionDigits)) : duration;
}
}
/**
* Gets the start date of the execution timer.
* @param executionId - An optional identifier for the execution timer. Defaults to 'default'.
* @returns The start date of the execution timer.
*/
getStartDate(executionId?: string): Date | undefined {
if (this.timer[executionId ?? 'default']?.startTime) {
const currentTime = performance.timeOrigin + this.timer[executionId ?? 'default']?.startTime;
return new Date(currentTime);
}
}
/**
* Gets the end date of the execution timer.
* @param executionId - An optional identifier for the execution timer. Defaults to 'default'.
* @returns The end date of the execution timer.
*/
getEndDate(executionId?: string): Date | undefined {
if (this.timer[executionId ?? 'default']?.endTime) {
const currentTime = performance.timeOrigin + this.timer[executionId ?? 'default']?.endTime;
return new Date(currentTime);
}
}
/**
* Gets the human-readable elapsed time of the execution timer.
* @param executionId - An optional identifier for the execution timer. Defaults to 'default'.
* @param fractionDigits – The number of digits to appear after the decimal point; should be a value between 0 and 100, inclusive.
* @returns A string representing the human-readable elapsed time.
*/
getElapsedTime(executionId?: string, fractionDigits?: number): string | undefined {
const duration = this.getDuration(executionId);
if (duration === undefined) {
return undefined;
}
const milliseconds = duration % 1000;
const seconds = Math.floor((duration / 1000) % 60);
const minutes = Math.floor((duration / (1000 * 60)) % 60);
const hours = Math.floor((duration / (1000 * 60 * 60)) % 24);
const parts = [];
if (hours > 0) {
parts.push(`${hours} hour${hours > 1 ? 's' : ''}`);
}
if (minutes > 0) {
parts.push(`${minutes} minute${minutes > 1 ? 's' : ''}`);
}
if (seconds > 0) {
parts.push(`${seconds} second${seconds > 1 ? 's' : ''}`);
}
if (parts?.length) {
parts.push('and');
}
if (milliseconds > 0) {
parts.push(`${Number.isFinite(fractionDigits) ? milliseconds.toFixed(fractionDigits) : milliseconds} ms`);
}
return parts.join(' ');
}
/**
* Gets details of a specific execution timer.
* @param executionId - The timer ID. Defaults to 'default'.
* @param durationFractionDigits - Decimal places for milliseconds.
* @param elapsedTimeFractionDigits - Decimal places for milliseconds.
* @returns An object containing timer details.
*/
getInfo(executionId: string = 'default', durationFractionDigits?: number, elapsedTimeFractionDigits?: number): TimerDetailsModel {
return {
executionId,
startTime: this.getStartDate(executionId),
endTime: this.getEndDate(executionId),
duration: this.getDuration(executionId, durationFractionDigits),
elapsedTime: this.getElapsedTime(executionId, elapsedTimeFractionDigits)
};
}
}