@contrail/util 1.1.15-alpha-2 → 1.1.15-alpha-4

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.
@@ -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
- export declare function displayTimingTree(nodes: TimingSpans, depth?: number): string;
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* () {
@@ -70,7 +74,7 @@ function withTimingSync(label, fn) {
70
74
  getTimingStack().pop();
71
75
  }
72
76
  }
73
- function displayTimingTree(nodes, depth = 0) {
77
+ function displayTimingTree(nodes = DEFAULT_TIMING_PROFILE.timingSpans, depth = 0) {
74
78
  const lines = [];
75
79
  const indent = ' '.repeat(depth);
76
80
  const totalWidth = 80;
@@ -93,3 +97,41 @@ function displayTimingTree(nodes, depth = 0) {
93
97
  }
94
98
  return result;
95
99
  }
100
+ function startSpan(spanName) {
101
+ if (DEFAULT_TIMING_PROFILE.activeSpans.has(spanName)) {
102
+ throw new Error(`Span "${spanName}" is already active. Please end it before starting a new one with the same name.`);
103
+ }
104
+ const startTime = performance.now();
105
+ const timingNode = { label: spanName, durationMs: 0, children: [] };
106
+ const parent = getTimingStack().length > 0 ? getTimingStack()[getTimingStack().length - 1] : null;
107
+ if (parent) {
108
+ parent.children.push(timingNode);
109
+ }
110
+ else {
111
+ getTimingSpans().push(timingNode);
112
+ }
113
+ getTimingStack().push(timingNode);
114
+ DEFAULT_TIMING_PROFILE.activeSpans.set(spanName, {
115
+ node: timingNode,
116
+ startTime: startTime,
117
+ });
118
+ }
119
+ function endSpan(spanName) {
120
+ const activeSpan = DEFAULT_TIMING_PROFILE.activeSpans.get(spanName);
121
+ if (!activeSpan) {
122
+ throw new Error(`No active span found with name "${spanName}". Make sure to call startSpan() first.`);
123
+ }
124
+ const endTime = performance.now();
125
+ activeSpan.node.durationMs = endTime - activeSpan.startTime;
126
+ const currentTop = getTimingStack()[getTimingStack().length - 1];
127
+ if (currentTop === activeSpan.node) {
128
+ getTimingStack().pop();
129
+ }
130
+ else {
131
+ const stackIndex = getTimingStack().findIndex(node => node === activeSpan.node);
132
+ if (stackIndex !== -1) {
133
+ getTimingStack().splice(stackIndex, 1);
134
+ }
135
+ }
136
+ DEFAULT_TIMING_PROFILE.activeSpans.delete(spanName);
137
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/util",
3
- "version": "1.1.15-alpha-2",
3
+ "version": "1.1.15-alpha-4",
4
4
  "description": "General JavaScript utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",