@contrail/util 1.1.15-alpha-3 → 1.1.15-alpha-5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const performance_util_1 = require("./performance-util");
|
|
4
|
+
function demonstrateStartEndSpan() {
|
|
5
|
+
console.log('--- Demonstrating startSpan/endSpan functionality ---\n');
|
|
6
|
+
(0, performance_util_1.clearDefaultTimingProfile)();
|
|
7
|
+
console.log('Example 1: Simple span');
|
|
8
|
+
(0, performance_util_1.startSpan)('Data Processing');
|
|
9
|
+
const start1 = performance.now();
|
|
10
|
+
while (performance.now() < start1 + 100) {
|
|
11
|
+
}
|
|
12
|
+
(0, performance_util_1.endSpan)('Data Processing');
|
|
13
|
+
(0, performance_util_1.displayTimingTree)();
|
|
14
|
+
console.log('\nExample 2: Nested spans');
|
|
15
|
+
(0, performance_util_1.clearDefaultTimingProfile)();
|
|
16
|
+
(0, performance_util_1.startSpan)('Complete Task');
|
|
17
|
+
(0, performance_util_1.startSpan)('Initialization');
|
|
18
|
+
const start2 = performance.now();
|
|
19
|
+
while (performance.now() < start2 + 50) { }
|
|
20
|
+
(0, performance_util_1.endSpan)('Initialization');
|
|
21
|
+
(0, performance_util_1.startSpan)('Main Processing');
|
|
22
|
+
(0, performance_util_1.startSpan)('Sub-task 1');
|
|
23
|
+
const start3 = performance.now();
|
|
24
|
+
while (performance.now() < start3 + 30) { }
|
|
25
|
+
(0, performance_util_1.endSpan)('Sub-task 1');
|
|
26
|
+
(0, performance_util_1.startSpan)('Sub-task 2');
|
|
27
|
+
const start4 = performance.now();
|
|
28
|
+
while (performance.now() < start4 + 40) { }
|
|
29
|
+
(0, performance_util_1.endSpan)('Sub-task 2');
|
|
30
|
+
(0, performance_util_1.endSpan)('Main Processing');
|
|
31
|
+
(0, performance_util_1.startSpan)('Cleanup');
|
|
32
|
+
const start5 = performance.now();
|
|
33
|
+
while (performance.now() < start5 + 20) { }
|
|
34
|
+
(0, performance_util_1.endSpan)('Cleanup');
|
|
35
|
+
(0, performance_util_1.endSpan)('Complete Task');
|
|
36
|
+
(0, performance_util_1.displayTimingTree)();
|
|
37
|
+
console.log('\nExample 3: Multiple sequential spans');
|
|
38
|
+
(0, performance_util_1.clearDefaultTimingProfile)();
|
|
39
|
+
(0, performance_util_1.startSpan)('Phase 1');
|
|
40
|
+
const start6 = performance.now();
|
|
41
|
+
while (performance.now() < start6 + 60) { }
|
|
42
|
+
(0, performance_util_1.endSpan)('Phase 1');
|
|
43
|
+
(0, performance_util_1.startSpan)('Phase 2');
|
|
44
|
+
const start7 = performance.now();
|
|
45
|
+
while (performance.now() < start7 + 80) { }
|
|
46
|
+
(0, performance_util_1.endSpan)('Phase 2');
|
|
47
|
+
(0, performance_util_1.startSpan)('Phase 3');
|
|
48
|
+
const start8 = performance.now();
|
|
49
|
+
while (performance.now() < start8 + 40) { }
|
|
50
|
+
(0, performance_util_1.endSpan)('Phase 3');
|
|
51
|
+
(0, performance_util_1.displayTimingTree)();
|
|
52
|
+
}
|
|
53
|
+
function demonstrateWarningBehavior() {
|
|
54
|
+
console.log('\n--- Demonstrating Warning Behavior ---\n');
|
|
55
|
+
(0, performance_util_1.clearDefaultTimingProfile)();
|
|
56
|
+
console.log('Example 4: Warning behavior');
|
|
57
|
+
(0, performance_util_1.startSpan)('Test Span');
|
|
58
|
+
console.log('Started "Test Span"');
|
|
59
|
+
(0, performance_util_1.startSpan)('Test Span');
|
|
60
|
+
console.log('Attempted to start "Test Span" again (should see warning above)');
|
|
61
|
+
(0, performance_util_1.endSpan)('Test Span');
|
|
62
|
+
console.log('Ended "Test Span"');
|
|
63
|
+
(0, performance_util_1.endSpan)('Non-existent Span');
|
|
64
|
+
console.log('Attempted to end "Non-existent Span" (should see warning above)');
|
|
65
|
+
(0, performance_util_1.displayTimingTree)();
|
|
66
|
+
}
|
|
67
|
+
demonstrateStartEndSpan();
|
|
68
|
+
demonstrateWarningBehavior();
|
|
@@ -5,13 +5,20 @@ type TimingNode = {
|
|
|
5
5
|
durationMs: number;
|
|
6
6
|
children: TimingNode[];
|
|
7
7
|
};
|
|
8
|
+
type ActiveSpan = {
|
|
9
|
+
node: TimingNode;
|
|
10
|
+
startTime: number;
|
|
11
|
+
};
|
|
8
12
|
type TimingProfile = {
|
|
9
13
|
timingSpans: TimingSpans;
|
|
10
14
|
timingStack: TimingStack;
|
|
15
|
+
activeSpans: Map<string, ActiveSpan>;
|
|
11
16
|
};
|
|
12
17
|
export declare function getDefaultTimingProfile(): TimingProfile;
|
|
13
18
|
export declare function clearDefaultTimingProfile(): void;
|
|
14
19
|
export declare function withTimingAsync<T>(label: string, fn: () => Promise<T>): Promise<T>;
|
|
15
20
|
export declare function withTimingSync<T>(label: string, fn: () => T): T;
|
|
16
21
|
export declare function displayTimingTree(nodes?: TimingSpans, depth?: number): string;
|
|
22
|
+
export declare function startSpan(spanName: string): void;
|
|
23
|
+
export declare function endSpan(spanName: string): void;
|
|
17
24
|
export {};
|
|
@@ -14,9 +14,12 @@ exports.clearDefaultTimingProfile = clearDefaultTimingProfile;
|
|
|
14
14
|
exports.withTimingAsync = withTimingAsync;
|
|
15
15
|
exports.withTimingSync = withTimingSync;
|
|
16
16
|
exports.displayTimingTree = displayTimingTree;
|
|
17
|
+
exports.startSpan = startSpan;
|
|
18
|
+
exports.endSpan = endSpan;
|
|
17
19
|
const DEFAULT_TIMING_PROFILE = {
|
|
18
20
|
timingSpans: [],
|
|
19
21
|
timingStack: [],
|
|
22
|
+
activeSpans: new Map(),
|
|
20
23
|
};
|
|
21
24
|
function getDefaultTimingProfile() {
|
|
22
25
|
return DEFAULT_TIMING_PROFILE;
|
|
@@ -30,6 +33,7 @@ function getTimingSpans() {
|
|
|
30
33
|
function clearDefaultTimingProfile() {
|
|
31
34
|
DEFAULT_TIMING_PROFILE.timingSpans = [];
|
|
32
35
|
DEFAULT_TIMING_PROFILE.timingStack = [];
|
|
36
|
+
DEFAULT_TIMING_PROFILE.activeSpans.clear();
|
|
33
37
|
}
|
|
34
38
|
function withTimingAsync(label, fn) {
|
|
35
39
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -93,3 +97,43 @@ function displayTimingTree(nodes = DEFAULT_TIMING_PROFILE.timingSpans, depth = 0
|
|
|
93
97
|
}
|
|
94
98
|
return result;
|
|
95
99
|
}
|
|
100
|
+
function startSpan(spanName) {
|
|
101
|
+
if (DEFAULT_TIMING_PROFILE.activeSpans.has(spanName)) {
|
|
102
|
+
console.warn(`Span "${spanName}" is already active. Please end it before starting a new one with the same name.`);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const startTime = performance.now();
|
|
106
|
+
const timingNode = { label: spanName, durationMs: 0, children: [] };
|
|
107
|
+
const parent = getTimingStack().length > 0 ? getTimingStack()[getTimingStack().length - 1] : null;
|
|
108
|
+
if (parent) {
|
|
109
|
+
parent.children.push(timingNode);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
getTimingSpans().push(timingNode);
|
|
113
|
+
}
|
|
114
|
+
getTimingStack().push(timingNode);
|
|
115
|
+
DEFAULT_TIMING_PROFILE.activeSpans.set(spanName, {
|
|
116
|
+
node: timingNode,
|
|
117
|
+
startTime: startTime,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
function endSpan(spanName) {
|
|
121
|
+
const activeSpan = DEFAULT_TIMING_PROFILE.activeSpans.get(spanName);
|
|
122
|
+
if (!activeSpan) {
|
|
123
|
+
console.warn(`No active span found with name "${spanName}". Make sure to call startSpan() first.`);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const endTime = performance.now();
|
|
127
|
+
activeSpan.node.durationMs = endTime - activeSpan.startTime;
|
|
128
|
+
const currentTop = getTimingStack()[getTimingStack().length - 1];
|
|
129
|
+
if (currentTop === activeSpan.node) {
|
|
130
|
+
getTimingStack().pop();
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
const stackIndex = getTimingStack().findIndex(node => node === activeSpan.node);
|
|
134
|
+
if (stackIndex !== -1) {
|
|
135
|
+
getTimingStack().splice(stackIndex, 1);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
DEFAULT_TIMING_PROFILE.activeSpans.delete(spanName);
|
|
139
|
+
}
|