@@ -6,104 +6,90 @@ namespace ts {
6
6
}
7
7
8
8
/*@internal */
9
+ /** Performance measurements for the compiler. */
9
10
namespace ts . performance {
10
- /** Performance measurements for the compiler. */
11
11
declare const onProfilerEvent : { ( markName : string ) : void ; profiler : boolean ; } ;
12
- let profilerEvent : ( markName : string ) => void ;
13
- let counters : MapLike < number > ;
14
- let measures : MapLike < number > ;
12
+
13
+ const profilerEvent = typeof onProfilerEvent === "function" && onProfilerEvent . profiler === true
14
+ ? onProfilerEvent
15
+ : ( markName : string ) => { } ;
16
+
17
+ let enabled = false ;
18
+ let profilerStart = 0 ;
19
+ let counts : Map < number > ;
20
+ let marks : Map < number > ;
21
+ let measures : Map < number > ;
15
22
16
23
/**
17
- * Emit a performance event if ts-profiler is connected. This is primarily used
18
- * to generate heap snapshots.
24
+ * Marks a performance event.
19
25
*
20
- * @param eventName A name for the event .
26
+ * @param markName The name of the mark .
21
27
*/
22
- export function emit ( eventName : string ) {
23
- if ( profilerEvent ) {
24
- profilerEvent ( eventName ) ;
28
+ export function mark ( markName : string ) {
29
+ if ( enabled ) {
30
+ marks [ markName ] = timestamp ( ) ;
31
+ counts [ markName ] = ( counts [ markName ] || 0 ) + 1 ;
32
+ profilerEvent ( markName ) ;
25
33
}
26
34
}
27
35
28
36
/**
29
- * Increments a counter with the specified name.
37
+ * Adds a performance measurement with the specified name.
30
38
*
31
- * @param counterName The name of the counter.
39
+ * @param measureName The name of the performance measurement.
40
+ * @param startMarkName The name of the starting mark. If not supplied, the point at which the
41
+ * profiler was enabled is used.
42
+ * @param endMarkName The name of the ending mark. If not supplied, the current timestamp is
43
+ * used.
32
44
*/
33
- export function increment ( counterName : string ) {
34
- if ( counters ) {
35
- counters [ counterName ] = ( getProperty ( counters , counterName ) || 0 ) + 1 ;
45
+ export function measure ( measureName : string , startMarkName ?: string , endMarkName ?: string ) {
46
+ if ( enabled ) {
47
+ const end = endMarkName && marks [ endMarkName ] || timestamp ( ) ;
48
+ const start = startMarkName && marks [ startMarkName ] || profilerStart ;
49
+ measures [ measureName ] = ( measures [ measureName ] || 0 ) + ( end - start ) ;
36
50
}
37
51
}
38
52
39
53
/**
40
- * Gets the value of the counter with the specified name .
54
+ * Gets the number of times a marker was encountered .
41
55
*
42
- * @param counterName The name of the counter.
43
- */
44
- export function getCount ( counterName : string ) {
45
- return counters && getProperty ( counters , counterName ) || 0 ;
46
- }
47
-
48
- /**
49
- * Marks the start of a performance measurement.
56
+ * @param markName The name of the mark.
50
57
*/
51
- export function mark ( ) {
52
- return measures ? timestamp ( ) : 0 ;
58
+ export function getCount ( markName : string ) {
59
+ return counts && counts [ markName ] || 0 ;
53
60
}
54
61
55
62
/**
56
- * Adds a performance measurement with the specified name.
63
+ * Gets the total duration of all measurements with the supplied name.
57
64
*
58
- * @param measureName The name of the performance measurement.
59
- * @param marker The timestamp of the starting mark.
65
+ * @param measureName The name of the measure whose durations should be accumulated.
60
66
*/
61
- export function measure ( measureName : string , marker : number ) {
62
- if ( measures ) {
63
- measures [ measureName ] = ( getProperty ( measures , measureName ) || 0 ) + ( timestamp ( ) - marker ) ;
64
- }
67
+ export function getDuration ( measureName : string ) {
68
+ return measures && measures [ measureName ] || 0 ;
65
69
}
66
70
67
71
/**
68
72
* Iterate over each measure, performing some action
69
- *
73
+ *
70
74
* @param cb The action to perform for each measure
71
75
*/
72
76
export function forEachMeasure ( cb : ( measureName : string , duration : number ) => void ) {
73
- return forEachKey ( measures , key => cb ( key , measures [ key ] ) ) ;
74
- }
75
-
76
- /**
77
- * Gets the total duration of all measurements with the supplied name.
78
- *
79
- * @param measureName The name of the measure whose durations should be accumulated.
80
- */
81
- export function getDuration ( measureName : string ) {
82
- return measures && getProperty ( measures , measureName ) || 0 ;
77
+ for ( const key in measures ) {
78
+ cb ( key , measures [ key ] ) ;
79
+ }
83
80
}
84
81
85
82
/** Enables (and resets) performance measurements for the compiler. */
86
83
export function enable ( ) {
87
- counters = { } ;
88
- measures = {
89
- "I/O Read" : 0 ,
90
- "I/O Write" : 0 ,
91
- "Program" : 0 ,
92
- "Parse" : 0 ,
93
- "Bind" : 0 ,
94
- "Check" : 0 ,
95
- "Emit" : 0 ,
96
- } ;
97
-
98
- profilerEvent = typeof onProfilerEvent === "function" && onProfilerEvent . profiler === true
99
- ? onProfilerEvent
100
- : undefined ;
84
+ counts = createMap < number > ( ) ;
85
+ marks = createMap < number > ( ) ;
86
+ measures = createMap < number > ( ) ;
87
+ enabled = true ;
88
+ profilerStart = timestamp ( ) ;
101
89
}
102
90
103
- /** Disables (and clears) performance measurements for the compiler. */
91
+ /** Disables performance measurements for the compiler. */
104
92
export function disable ( ) {
105
- counters = undefined ;
106
- measures = undefined ;
107
- profilerEvent = undefined ;
93
+ enabled = false ;
108
94
}
109
95
}
0 commit comments