@@ -6,104 +6,90 @@ namespace ts {
66}
77
88/*@internal */
9+ /** Performance measurements for the compiler. */
910namespace ts . performance {
10- /** Performance measurements for the compiler. */
1111 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 > ;
1522
1623 /**
17- * Emit a performance event if ts-profiler is connected. This is primarily used
18- * to generate heap snapshots.
24+ * Marks a performance event.
1925 *
20- * @param eventName A name for the event .
26+ * @param markName The name of the mark .
2127 */
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 ) ;
2533 }
2634 }
2735
2836 /**
29- * Increments a counter with the specified name.
37+ * Adds a performance measurement with the specified name.
3038 *
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.
3244 */
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 ) ;
3650 }
3751 }
3852
3953 /**
40- * Gets the value of the counter with the specified name .
54+ * Gets the number of times a marker was encountered .
4155 *
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.
5057 */
51- export function mark ( ) {
52- return measures ? timestamp ( ) : 0 ;
58+ export function getCount ( markName : string ) {
59+ return counts && counts [ markName ] || 0 ;
5360 }
5461
5562 /**
56- * Adds a performance measurement with the specified name.
63+ * Gets the total duration of all measurements with the supplied name.
5764 *
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.
6066 */
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 ;
6569 }
6670
6771 /**
6872 * Iterate over each measure, performing some action
69- *
73+ *
7074 * @param cb The action to perform for each measure
7175 */
7276 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+ }
8380 }
8481
8582 /** Enables (and resets) performance measurements for the compiler. */
8683 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 ( ) ;
10189 }
10290
103- /** Disables (and clears) performance measurements for the compiler. */
91+ /** Disables performance measurements for the compiler. */
10492 export function disable ( ) {
105- counters = undefined ;
106- measures = undefined ;
107- profilerEvent = undefined ;
93+ enabled = false ;
10894 }
10995}
0 commit comments