@@ -17,6 +17,32 @@ let keyStatistics: Record<
1717 p90 ?: number ;
1818 }
1919> = { } ;
20+ /**
21+ * This is a performance analysis tool for concurrent tasks
22+ * You can use it to collect the start and end time of a task, the collected data with the same key will be summarized
23+ * the summarzied report contains the min, max, avg, median, p90 of the task
24+ * When the code snippet is executed, the performance analysis tool will automatically collect the start and end time of the task
25+ *
26+ * example:
27+ * const start = Date.now();
28+ * await fn_to_measure()
29+ * const end = Date.now();
30+ * PerformanceAnalysis.collect('fn_to_measure', start, end)
31+ *
32+ * You can choose when to summarize the performance data
33+ * for example, you can summarize the performance data before server closed
34+ *
35+ * public async close() {
36+ if (this.servers) {
37+ ... close server
38+ }
39+ PerformanceAnalysis.count();
40+ }
41+ *
42+ * Note: If you want to view the performance by each API call, you can use k6 or you can specify the group name when collecting the performance data
43+ * and implement another count & writePerformanceReport funtion to summarize the performance data by group name
44+ *
45+ */
2046export class PerformanceAnalysis {
2147 public static collect (
2248 key : string ,
@@ -42,7 +68,7 @@ export class PerformanceAnalysis {
4268 }
4369
4470 public static count ( ) : boolean {
45- // sort by diff
71+ // sort by time diff
4672 if ( isEmpty ( performanceRecord ) ) {
4773 console . log ( 'performanceRecord is empty' ) ;
4874 return false ;
@@ -78,10 +104,8 @@ export class PerformanceAnalysis {
78104 } ;
79105
80106 // write to txt file
81- // write by key
82107 public static writePerformanceReport ( ) {
83108 const filePath = path . join ( './performanceRecord.txt' ) ;
84- // write by key
85109 // print current date, time as humun readable format
86110 fs . appendFileSync ( filePath , `------${ new Date ( ) . toLocaleString ( ) } \n` ) ;
87111 for ( const key of Object . keys ( keyStatistics ) ) {
@@ -103,8 +127,9 @@ export function getAnalysis() {
103127 const counted = PerformanceAnalysis . count ( ) ;
104128 if ( counted && ! is_analysis ) {
105129 PerformanceAnalysis . writePerformanceReport ( ) ;
106- console . log ( 'performance analysis finished' ) ;
107- console . log ( 'check the performanceRecord.txt file for details' ) ;
130+ console . log (
131+ 'performance analysis finished, check the performanceRecord.txt file for details'
132+ ) ;
108133 is_analysis = true ;
109134 }
110135}
0 commit comments