forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance.js
More file actions
194 lines (194 loc) · 6.34 KB
/
performance.js
File metadata and controls
194 lines (194 loc) · 6.34 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.disable = exports.enable = exports.isEnabled = exports.clearMarks = exports.clearMeasures = exports.forEachMark = exports.forEachMeasure = exports.getDuration = exports.getCount = exports.measure = exports.mark = exports.nullTimer = exports.createTimer = exports.createTimerIf = void 0;
var ts_1 = require("./_namespaces/ts");
var perfHooks;
// when set, indicates the implementation of `Performance` to use for user timing.
// when unset, indicates user timing is unavailable or disabled.
var performanceImpl;
/** @internal */
function createTimerIf(condition, measureName, startMarkName, endMarkName) {
return condition ? createTimer(measureName, startMarkName, endMarkName) : exports.nullTimer;
}
exports.createTimerIf = createTimerIf;
/** @internal */
function createTimer(measureName, startMarkName, endMarkName) {
var enterCount = 0;
return {
enter: enter,
exit: exit
};
function enter() {
if (++enterCount === 1) {
mark(startMarkName);
}
}
function exit() {
if (--enterCount === 0) {
mark(endMarkName);
measure(measureName, startMarkName, endMarkName);
}
else if (enterCount < 0) {
ts_1.Debug.fail("enter/exit count does not match.");
}
}
}
exports.createTimer = createTimer;
/** @internal */
exports.nullTimer = { enter: ts_1.noop, exit: ts_1.noop };
var enabled = false;
var timeorigin = (0, ts_1.timestamp)();
var marks = new Map();
var counts = new Map();
var durations = new Map();
/**
* Marks a performance event.
*
* @param markName The name of the mark.
*
* @internal
*/
function mark(markName) {
var _a;
if (enabled) {
var count = (_a = counts.get(markName)) !== null && _a !== void 0 ? _a : 0;
counts.set(markName, count + 1);
marks.set(markName, (0, ts_1.timestamp)());
performanceImpl === null || performanceImpl === void 0 ? void 0 : performanceImpl.mark(markName);
if (typeof onProfilerEvent === "function") {
onProfilerEvent(markName);
}
}
}
exports.mark = mark;
/**
* Adds a performance measurement with the specified name.
*
* @param measureName The name of the performance measurement.
* @param startMarkName The name of the starting mark. If not supplied, the point at which the
* profiler was enabled is used.
* @param endMarkName The name of the ending mark. If not supplied, the current timestamp is
* used.
*
* @internal
*/
function measure(measureName, startMarkName, endMarkName) {
var _a, _b;
if (enabled) {
var end = (_a = (endMarkName !== undefined ? marks.get(endMarkName) : undefined)) !== null && _a !== void 0 ? _a : (0, ts_1.timestamp)();
var start = (_b = (startMarkName !== undefined ? marks.get(startMarkName) : undefined)) !== null && _b !== void 0 ? _b : timeorigin;
var previousDuration = durations.get(measureName) || 0;
durations.set(measureName, previousDuration + (end - start));
performanceImpl === null || performanceImpl === void 0 ? void 0 : performanceImpl.measure(measureName, startMarkName, endMarkName);
}
}
exports.measure = measure;
/**
* Gets the number of times a marker was encountered.
*
* @param markName The name of the mark.
*
* @internal
*/
function getCount(markName) {
return counts.get(markName) || 0;
}
exports.getCount = getCount;
/**
* Gets the total duration of all measurements with the supplied name.
*
* @param measureName The name of the measure whose durations should be accumulated.
*
* @internal
*/
function getDuration(measureName) {
return durations.get(measureName) || 0;
}
exports.getDuration = getDuration;
/**
* Iterate over each measure, performing some action
*
* @param cb The action to perform for each measure
*
* @internal
*/
function forEachMeasure(cb) {
durations.forEach(function (duration, measureName) { return cb(measureName, duration); });
}
exports.forEachMeasure = forEachMeasure;
/** @internal */
function forEachMark(cb) {
marks.forEach(function (_time, markName) { return cb(markName); });
}
exports.forEachMark = forEachMark;
/** @internal */
function clearMeasures(name) {
if (name !== undefined)
durations.delete(name);
else
durations.clear();
performanceImpl === null || performanceImpl === void 0 ? void 0 : performanceImpl.clearMeasures(name);
}
exports.clearMeasures = clearMeasures;
/** @internal */
function clearMarks(name) {
if (name !== undefined) {
counts.delete(name);
marks.delete(name);
}
else {
counts.clear();
marks.clear();
}
performanceImpl === null || performanceImpl === void 0 ? void 0 : performanceImpl.clearMarks(name);
}
exports.clearMarks = clearMarks;
/**
* Indicates whether the performance API is enabled.
*
* @internal
*/
function isEnabled() {
return enabled;
}
exports.isEnabled = isEnabled;
/**
* Enables (and resets) performance measurements for the compiler.
*
* @internal
*/
function enable(system) {
var _a;
if (system === void 0) { system = ts_1.sys; }
if (!enabled) {
enabled = true;
perfHooks || (perfHooks = (0, ts_1.tryGetNativePerformanceHooks)());
if (perfHooks) {
timeorigin = perfHooks.performance.timeOrigin;
// NodeJS's Web Performance API is currently slower than expected, but we'd still like
// to be able to leverage native trace events when node is run with either `--cpu-prof`
// or `--prof`, if we're running with our own `--generateCpuProfile` flag, or when
// running in debug mode (since its possible to generate a cpu profile while debugging).
if (perfHooks.shouldWriteNativeEvents || ((_a = system === null || system === void 0 ? void 0 : system.cpuProfilingEnabled) === null || _a === void 0 ? void 0 : _a.call(system)) || (system === null || system === void 0 ? void 0 : system.debugMode)) {
performanceImpl = perfHooks.performance;
}
}
}
return true;
}
exports.enable = enable;
/**
* Disables performance measurements for the compiler.
*
* @internal
*/
function disable() {
if (enabled) {
marks.clear();
counts.clear();
durations.clear();
performanceImpl = undefined;
enabled = false;
}
}
exports.disable = disable;