@@ -17,6 +17,32 @@ let keyStatistics: Record<
17
17
p90 ?: number ;
18
18
}
19
19
> = { } ;
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
+ */
20
46
export class PerformanceAnalysis {
21
47
public static collect (
22
48
key : string ,
@@ -42,7 +68,7 @@ export class PerformanceAnalysis {
42
68
}
43
69
44
70
public static count ( ) : boolean {
45
- // sort by diff
71
+ // sort by time diff
46
72
if ( isEmpty ( performanceRecord ) ) {
47
73
console . log ( 'performanceRecord is empty' ) ;
48
74
return false ;
@@ -78,10 +104,8 @@ export class PerformanceAnalysis {
78
104
} ;
79
105
80
106
// write to txt file
81
- // write by key
82
107
public static writePerformanceReport ( ) {
83
108
const filePath = path . join ( './performanceRecord.txt' ) ;
84
- // write by key
85
109
// print current date, time as humun readable format
86
110
fs . appendFileSync ( filePath , `------${ new Date ( ) . toLocaleString ( ) } \n` ) ;
87
111
for ( const key of Object . keys ( keyStatistics ) ) {
@@ -103,8 +127,9 @@ export function getAnalysis() {
103
127
const counted = PerformanceAnalysis . count ( ) ;
104
128
if ( counted && ! is_analysis ) {
105
129
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
+ ) ;
108
133
is_analysis = true ;
109
134
}
110
135
}
0 commit comments